LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   wrapper for unix command with multiple parameters (https://www.linuxquestions.org/questions/linux-general-1/wrapper-for-unix-command-with-multiple-parameters-854939/)

casttree 01-07-2011 11:54 PM

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

stress_junkie 01-08-2011 05:01 PM

Quote:

Originally Posted by casttree (Post 4217060)
I hope to add a wrapper script for the command with different parameters

Which command?

Quote:

Originally Posted by casttree (Post 4217060)
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.

casttree 01-08-2011 08:13 PM

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

Kenhelm 01-08-2011 09:51 PM

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

archtoad6 01-09-2011 09:51 AM

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)?


All times are GMT -5. The time now is 12:16 PM.