I have many shell scripts in a directory starting with letter 'a'. How could I execute all of those shell scripts at a shot? Can we develop any other small script to run this?
3 Answers
command line:
for each in path_to_directory/a*.sh ; do bash $each ; done
example:
:~$ ll | grep \.sh
-rw-rw-r-- 1 user user 1476 Май 25 16:07 123.sh
-rw-rw-r-- 1 user user 1176 Май 25 16:09 222.sh
-rw-rw-r-- 1 user user 419 Май 20 14:03 2.sh
-rw-rw-r-- 1 user user 191 Май 20 17:50 3.sh
-rw-rw-r-- 1 user user 9 Июн 2 11:53 a1.sh
-rw-rw-r-- 1 user user 9 Июн 2 11:53 a2.sh
i:~$ cat q1.sh
echo sadf
:~$ cat a1.sh
echo 123
:~$ cat a2.sh
echo 234
:~$ for each in ./a*.sh ; do bash $each ; done
123
234
OR you can use a command line utility called run-parts. This tool can automatically discover multiple scripts or programs in a directory, and run them all.
For example, to run all scripts in /etc whose names start with 'a' and end with '.sh':
$ run-parts --regex '^a.*\.sh$' /etc
With "--test" option, you can print the names of the scripts which would be executed, without actually running them. This is useful for testing purpose
$ run-parts --test ./my_script_directory

- 2,177
find . -maxdepth 1 -type f -perm /111 -name 'a*' -exec {} \;
or
find . -maxdepth 1 -type f -executable -name 'a*' -exec {} \;
Either of these will find all filenames beginning with a
in the current directory (but not subdirectories) with owner, group, or other execution bit(s) set, and try to run them.
I say "try" because the attempt may fail, e.g., if only the owner and group execution bits are set AND you are neither the owner nor a member of the group. In that case, you would have no permission to execute the file.
Another possible failure mode is that it can only execute scripts that can be interpreted by your current shell. so an awk
or perl
etc script beginning with a
won't run - it will fail with syntax errors because they're not shell scripts.
Another method is:
for f in ./a* ; do [ -x "$f" ] && [ ! -d "$f" ] && "$f" ; done
That will execute anything beginning with a
that is executable by you (and is NOT a directory), no matter what kind of executable it is - shell script, awk script, perl script, binary, etc.

- 78,579
You can try this-
for i in a*; do file "$i"|grep -q 'script' && ./"$i"; done
Explanation-
for i ...
- This is a for loop which will iterate over every item that starts with a.file "$i"|grep ...
file command displays the type of the file. This is necessary because there may be other files or directories starting with 'a'. The grep looks for the word 'script' in the output of file&& ./"$i" ...
If the search by grep is successful, it executes the script.

- 1,025
-
-
-
-
-
1Perhaps
for f in a*; do file "$f" | grep -q script && ./"$f"; done
. Also note the quoting of variables. – Wildcard Jun 02 '16 at 09:00 -
@Wildcard I thought
source
and.
does the same thing. is there any difference? – Aniket Bhattacharyea Jun 02 '16 at 09:01 -
. scriptname
andsource scriptname
are the same (in bash), but./scriptname
is completely different. In the last case.
simply means "the current directory." Also see "Different ways to execute a shell script." – Wildcard Jun 02 '16 at 09:05
do ./"$each"
, or evendo "$each"
would suffice, no? – gardenhead Jun 02 '16 at 14:25