Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux shell append variable parameters to command

I am trying to get a bash script that generates JSDoc for given parameters like this

./jsdoc.sh file.js another.js maybe-a-third.js

I am getting stuck on how to pass an unknown quantity of parameters to the next shell command.

(also, don't know how to check if param exists, only if not exitst if [ -z ... ])

This code works for up to two parameters, but obviously not the right way to go about it...

#!/bin/bash

# would like to know how to do positive check
if [ -z "$1" ]
then echo no param
else
        d=$PWD
        cd ~/projects/jsdoc-toolkit/

        # this bit is obviously not the right approach
        if [ -z "$2" ]
        then java -jar jsrun.jar app/run.js -a -t=templates/jsdoc/ $d/$1
        else java -jar jsrun.jar app/run.js -a -t=templates/jsdoc/ $d/$1 $d/$2
        fi

        cp -R out/jsdoc $d
fi

Any other pointers of how I could achieve this would be appreciated.

Edit: Updated script according to @skjaidev's answer - happy days ;)

#!/bin/bash

d=$PWD

for i in $*; do
    params=" $params $d/$i"
done

if [ -n "$1" ]; then
        cd ~/projects/jsdoc-toolkit/
        java -jar jsrun.jar app/run.js -a -t=templates/jsdoc/ $params
        cp -R out/jsdoc $d
fi
like image 534
Billy Moon Avatar asked Aug 05 '11 21:08

Billy Moon


1 Answers

$* has all the parameters. You could iterate over them

for i in $*;
do
    params=" $params $d/$i"
done
your_cmd $params
like image 183
jman Avatar answered Sep 27 '22 19:09

jman