How can I make a script take multiple arguments?
Using the same structure as your original script, you just need to iterate over the $@
array (that's the list of arguments given in the command line):
#!/usr/local/bin/bash
set -e
if [ "$#" -lt 1 ]
then
echo "Please insert at least one argument"
exit
else
echo -e "\c"
fi
for file in "$@"
do
if [ -h "$file" ]
then
echo "$file is a symbolic link"
else
echo "$file is not a symbolic link"
fi
done
A simplified version of the same thing would be:
#!/usr/bin/env bash
[ "$#" -lt 1 ] && printf "Please give at least one argument\n" && exit
for file
do
[ -h "$file" ] && printf "%s is a symbolic link\n" "$file" ||
printf "%s is not a symbolic link\n" "$file"
done
No one mentioned shift?
if [ x = "x$1" ] ; then
echo need at least one file
exit 1
fi
while [ x != "x$1" ] ; do
if [ -h "$1" ]; then
echo "$1 is a symbolic link"
else
echo "$1 is not a symbolic link"
fi
shift
done
You can use a for loop to process all files passed to script:
for f do
if [ -h "$f" ]; then
printf "%s is a symbolic link\n" "$f"
else
printf "%s is not a symbolic link\n" "$f"
fi
done