You are not logged in.
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
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
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
Offline
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
But *.ls works both ways, and I'm sure that's what badboy meant.
Offline
find . -executable -type f -exec '{}' \;
Offline
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
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
So far Kitty's solution is the most sensible one.
Don't use ls in Bash scripts: http://mywiki.wooledge.org/ParsingLs
Offline
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
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