ken geometrics
Joined: 23 Jan 2009 Posts: 76 Location: California
Posted: Sat 21 Aug 2010, 12:12 Post subject:
Re: 7 tips worth their weight in bash Subject description: true awesomeness
droope wrote:
Hi people!
Check this out
http://ad.hominem.org/log/2006/02/7_bash_tips.php
You won't regret it.
Regards,
Droope
On the find commands you need to put quotes around the wild carded file names. If you don't, bash may find a file that matches the name in the current directory.
#/bin/sh
echo this message will self destruct in 5 seconds
sleep 5
rm -f $0
works great for a firstrun script or anything that should only be done once
here are some more:
Command Description Example
& Run the previous command in the background ls &
&& Logical AND if [ "$foo" -ge "0" ] && [ "$foo" -le "9"]
|| Logical OR if [ "$foo" -lt "0" ] || [ "$foo" -gt "9" ] (not in Bourne shell)
^ Start of line grep "^foo"
$ End of line grep "foo$"
= String equality (cf. -eq) if [ "$foo" = "bar" ]
! Logical NOT if [ "$foo" != "bar" ]
$$ PID of current shell echo "my PID = $$"
$! PID of last background command ls & echo "PID of ls = $!"
$? exit status of last command ls ; echo "ls returned code $?"
$0 Name of current command (as called) echo "I am $0"
$1 Name of current command's first parameter echo "My first argument is $1"
$9 Name of current command's ninth parameter echo "My ninth argument is $9"
$@ All of current command's parameters (preserving whitespace and quoting) echo "My arguments are $@"
$* All of current command's parameters (not preserving whitespace and quoting) echo "My arguments are $*"
-eq Numeric Equality if [ "$foo" -eq "9" ]
-ne Numeric Inquality if [ "$foo" -ne "9" ]
-lt Less Than if [ "$foo" -lt "9" ]
-le Less Than or Equal if [ "$foo" -le "9" ]
-gt Greater Than if [ "$foo" -gt "9" ]
-ge Greater Than or Equal if [ "$foo" -ge "9" ]
-z String is zero length if [ -z "$foo" ]
-n String is not zero length if [ -n "$foo" ]
-nt Newer Than if [ "$file1" -nt "$file2" ]
-d Is a Directory if [ -d /bin ]
-f Is a File if [ -f /bin/ls ]
-r Is a readable file if [ -r /bin/ls ]
-w Is a writable file if [ -w /bin/ls ]
-x Is an executable file if [ -x /bin/ls ]
parenthesis:
( ... ) Function definition function myfunc() { echo hello }
the && || combo is great for replacing a hierachy type of nested ifs
[ a -lt 5 ] && command1 || [ a -lt 10 ] && command2 || [ a -lt 22 ] && command3 || echo "a is greater than or equal to 22 - bust" && exit
putting that in a properly formatted if then else setup would be 10 lines and difficult to read, but case is a little better if you use regular expressions
case a in
[0-4])cmd1;;
[5-9])cmd2;;
1[0-9]|2[0,1])cmd3;;
*)echo bust;;
#install all rpms in the current directory (good template for other tasks though)
for x in `ls ./*rpm`; do rpm2cpio $x | cpio -ivd; done
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum