views:

24

answers:

2

I wrote a sh program which when you type in the argument which is a file name of an image the program would preview it and this can take multiple arguments (as show below)

#!/bin/sh

for i in $*; do if [ ! -f "$i" ]; then    
echo "invalid file $i"    
else    
display -size 40x50 $i &    
fi    
done

How would i be able to limit the number of arguments to 5?

Please help! Thanks

+1  A: 

You can check $# which is a count of number of command line arguments to the script and ensure that it is not more than 5.

You can do it like:

if [ $# -gt 5 ]; then
        echo '>5 arguments given..exiting'
        exit 1                                                                  
fi

# your existing script here.
codaddict
so how would i write it out like?
GuzzyD
A: 
if [ $# -gt 5 ]; then
    echo 'No more than 5 arguments are allowed'
    exit 1
fi
Alan Haggai Alavi