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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-20-2005, 06:49 AM
|
#1
|
Member
Registered: Apr 2004
Location: Leeds, UK
Distribution: FC1, FC2, Debian
Posts: 308
Rep:
|
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.
|
|
|
10-20-2005, 07:21 AM
|
#2
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
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.
|
|
|
10-20-2005, 09:56 AM
|
#3
|
Senior Member
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,545
Rep:
|
|
|
|
10-20-2005, 10:38 AM
|
#4
|
Member
Registered: Apr 2004
Location: Leeds, UK
Distribution: FC1, FC2, Debian
Posts: 308
Original Poster
Rep:
|
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!
|
|
|
10-20-2005, 10:53 AM
|
#5
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
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).
|
|
|
10-20-2005, 11:44 AM
|
#6
|
Member
Registered: Apr 2004
Location: Leeds, UK
Distribution: FC1, FC2, Debian
Posts: 308
Original Poster
Rep:
|
Yep, I'll be using different choices each time, as the prompts at the moment can be left empty.
|
|
|
All times are GMT -5. The time now is 06:44 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|