LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-20-2005, 06:49 AM   #1
neocookie
Member
 
Registered: Apr 2004
Location: Leeds, UK
Distribution: FC1, FC2, Debian
Posts: 308

Rep: Reputation: 30
Passing parameters to a shell script


This has probably been asked before, but I'm not sure how to search for it, so here goes:

I've written a shell script, which takes input from the user. It runs fine. However, it prompts the user for information, eg:
Code:
echo Host:
read HOST
This is fine, but I now want to use it with cron, so I need to be able to pass parameters in the following manner:
Code:
# myscript.sh --host=localhost
Can someone point me in the right direction for some documentation on how to accomplish this?

Thanks.
 
Old 10-20-2005, 07:21 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Doing "--host=localhost" is unnecessary unless you've got multiple requirements for this script so that it can take in options other than the hostname.

You can just do: myscript.sh localhost

Anything passed at the command line becomes a positional variable starting at 1. So the above is sending a variable called $1 into your script.

Since positional parameters can actually be changed depending on what you are doing the first thing you'd want to do in the script is store what was passed in into another variable that doesn't change:

HOST=$1

From then on you could get "localhost" by referencing $HOST.
 
Old 10-20-2005, 09:56 AM   #3
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,545

Rep: Reputation: 57
http://www.tldp.org/LDP/abs/html/othertypesv.html
http://www.tldp.org/LDP/abs/html/extmisc.html#GETOPTY

Some reading material for you Not too difficult to do.
 
Old 10-20-2005, 10:38 AM   #4
neocookie
Member
 
Registered: Apr 2004
Location: Leeds, UK
Distribution: FC1, FC2, Debian
Posts: 308

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by jlightner
Doing "--host=localhost" is unnecessary unless you've got multiple requirements for this script so that it can take in options other than the hostname.
Sorry, forgot to mention that there would be more than one variable being passed.

Thanks for those links. I'll check them out!
 
Old 10-20-2005, 10:53 AM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
You can do more than one variable the way I wrote it. The second one would be $2, the third would be $3 etc...

Creating options flags would be something you'd do if you want to have different choices each time. (e.g. --host to specify host or --user to specify user and invocations where you intend to sometimes specify ONLY the user or ONLY the host. If you're specifying both user and host every time then doing it without the --host and --user would be the way to go).
 
Old 10-20-2005, 11:44 AM   #6
neocookie
Member
 
Registered: Apr 2004
Location: Leeds, UK
Distribution: FC1, FC2, Debian
Posts: 308

Original Poster
Rep: Reputation: 30
Yep, I'll be using different choices each time, as the prompts at the moment can be left empty.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
passing parameters to functions in shell script kushalkoolwal Programming 1 09-28-2005 02:40 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM
passing java parameters to a unix script nephilim Programming 25 10-22-2003 10:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:44 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
Open Source Consulting | Domain Registration