LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help me with bash shell script (https://www.linuxquestions.org/questions/programming-9/help-me-with-bash-shell-script-915052/)

jack_green 11-23-2011 04:33 AM

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..

Bruce Hill 11-23-2011 04:49 AM

This Bash Guide is the best read on the net.

Hope that helps.

David the H. 11-23-2011 11:26 AM

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?"


jack_green 11-23-2011 07:47 PM

Quote:

Originally Posted by David the H. (Post 4531828)
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

jack_green 11-23-2011 07:48 PM

Quote:

Originally Posted by Bruce Hill (Post 4531437)
This Bash Guide is the best read on the net.

Hope that helps.

sorry , i am not asking tutorial.
anyway thanks all the same.

corbintechboy 11-23-2011 08:36 PM

Quote:

Originally Posted by jack_green (Post 4532244)
sorry , i am not asking tutorial.
anyway thanks all the same.

Who would want to learn anyway? :rolleyes:

jack_green 11-23-2011 11:30 PM

Quote:

Originally Posted by corbintechboy (Post 4532280)
Who would want to learn anyway? :rolleyes:

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 .

Bruce Hill 09-25-2017 07:50 AM

Quote:

Originally Posted by Bruce Hill (Post 4531437)
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.

pan64 09-25-2017 10:13 AM

Quote:

Originally Posted by jack_green (Post 4532244)
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.


All times are GMT -5. The time now is 06:33 PM.