How to pass command line argument in shell script? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How to pass command line argument in shell script?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-29-2013
Nsharma3006 Nsharma3006 is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 31 August 2013, 8:44 AM EDT
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
How to pass command line argument in shell script?

I need to write a shell script, when I run that script I should pass those arguments if not, then script should not run and pass the error message like

Code:
invalid option - - should pass the argument.

and Exit from the script
Sponsored Links
    #2  
Old 08-29-2013
vbe's Avatar
vbe vbe is online now Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 21 October 2014, 3:26 PM EDT
Location: Switzerland - GE
Posts: 5,833
Thanks: 162
Thanked 414 Times in 389 Posts
What have you done so far?
What issue did you encounter?
Sponsored Links
    #3  
Old 08-29-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 October 2014, 9:27 AM EDT
Location: Aachen, Germany
Posts: 4,406
Thanks: 73
Thanked 1,076 Times in 1,015 Posts
Search these fora - there's many an example of exactly your request.
    #4  
Old 08-29-2013
Nsharma3006 Nsharma3006 is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 31 August 2013, 8:44 AM EDT
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
I have not started yet because I dont know how to start for writing command line argument.

I am searching if I can find the code for the same.

---------- Post updated at 08:37 PM ---------- Previous update was at 04:07 PM ----------

Can anyone help how to write I m new to script writing

---------- Post updated at 09:36 PM ---------- Previous update was at 08:37 PM ----------

I need to run a script like

Code:
./script.ksh 01/02

if I run just like this

Code:
./script.ksh

then It should thru error message like invalid option and exit from the script
Sponsored Links
    #5  
Old 08-30-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 October 2014, 2:22 PM EDT
Location: San Jose, CA, USA
Posts: 4,881
Thanks: 182
Thanked 1,641 Times in 1,392 Posts
Quote:
Originally Posted by Nsharma3006 View Post
I have not started yet because I dont know how to start for writing command line argument.

I am searching if I can find the code for the same.

---------- Post updated at 08:37 PM ---------- Previous update was at 04:07 PM ----------

Can anyone help how to write I m new to script writing

---------- Post updated at 09:36 PM ---------- Previous update was at 08:37 PM ----------

I need to run a script like

Code:
./script.ksh 01/02

if I run just like this

Code:
./script.ksh

then It should thru error message like invalid option and exit from the script
You're probably not getting much help here because this sounds more like a homework item placed in the wrong forum and not using the correct format for a homework assignment.

If you really are trying to learn how to write shell programs on your own, we need more details to understand what you're trying to do.

Options are generally introduced by a leading minus sign (or less commonly, a leading plus sign), your example doesn't include either one.

An error message like "invalid option" implies that you are processing options (rather than operands) and that there are "valid" and "invalid" options. What determines whether an option is valid or invalid?
Sponsored Links
    #6  
Old 08-30-2013
vbe's Avatar
vbe vbe is online now Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 21 October 2014, 3:26 PM EDT
Location: Switzerland - GE
Posts: 5,833
Thanks: 162
Thanked 414 Times in 389 Posts
If you looked at the forums, you would have found tutorilas:
http://www.unix.com/answers-frequent...tutorials.html
which would have lead you e.g. here:
Bourne Shell Tutorial


We are here to help/assist you, not for doing the work for you...
So clue: you will have to use $# to test if any arguments were passed...
Sponsored Links
    #7  
Old 08-30-2013
Nsharma3006 Nsharma3006 is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 31 August 2013, 8:44 AM EDT
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
I like to run my script

Code:
./script.ksh 01

and like to read this 01 value in the script inside and use it for further logic.

I tried writing like this

Code:
#!/bin/sh
$#
echo $#
read $#
exit

its not working as I desired
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to pass the command line arguments to the shell script in c language? sharlin Programming 2 05-02-2012 05:15 AM
How to pass a filename as a command line argument ektubbe Shell Programming and Scripting 1 05-01-2012 03:25 AM
How we can pass the argument when calling shell script from perl script anuragpgtgerman Shell Programming and Scripting 5 03-21-2012 03:57 PM
shell script for ftp files passed in command line argument rateeshkumar Shell Programming and Scripting 5 03-23-2011 10:27 PM
Pass command line argument to variable Poonamol Shell Programming and Scripting 10 09-28-2010 01:49 AM



All times are GMT -4. The time now is 03:55 PM.