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
Default value for variable
${<variable>:-<default-value>}
Iterate over a list as a string
Different options:
# option 1, without variable for x in a b c; do echo $x; done # option 2, with string list="a b c" for x in $list; do echo $x; done # only works with sh and bash for x in ${=list}; do echo $x; done # only works with zsh for x in $(echo $list}; do echo $x; done # works with any shell # option 3, with an array 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 "."