You are not logged in.

#1 2010-01-25 12:48:15

zfsd
Member
Registered: 2010-01-13
Posts: 15

Howto run bash scripts inside bash scripts?

Greetings.

I'm trying to make a simple script which executes all bash scripts in sight.

Here's what I've got:

#!/bin/bash
for i in $( ls *.sh ); do
        bash $i
done

What is the right syntax?

Thanks.

Last edited by zfsd (2010-01-25 12:54:34)

Offline

#2 2010-01-25 13:05:53

badboy
Member
Registered: 2009-01-02
Posts: 32

Re: Howto run bash scripts inside bash scripts?

never ever use ls in a for-loop, even better if you don't even use it in any script.

for file in *; do
  bash $file
done

should work fine. Any problems with it?

if all scripts are executable (chmod +x file) you can use ./${file}, too

Offline

#3 2010-01-25 13:40:14

zfsd
Member
Registered: 2010-01-13
Posts: 15

Re: Howto run bash scripts inside bash scripts?

Thanks, that worked perfectly!

I was following an example given at this site.
As long as the ls isn't inside the loop, it shouldn't be a prolem?

Thanks again smile

Offline

#4 2010-01-25 13:48:25

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Howto run bash scripts inside bash scripts?

badboy wrote:

never ever use ls in a for-loop, even better if you don't even use it in any script.

for file in *; do
  bash $file
done

should work fine. Any problems with it?

if all scripts are executable (chmod +x file) you can use ./${file}, too

This will also attempt to run files which aren't bash scripts as bash scripts.  The ls is fine.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#5 2010-01-25 13:52:47

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Howto run bash scripts inside bash scripts?

But *.ls works both ways, and I'm sure that's what badboy meant.

Offline

#6 2010-01-25 14:39:39

scj
Member
From: Sweden
Registered: 2007-09-23
Posts: 158

Re: Howto run bash scripts inside bash scripts?

find . -executable -type f -exec '{}' \;

Offline

#7 2010-01-25 16:16:40

Kitty
Member
From: The Burning Desert
Registered: 2008-01-11
Posts: 88

Re: Howto run bash scripts inside bash scripts?

cd ~/scripts || exit 1
for file in *.sh; do
  bash $file
done

This might work even better for you.

Since you used 'ls *.sh' in your original example, I'm assuming that you've named everything you want to run in that format. Change ~/scripts to where ever you're keeping them.


/etc/rc.d/ is where daemons reside. Beware.

Offline

#8 2010-01-25 21:55:07

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: Howto run bash scripts inside bash scripts?

Whenever I use ls, it's like this:

IFS=$'\n'
for file in $(ls -A1); do
  echo "$file"
done

that avoids any whitespace issues you usually get with ls (unless the filenames contain newlines which is almost never), and -A shows dotfiles too


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#9 2010-01-25 23:02:58

res
Member
Registered: 2010-01-14
Posts: 55

Re: Howto run bash scripts inside bash scripts?

So far Kitty's solution is the most sensible one.

Don't use ls in Bash scripts: http://mywiki.wooledge.org/ParsingLs

Offline

#10 2010-01-25 23:21:07

GraveyardPC
Member
Registered: 2008-11-29
Posts: 99

Re: Howto run bash scripts inside bash scripts?

None of the versions provided thus far are full proof. Simply checking if a file is executable does not guarantee it's a shell script, and checking if it has a ".sh" extension isn't either. The only real way you can be sure of both is by doing this:

for file in *; do
  case $(file -biL $file) in
    *text/x-shellscript*) [ -x "$file" ] && ./$file ;;
  esac
done

Last edited by GraveyardPC (2010-01-25 23:26:03)

Offline

#11 2010-01-29 12:07:30

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: Howto run bash scripts inside bash scripts?

Rather:
for file in *; do file -b $file | grep /bin/bash &>/dev/null && [[ -x $file ]] && ./$file; done

Last edited by JohannesSM64 (2010-01-29 12:08:01)

Offline

Board footer

Powered by FluxBB