A script that picks out its first command line argument into a separate variable called op
and then removes this from the list of command line arguments:
#!/bin/sh
op=$1
shift
If this script is called with add 1 2 3
as arguments, then the code would assign the string add
to op
and would leave 1 2 3
as the only three command line arguments in "$@"
.
We may then loop over the remaining command line arguments:
#!/bin/sh
op=$1
shift
for arg do
# some commands should go here
done
The added loop will make sure that the variable arg
will take the value of each of the remaining command line arguments in turn. We could also have written the start of the loop like for arg in "$@"; do ...
but it's more to type and programmers sometimes forget to add the quotes around $@
.
Depending on the value $op
one of several operations may happen in the loop. We may use a simple test to see what $op
is and carry out the correct operation. In any case, some value is being accumulated in acc
below, and this acc
value should (according to the way the commands are presented in the question) be initialized to the first number, and that number needs to be shifted off the list of arguments too.
#!/bin/sh
op=$1
shift
acc=$1
shift
for arg do
case $op in
add) acc=$(( acc + arg )) ;;
sub) acc=$(( acc - arg )) ;;
mul) acc=$(( acc * arg )) ;;
div) acc=$(( acc / arg )) ;; # note: integer division here
*)
printf 'Unknown operation: %s\n' "$op" >&2
exit 1
esac
done
printf '%s\n' "$acc"
Testing this script after saving it and making it executable:
$ ./script add 1 2 3
6
$ ./script sub 4 2 1
1
$ ./script xor a b c
Unknown operation: xor
$ ./script add
script[7]: shift: nothing to shift
Note the two last invocations. The last one failed to provide any numbers, so the second shift
fails. The second to last invocation notices that the operation is unknown and terminates prematurely.
By changing the assignment to op
and the initialization of acc
slightly, we can make the error messages slightly more useful:
#!/bin/sh
op=${1:?Expected operator}
shift
acc=${1:?Expected number}
shift
for arg do
case $op in
add) acc=$(( acc + arg )) ;;
sub) acc=$(( acc - arg )) ;;
mul) acc=$(( acc * arg )) ;;
div) acc=$(( acc / arg )) ;; # note: integer division here
*)
printf 'Unknown operation: %s\n' "$op" >&2
exit 1
esac
done
printf '%s\n' "$acc"
Testing again:
$ ./script
script[3]: 1: Expected operator
$ ./script add
script[6]: 1: Expected number
$ ./script add 1 2
3
The exact formatting of these error messages may differ depending on what shell acts as /bin/sh
on your system.
for var in $@
. Either shorten it tofor var
or change it tofor var in "$@"
(note the quotes). (2) What are you asking? You say you want to dotest.sh add 1 2 3
, but then you say you want to dotest.sh 1 2 3
. (3) Your second script is nearly what you want. Your first script adds all the arguments; what part of that do you want? – G-Man Says 'Reinstate Monica' Feb 12 '21 at 06:26test.sh add 1 2 3
for boththe first script allows it to do
– Dooooofy Feb 12 '21 at 06:34test.sh 1 2 3
and I need it to be able to add numbers together to get an output so my example wastest.sh add 1 2 3
to get 5add= echo $(( sum + var )) fi echo $add ``` pops up an error line 7: [: =: unary operator expected I'm not sure what is means
– Dooooofy Feb 12 '21 at 07:01$operator
is unset in your 2nd script, so the command sees[ = add ]
and complains, that the first operand is missing. – pLumo Feb 12 '21 at 07:15the task is just to write a bash script that accepts operators and a list of integers
– Dooooofy Feb 12 '21 at 07:25test.sh add 1 2 3
return5
and not6
? – pLumo Feb 12 '21 at 07:32calculator() { op=$1; shift; printf '%s\0' "$@" | paste -szd"$op" | xargs -0 echo 'scale=5;' | bc ; }
and then runcalculator + 1 2 3
-->6
orcalculator '/' 1 2 3
-->0.16666
– pLumo Feb 12 '21 at 07:32test.sh add 1 2 3
returns 5 because its adding the numbers together not multiplying them. what would I make operator equal? I need it to be able to do addition and subtraction.my task is as follows
using a loop command write a bash script that will accept an operator and a list of integers and have it execute the operation on the list
– Dooooofy Feb 12 '21 at 07:41