I'm new to bash and want to write a simple program which takes an even amount of parameters. The script should always take two parameters and print them in one line as first an last name. A usage of the script could be:
getnames John Doe Max Muster Tim Stone
The output should be:
1. Person: John Doe
2. Person: Max Muster
3. Person: Tim Stone
The output I get right now:
::> getnames John Doe Max Muster Tim Stone
/home/user/bin/getnames: line 17: x: command not found
1. Person: John
/home/user/bin/getnames: line 17: x: command not found
2. Person: Doe
/home/user/bin/getnames: line 17: x: command not found
3. Person: Max
/home/user/bin/getnames: line 17: x: command not found
4. Person: Muster
/home/user/bin/getnames: line 17: x: command not found
5. Person: Tim
/home/user/bin/getnames: line 17: x: command not found
6. Person: Stone
Here is the code I've written so far:
#!/bin/bash
if [ $# -eq 0 ]; then
echo "No names given"
exit 1
elif [ $# -lt 2 ]; then
echo "Please enter at least one first and one last name"
exit 1
elif [ $(($#%2)) -ne 0 ]; then
echo "Please always enter a first name and a last name for one person"
exit 1
fi
count=1
for x in $*; do
echo "$count. Person: $x $(x + 1)"
let "x+=2"
let count++
done
evalto add a level of indirection toxbut that is just not a good way to do it. It's also much more limited thanshift.