views:

208

answers:

2

Hello,
when I want to execute some shell script in Unix (and let's say that I am in the directory where the script is), I just type:

./someShellScript.sh

and when I want to "source" it (e.g. run it in the current shell, NOT in a new shell), I just type the same command just with the "." (or with the "source" command equivalent) before it:

. ./someShellScript.sh


And now the tricky part. When I want to execute "multiple" shell scripts (let's say all the files with .sh suffix) in the current directory, I type:

find . -type f -name *.sh -exec {} \;

but "what command should I use to "SOURCE" multiple shell scripts in a directory"?
I tried this so far but it DIDN'T work:

find . -type f -name *.sh -exec . {} \;

and it only threw this error:

find: `.': Permission denied


Thanks.

+6  A: 
for file in *.sh
do . $file
done
Jonathan Leffler
Thank you. But what I have had also some "directories ending to ".sh" " in the current directory. The script will try to run all the files BUT also "all the directories", won't it? Isn't there any restriction only to files?
Petike
ephemient
Thanks. I am so silly :-) .
Petike
Yes, as written it would try to run directories with names ending '.sh'. Question: what on earth made you give a directory the suffix '.sh'? (And if you'd observed in the question that there was a mix of files and directories with the suffix '.sh', then the accurate answer - as given by Ephemient - could have been given up front.)
Jonathan Leffler
A: 

Try the following version of Jonathan's code:

export IFSbak = $IFS;export IFS="\0"
for file in `find . -type f -name '*.sh' -print0`
do source "$file"
end
export IFS=$IFSbak

The problem lies in the way shell's work, and that '.' itself is not a command (neither is source in this). When you run find, the shell will fork itself and then turn into the find process, meaning that any environment variables or other environmental stuff goes into the find process (or, more likely, find forks itself for new processes for each exec).

Also, note that your command (and Jonathan's) will not work if there are spaces in the file names.

sargas
using find is unnecessary since he is doing it in the current directory. also have to take care of spaces in file names.
ghostdog74