Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sat 25 Oct 2014, 13:38
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
7 tips worth their weight in bash
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
droope


Joined: 31 Jul 2008
Posts: 814
Location: Uruguay, Mercedes

PostPosted: Fri 20 Aug 2010, 08:16    Post_subject:  7 tips worth their weight in bash
Sub_title: true awesomeness
 

Hi people!

Check this out

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

You won't regret it. Smile

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

Mi blog (Spanish)
Back to top
View user's profile Send_private_message 
ken geometrics

Joined: 23 Jan 2009
Posts: 76
Location: California

PostPosted: Sat 21 Aug 2010, 12:12    Post_subject: Re: 7 tips worth their weight in bash
Sub_title: 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. Smile

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.
Back to top
View user's profile Send_private_message 
piratesmack


Joined: 16 Sep 2009
Posts: 100

PostPosted: Sat 21 Aug 2010, 13:24    Post_subject:  

Thanks for posting this.
I learned a few things Smile
Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4353

PostPosted: Wed 25 Aug 2010, 03:55    Post_subject:  

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

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0506s ][ Queries: 12 (0.0044s) ][ GZIP on ]