LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2011, 04:33 AM   #1
jack_green
Member
 
Registered: Jul 2011
Posts: 73

Rep: Reputation: Disabled
help me with bash shell script


hello , friends. i am new to bash shell script. i have no idea about the thing about this "" () [] .

for example: [ "$string" == "test" ] [ $number -eq 0 ] but sometime (( ("%year" % 4) == "0" )) . It seems that all of them works.

ok, now i am not sure when should i use [ string -eq string ] or [ digit -eq digit ], when use [ == ]
and (( )). something like that
please help me..
 
Old 11-23-2011, 04:49 AM   #2
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL, USA
Distribution: Fedora, Gentoo, Ubuntu (all servers at work are openSUSE)
Posts: 6,931

Rep: Reputation: 128Reputation: 128
This Bash Guide is the best read on the net.

Hope that helps.
 
Old 11-23-2011, 11:26 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The link given gives you all you need to know. But to boil it down:

[...] The traditional test command, [ is actually a synonym for "test". Portable across shells and systems, but suffers from syntactical weaknesses.

[[...]] The "new" test keyword. First implemented in ksh, and also adopted by bash. It overcomes a lot of the problems with the old test, and adds some new features like pattern-matching with globs and regex. Everything that the original does, this one can do too, and often better. But it's not portable.

((...)) Arithmetic evaluation. Supported in both bash and ksh these days. Since it outputs an exit code, it can be used in place of one of the above tests when the operation involves numbers. But remember, the shell does integer math only.

So in a nutshell, use ((...)) for arithmetic comparisons and [[...]] for string and complex comparisons. Only use [...] if you truly need posix-style compatibility, which you probably don't. But do learn how to use it, for those times when you come across it.

Edit: by the way, be very careful to recognize the difference between a string and a numeric comparison. Try these two commands, for example:

Code:
[[ 99 > 100 ]] && echo "dat's true" || echo "say what?"

[[ 99 -gt 100 ]] && echo "dat's true" || echo "say what?"

Last edited by David the H.; 11-23-2011 at 11:35 AM.
 
1 members found this post helpful.
Old 11-23-2011, 07:47 PM   #4
jack_green
Member
 
Registered: Jul 2011
Posts: 73

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
The link given gives you all you need to know. But to boil it down:

[...] The traditional test command, [ is actually a synonym for "test". Portable across shells and systems, but suffers from syntactical weaknesses.

[[...]] The "new" test keyword. First implemented in ksh, and also adopted by bash. It overcomes a lot of the problems with the old test, and adds some new features like pattern-matching with globs and regex. Everything that the original does, this one can do too, and often better. But it's not portable.

((...)) Arithmetic evaluation. Supported in both bash and ksh these days. Since it outputs an exit code, it can be used in place of one of the above tests when the operation involves numbers. But remember, the shell does integer math only.

So in a nutshell, use ((...)) for arithmetic comparisons and [[...]] for string and complex comparisons. Only use [...] if you truly need posix-style compatibility, which you probably don't. But do learn how to use it, for those times when you come across it.

Edit: by the way, be very careful to recognize the difference between a string and a numeric comparison. Try these two commands, for example:

Code:
[[ 99 > 100 ]] && echo "dat's true" || echo "say what?"

[[ 99 -gt 100 ]] && echo "dat's true" || echo "say what?"


thanks so much
 
Old 11-23-2011, 07:48 PM   #5
jack_green
Member
 
Registered: Jul 2011
Posts: 73

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Bruce Hill View Post
This Bash Guide is the best read on the net.

Hope that helps.
sorry , i am not asking tutorial.
anyway thanks all the same.
 
Old 11-23-2011, 08:36 PM   #6
corbintechboy
Member
 
Registered: Sep 2003
Location: Kentucky
Posts: 480
Blog Entries: 1

Rep: Reputation: 51
Quote:
Originally Posted by jack_green View Post
sorry , i am not asking tutorial.
anyway thanks all the same.
Who would want to learn anyway?
 
Old 11-23-2011, 11:30 PM   #7
jack_green
Member
 
Registered: Jul 2011
Posts: 73

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by corbintechboy View Post
Who would want to learn anyway?
yeah, i wanna learn bash shell script , but i already have bash shell script guide . and i posted the thread here casue i didnot understand at that moments and wanna somebody who can tell me clearly. and "David the H" who did it for me .
 
Old 09-25-2017, 07:50 AM   #8
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL, USA
Distribution: Fedora, Gentoo, Ubuntu (all servers at work are openSUSE)
Posts: 6,931

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by Bruce Hill View Post
This Bash Guide is the best read on the net.

Hope that helps.
There is The Bash Guide for those who are interested.
It's different than the other guide; complementary, and for a different learning style.
 
Old 09-25-2017, 10:13 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,768

Rep: Reputation: 2887Reputation: 2887Reputation: 2887Reputation: 2887Reputation: 2887Reputation: 2887Reputation: 2887Reputation: 2887Reputation: 2887Reputation: 2887Reputation: 2887
Quote:
Originally Posted by jack_green View Post
sorry , i am not asking tutorial.
anyway thanks all the same.
just check the one of the first links on that page (Special Characters) and you will find an answer.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a bash shell script from within another script dedman Linux - Software 7 04-24-2010 09:53 PM
in bash shell how to run shell script during startup rammohan04 Red Hat 2 07-31-2009 03:07 AM
help with bash shell script !! taiwf Linux - Newbie 5 06-11-2006 07:07 PM
BASH shell script help ewarmour Programming 8 05-24-2002 08:57 AM
bash shell script MaryM Linux - Newbie 0 02-16-2002 12:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration