Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take the first command line argument and pass the rest

Tags:

bash

Example:

check_prog hostname.com /bin/check_awesome -c 10 -w 13

check_remote -H $HOSTNAME -C "$ARGS" #To be expanded as check_remote -H hostname.com -C "/bin/check_awesome -c 10 -w 13" 

I hope the above makes sense. The arguments will change as I will be using this for about 20+ commands. Its a odd method of wrapping a program, but it's to work around a few issues with a few systems we are using here (gotta love code from the 70s).

The above could be written in Perl or Python, but Bash would be the preferred method.

like image 749
user554005 Avatar asked May 13 '12 03:05

user554005


People also ask

How do you pass a command line argument to a function in shell script?

To invoke a function, simply use the function name as a command. To pass parameters to the function, add space separated arguments like other commands. The passed parameters can be accessed inside the function using the standard positional variables i.e. $0, $1, $2, $3 etc.

How do you pass a command line argument?

If you want to pass command line arguments then you will have to define the main() function with two arguments. The first argument defines the number of command line arguments and the second argument is the list of command line arguments.


1 Answers

You can use shift

shift is a shell builtin that operates on the positional parameters. Each time you invoke shift, it "shifts" all the positional parameters down by one. $2 becomes $1, $3 becomes $2, $4 becomes $3, and so on

example:

$ function foo() { echo $@; shift; echo $@; }  $ foo 1 2 3 1 2 3 2 3 
like image 53
ravi Avatar answered Oct 14 '22 06:10

ravi