2013年1月4日 星期五

[Shell] tips

set
In shell scripts set -e is often used to make them more robust by stopping the script when some of the commands executed from the script exits with non-zero exit code.

if then else

if [ -d $directory ]; then
 echo "Directory exists"
else 
 echo "Directory does not exists"
fi 

Please note the spacing inside the [ and ] brackets! Without the spaces, it won't work!

${PWD##*/}
get leaf directory of pwd. Eg.
 > echo $PWD
/home/wilsonhong
> echo {$PWD##*/}
wilsonhong

-d
if [ -d "$DIRECTORY" ] 
check directory exist or not 

gnome-terminal -e "<cmd>"
run command interminal


find . -iname "*.pyc" -exec rm -f {} \;
delete all .pyc files 

For each result, command {} is executed. All occurences of {} are replaced by the filename. ; is prefixed with a slash to prevent the shell from interpreting it.


沒有留言:

張貼留言