3

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.

1
  • Of course. Find runs commands and is not a shell. It apparently tried to execute the current directory, ., which is a bit futile :-) Commented May 30, 2012 at 22:47

3 Answers 3

13
for file in *.sh
do . $file
done
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.)
If the file names could contain spaces or other less-than-desirable characters, then use do . "$file" instead. The glob expansion is safe. Also, if the current directory is not included in "$PATH" (which many people recommend; there are some merits to the recommendation), then you should use do . "./$file".
0

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.

1 Comment

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

You can use find and xargs to do this:

find . -type f -name "*.sh" | xargs -I sh {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.