1

I'm pretty new to shell scripts, so it confuses me a little and I couldn't seem to find a solution to this. Let's say I have a shell script that can take a number of arguments. For the sake of example, I could call it as follow:

myscript -a valA -b valB -c valC -d valD some/directory

Now, some of these arguments are for my script itself, while others are for a command that will be called within my script. So for this case, -a, -d and the directory are for my script, everything else will be for the command. So I want to do something like this:

args=''

if [ $# == 0 ]
then
    echo "No arguments found!"
    exit 1
fi

while [ "$2" ]
do
    if [ $1 == '-a' ]
    then
        #some process here
        shift 2
    elif [ $1 == '-d' ]
    then
        #some process here
        shift 2
    else
        #add the argument to args
        shift
    fi
done
directory=$1

for file in $directory/*.txt
do 
    #call 'someCommand' here with arguments stored in args + $file
done

I've tried doing

args="$args $1"

Then calling the command doing

someCommand "$args $file"

but then, someCommand seems to think the whole thing is one single argument.

Also, if you see anything wrong with the rest of my script, feel free to point it out. It seems to work, but I could very well be missing some corner cases or doing things that may lead to unexpected behaviors.

Thanks!

2
  • Don't bother with the if [ $# == 0 ] clause. Instead, just write: directory=${1?No directory specified} when you make the assignment to directory. Commented Sep 7, 2012 at 17:44
  • Replace the if/elif/else chain with a case statement. Commented Sep 7, 2012 at 17:46

3 Answers 3

1

Use an array.

newargs=()
newargs+="-9"
newargs+="$somepid"
kill "${newargs[@]}"
Sign up to request clarification or add additional context in comments.

Comments

1

Just remove the quotes:

someCommand $args "$file"

Comments

0

Use set:

From the set man page: Without options, the name and value of each shell variable are displayed in a format that can be reused as input. The output is sorted according to the current locale. When options are specified, they set or unset shell attributes. Any arguments remaining after the options are processed are treated as values for the positional parameters and are assigned, in order, to $1, $2, ... $n.

args=''

if [ $# == 0 ]
then
    echo "No arguments found!"
    exit 1
fi

while [ "$2" ]
do
    if [ $1 == '-a' ]
    then
        echo "Consuming $1 $2"
        shift 2
    elif [ $1 == '-d' ]
    then
        echo "Consuming $1 $2"
        shift 2
    else
        args="$args $1"
        shift
    fi
done
directory=$1

# *** call set here ***
set "$args"

for file in $directory/*.txt
do 
    # refer to the parameters using $@
    someCommand $@ $file
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.