LinuxQuestions.org
Visit Jeremy's Blog.
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 07-23-2015, 02:11 PM   #1
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Rep: Reputation: Disabled
script with option


Hi Linux Guru's
please spare me if this topic had landed on a wrong locaton,

I am thinking on how to make our script flexible and able to read 2 arguements, can you guide me on what is the best approach on how to add in our simple script the capability to read the arguements when it is executed.

the script name is pogi.sh
Code:
#!/bin/bash

echo "performing the install now"
ssh -qt pogi@172.16.2.2 "cd /home/pogi/scripts/bin; ./installer.pl  --rpm nrpe.1.1.10.rpm --host webserver,databaseserver"
so I'm thinking how to enable the script by executing it by this way

Code:
./pogi.sh <RPM> <HOST(S)>
so the arguments needed are for "--rpm" and "--host"
 
Old 07-23-2015, 02:14 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
$1 and $2 would be dereferenced as arguments passed into the commandline
 
Old 07-23-2015, 02:28 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,931

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
If you want to include command line options like --rpm or -r then you can use getopt. Otherwise you can use case or if then to iterate through the command line arguments.

http://bahmanm.com/blogs/command-lin...h-using-getopt
 
1 members found this post helpful.
Old 07-23-2015, 02:29 PM   #4
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
$1 and $2 would be dereferenced as arguments passed into the commandline
Hi schneidz / michaelk
can you guide me? I added a few lines on the script and seems it does not work the way I expect


Code:
#!/bin/bash
PACKAGE="$1";
NODE="$2";
echo "performing the install now"
ssh -qt pogi@172.16.2.2 "cd /home/pogi/scripts/bin; ./installer.pl  --rpm $PAKAGE --host $NODE"
 
Old 07-23-2015, 02:38 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
^ package is spelt (sic) wrong. what error do you get ?
 
1 members found this post helpful.
Old 07-23-2015, 02:52 PM   #6
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
^ package is spelt (sic) wrong. what error do you get ?
Hi schneidz,
thanks it works now, I just replaced the package/node to $1 and $2

Code:
#!/bin/bash
echo "performing the install now"
ssh -qt pogi@172.16.2.2 "cd /home/pogi/scripts/bin; ./installer.pl  --rpm $1 --host $2"
 
Old 07-24-2015, 08:59 PM   #7
JeremyBoden
Member
 
Registered: Nov 2011
Posts: 938

Rep: Reputation: 174Reputation: 174
You should (ideally) validate your parameters before using them, where possible.
 
1 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
shell script? : usage of $! with su -c option DTParanoia Linux - General 3 04-05-2005 06:58 PM
--with-x option at configure script killer_bunny Linux - Newbie 1 11-25-2004 07:51 PM
Bash script - confirmation option satimis Programming 4 10-21-2004 08:29 AM
Running script with -m option / Kylix installation pchaisty Linux - Software 1 05-27-2004 04:01 AM
script help?? want an option for init 3 or 4 on boot up?? trey85stang Slackware 1 01-04-2004 11:20 PM


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