LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 10-04-2012, 04:48 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 30
What is the function of this script


I have a ftp script that used to transfer files , the beginning of the script is as below , what is the function of it ? thanks.


if [ $# != "1" ]
then
echo "Usage: $0 host_name"
exit 1
fi
 
Old 10-04-2012, 04:58 AM   #2
ToK
Member
 
Registered: Nov 2005
Posts: 108

Rep: Reputation: 17
Hi Ust

#$ contains the number of passed argument, so the first line checks, if on argument is passed on the command line. By the way the Brackets [ and ] call the command "test". You could also write $( test $# != "1" ). Check the manual page of "test" for more info.
$0 contains the command itself, so if more or less than one argument is passed to the command, a short message is display which says you should enter the command and the hostname (to connect to).

ToK
 
1 members found this post helpful.
Old 10-04-2012, 05:01 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
It's an error trap for the script not being called with one and only one argument. The error message shows that the mandatory agrument is a host name.

Code:
if [ $# != "1" ]  <= if the number of arguments is not equal to (string!) 1 (better as numeric comparison using -ne instead of !=)
then
    echo "Usage: $0 host_name"  <= $0 is the name by which the script was called
   exit 1 <= Exit, setting a non-zero return code to indicate an error
fi
EDIT: beaten to it by ToK !

Last edited by catkin; 10-04-2012 at 05:02 AM.
 
1 members found this post helpful.
Old 10-04-2012, 05:29 AM   #4
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by catkin View Post
It's an error trap for the script not being called with one and only one argument. The error message shows that the mandatory agrument is a host name.

Code:
if [ $# != "1" ]  <= if the number of arguments is not equal to (string!) 1 (better as numeric comparison using -ne instead of !=)
then
    echo "Usage: $0 host_name"  <= $0 is the name by which the script was called
   exit 1 <= Exit, setting a non-zero return code to indicate an error
fi
EDIT: beaten to it by ToK !
thanks,

sorry to my fool , argument means parameter ?

the script is run by this run
#my_script 192.168.0.1

in my case , 192.168.0.1 is the argument ?

thanks.
 
Old 10-04-2012, 05:37 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Yes, you are correct, the command line arguments become the positional parameters in a bash script. So yes, 192.168.0.1 is the argument and the first positional parameter in your case.
 
Old 10-04-2012, 05:46 AM   #6
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
another question what is the difference of "exit 1" and "exit" , "exit 1" can export the error message ?

Thanks.
 
Old 10-04-2012, 05:56 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
No, exit returns the exit code of the last cmd run or the num you specify.
In either case, it must be a value in the range 0 - 255.
 
2 members found this post helpful.
  


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
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 10:06 PM
[SOLVED] Help with function in script Tiago85 Linux - Newbie 12 09-29-2012 01:46 PM
how to call a function from another script smritisingh03 Linux - Newbie 2 01-05-2011 07:29 PM
How to cd in a script function? subhrajyotibal Linux - Newbie 2 04-25-2008 02:52 AM
return value of function in script ramesh_manu Red Hat 1 02-18-2007 02:05 PM


All times are GMT -5. The time now is 05:26 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