LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-15-2015, 10:15 AM   #1
ashley75
Member
 
Registered: Aug 2003
Posts: 232

Rep: Reputation: 30
help on explaination of shell script


Hi all,

can someone please explain to me these three line?

filename=`basename $0`

if [[ $# = 1 ]] ; then
LocalHost=$1



what I am not clear is the O and 1.

thanks,
 
Old 01-15-2015, 10:23 AM   #2
J Martin Rushton
Member
 
Registered: Jan 2015
Location: England
Distribution: Mainly CentOS
Posts: 31

Rep: Reputation: Disabled
$0 is the string used to invoke the script, for instance "/home/bin/mytest" or "./test2".

$# is the number of command line arguments, so the quoted example is seeing if there is exactly one command argument and setting $LocalHost to it. The 1 is therefore just a number.
 
Old 01-15-2015, 12:02 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
$1 would be the first argument passed to the script, so for example: ./scriptname some words
$0 will be ./scriptname, $1 is some and $2 is words. And $# is 2 in this case.
but there is an error
if [[ $# -eq 1 ]]; then would be correct syntax
 
1 members found this post helpful.
Old 01-15-2015, 03:16 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by pan64 View Post
but there is an error
if [[ $# -eq 1 ]]; then would be correct syntax
So, you're saying that comparing the string "1" with the string "1" for equality is insufficient and you need to make the shell convert each string to an integer and compare the two integers, right?

Unless your numbers can potentially have different representations ("1", "01", "0x1", "0x0001", etc.), a string compare for equality is fine, and that is what is commonly done. For greater-than or less-than, not so good, though. The string "10" is less than "9".
 
Old 01-15-2015, 09:08 PM   #5
cfajohnson
LQ Newbie
 
Registered: Aug 2012
Distribution: Linux Mint 17
Posts: 22

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
$1 would be the first argument passed to the script, so for example: ./scriptname some words
$0 will be ./scriptname, $1 is some and $2 is words. And $# is 2 in this case.
but there is an error
if [[ $# -eq 1 ]]; then would be correct syntax
No, either [[ $# == 1 ]] or [ $# -eq 1 ] or (( $# == 1 )) would be correct.

Last edited by cfajohnson; 01-16-2015 at 01:20 AM.
 
Old 01-16-2015, 12:19 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by rknichols View Post
So, you're saying that comparing the string "1" with the string "1" for equality is insufficient and you need to make the shell convert each string to an integer and compare the two integers, right?
No.
I tried to say:
if [[ $# = 1 ]] ; then
is not ok (as in the original post only one = was used). From the other hand using numerical comparison looks better for numbers, but you are right, actually it is completely irrelevant (compare them as number and/or strings is the same right now)
 
Old 01-16-2015, 02:38 AM   #7
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware
Posts: 441

Rep: Reputation: 574Reputation: 574Reputation: 574Reputation: 574Reputation: 574Reputation: 574
Quote:
Originally Posted by pan64
No.
I tried to say:
if [[ $# = 1 ]] ; then
is not ok (as in the original post only one = was used).
You can indeed use one single "=" as well. In fact, "=" and "==" are synonymous, at least in bash. But "=" is recommended for POSIX conformance.

Code:
string comparison

=
    is equal to
    if [ "$a" = "$b" ]

==
    is equal to
    if [ "$a" == "$b" ]

    This is a synonym for =

(ABS Guide: Comparison operators)
Code:
string1 == string2
string1 = string2

       True if the strings are equal.  = should be used with the test
       command for POSIX conformance.

(man bash: Conditional Expressions)

Last edited by Philip Lacroix; 01-16-2015 at 02:49 AM. Reason: fixed link
 
Old 01-16-2015, 02:48 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
ok, thanks, so that is only valid in c.
 
Old 01-16-2015, 03:12 AM   #9
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
AFAIR, the reasoning is slightly different.

http://www.gnu.org/software/bash/man...al-Expressions

The -eq format is used for numeric comparisons and the = or ==for string comparisons.

OK

Last edited by AnanthaP; 01-16-2015 at 03:16 AM.
 
  


Reply



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
Shell script for run an shell script on server using ssh bloodstreetboy Linux - Server 5 01-12-2013 03:23 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
script explaination maooah Programming 4 06-01-2008 01:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:54 PM.

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
Open Source Consulting | Domain Registration