LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-07-2011, 11:54 PM   #1
casttree
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Rep: Reputation: 0
wrapper for unix command with multiple parameters


I hope to add a wrapper script for the command with different parameters

for example , for any Unix command or script ,like blow
command.sh -s p1 -o p2 -q p3
or
command.sh

Probably we could do as this way
cat wrapper

$1 $2 $3 $4 $5 $6 $7 | tee test.log
(assume it has 0 to 6 parameters)

and use it like


wrapper command.sh -s p1 -o p2 -q p3
wrapper command.sh

It is a little ugly to list all fixed parameter as above, do we have better code to handle various paramters ?

Thanks
 
Old 01-08-2011, 05:01 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by casttree View Post
I hope to add a wrapper script for the command with different parameters
Which command?

Quote:
Originally Posted by casttree View Post
for example , for any Unix command or script ,like blow
command.sh -s p1 -o p2 -q p3
or
command.sh

Probably we could do as this way
cat wrapper

$1 $2 $3 $4 $5 $6 $7 | tee test.log
(assume it has 0 to 6 parameters)

and use it like


wrapper command.sh -s p1 -o p2 -q p3
wrapper command.sh

It is a little ugly to list all fixed parameter as above, do we have better code to handle various paramters ?
Blow? What is that?

It isn't clear what you are talking about.

I believe that you will be required to parse the argument list, whatever you have in mind. Since command parameters are (generally) allowed to be entered in any order then you would have to check each given parameter against all of the possible parameters for whatever command you are wrapping.

I think you would need to provide more details before anyone could provide you with good information.
 
Old 01-08-2011, 08:13 PM   #3
casttree
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Sorry I should specify it as wrapper for script, instead of command.

I need to use it for a script we used. The script could have different parameters , and no order is required. It is possible that we can run the command with no parameters or 2 , 4, 6 parameters (like -s , -o , -p, etc).

Like :

copyPkg.sh -s /home/src -d /home/dest -o optionalPara
If the script doesn't specify teh paramter, it could use default value.

like
copyPkg.sh

Since the original script is created by other group, I don't want to change the original one, but rather to add a wrapper to add additional function, like logging, clean data etc. and the wrapper need to handle all existing parameters of original scripts.

wrapper.sh copyPkg.sh
or wrapper.sh copyPkg.sh -s /home/src -d /home/dst


The simple way like below could work to add the logging function, but may not be a best way.
>cat wrapper.sh
$1 $2 $3 $4 $5 $6 $7 | tee test.log

(assume it has 0 to 6 parameters)



I am wondering if there is a general way to handle the different number of parameters.

Thanks

Last edited by casttree; 01-08-2011 at 08:26 PM.
 
Old 01-08-2011, 09:51 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
In bash either $* or $@ (unquoted) expand to all the arguments given on the command line:
$1 $2 $3 $4 $5 ............

"$@" (with the quotes) expands to the arguments individually quoted:
"$1" "$2" "$3" "$4" "$5" ............

"$*" (with the quotes) expands to the arguments quoted as a single string:
"$1 $2 $3 $4 $5 ............"

Try
>cat wrapper.sh
"$@" | tee test.log
 
Old 01-09-2011, 09:51 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
IIUC, your original script has a variable # (1-6) of arguments, how do you intend that the wrapper knows which are meant for it when there are less than 6? Or is the wrapper only ever going to get a single argument (the last)?
 
  


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
What is the unix command to trim multiple spaces into single space? manohare Linux - General 4 03-02-2009 07:25 AM
linux command wrapper linux_guy Linux - Software 3 11-21-2007 11:01 AM
UNIX shell script: split long command on multiple lines loopoo Linux - Newbie 2 10-23-2006 09:34 AM
What is a command wrapper? johnsanty Programming 5 09-22-2006 02:19 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 09:23 PM.

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