7 tips worth their weight in bash

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
droope
Posts: 801
Joined: Fri 01 Aug 2008, 00:17
Location: Uruguay, Mercedes

7 tips worth their weight in bash

#1 Post by droope »

Hi people!

Check this out

http://ad.hominem.org/log/2006/02/7_bash_tips.php

You won't regret it. :)

Regards,
Droope
What seems hard is actually easy, while what looks like impossible is in fact hard.

“Hard things take time to do. Impossible things take a little longer.â€￾ –Percy Cerutty

[url=http://droope.wordpress.com/]Mi blog[/url] (Spanish)

ken geometrics
Posts: 76
Joined: Fri 23 Jan 2009, 14:59
Location: California

Re: 7 tips worth their weight in bash

#2 Post by ken geometrics »

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.

User avatar
piratesmack
Posts: 100
Joined: Wed 16 Sep 2009, 14:22

#3 Post by piratesmack »

Thanks for posting this.
I learned a few things :)

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#4 Post by technosaurus »

Code: Select all

#/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

esac
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply