This is an old revision of the document!
Table of Contents
Shell
Block comment
With “END” being just an example of any random string that must not appear in the commented section:
: << 'END'
sleep 1
END
Iterate over a list as a string
If you have to define it as a string, sh
will work, and for zsh
you have to execute before setopts shwordsplit
:
setopts shwordsplit list="a b c" for x in $list; do echo $x; done
However if you can, it is better to define arrays:
list=(a b c) for x in $list; do echo $x; done
Do something recursively on all files
Use find
, with {}
being the file name:
find . -type f -exec <command> {} \; find -iname "<pattern>" -exec <command> {} \;
Manual solution:
#!/bin/sh function search_dir() { DIR=$1; for file in $DIR/*; do if [[ -f $file ]]; then echo "Do something with $file" fi; done for element in $DIR/* ; do if [[ -e $element && -d $element && \ $(basename "$element") != ".." && \ $(basename "$element") != "." ]]; then search_dir "$element"; fi; done; } search_dir "."