0

I am trying to write my first shell script for a class. The goal is to take a list of integers as a command line argument and display their squares and the sum of the squares. I am getting an error that the arguments are not being found.

This is the piece that is giving the error that the arguments are not found:

sumsq=0 #sum of squares  
int=0 #Running sum initialized to 0  
count=0 #Running count of numbers passed as arguments  

while [ $# != 0 ]  
do  
    numbers[$int]=`expr $1`     #Assigns arguments to integers
    let square=`expr $1*$1`     #Operation to square arguments
    squares[$int]=$square       #Calc. square of each argument
    sumsq=`expr $sumsq + $square`   #Add square to total
    count=`expr $count + 1`     #Increment count
    shift               #Remove the used argument
    int=`expr $int + 1`     #Increment to next argument

done

I am using dash shell.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
snaper
  • 5
  • 3
  • Could you please show the command line you entered, and the exact error message you received? – lurker Jan 04 '15 at 02:44
  • 1
    Is you are using [tag:bash] ? – Gilles Quénot Jan 04 '15 at 02:45
  • @lurker I entered it as './Assign2-1 3 4 5' The error message as follow repeated for each argument: ./Assign2-1: 27: ./Assign2-1: numbers[0]=3: not found ./Assign2-1: 28: ./Assign2-1: let: not found ./Assign2-1: 29: ./Assign2-1: squares[0]=: not found expr: syntax error – snaper Jan 04 '15 at 02:54
  • @sputnick I think I am using dash. It is the default Ubuntu shell. – snaper Jan 04 '15 at 02:58
  • Thanks to update your original POST instead of putting the command line in the comments – Gilles Quénot Jan 04 '15 at 02:59
  • @snaper: `echo $SHELL` will tell you – Gilles Quénot Jan 04 '15 at 03:09
  • It strongly looks to me like `dash` does not support arrays. The [`dash`](http://linux.die.net/man/1/dash) man page doesn't mention arrays. Strict POSIX shells (and `dash` aims to be a strict POSIX shell) don't. I invoked your script as `dash -x bfile.dash 1 2 3 4 5` and got the error `bfile.dash: 7: bfile.dash: numbers[0]=1: not found`. That's because the shell is treating what you intended to be an array assignment as a command name to be executed — and there isn't a command `numbers[0]=1` on most machines. If you want a shell supporting arrays, use Bash or Korn shell (or `zsh`). – Jonathan Leffler Jan 04 '15 at 03:09
  • 1
    Thank you for the help. It indeed was an issue with the shell. In case you come across someone with this problem again, remind them to also change the first line in the script, making sure it calls the correct interpretor (I did not include this line of code above), as this was still causing me problems. – snaper Jan 04 '15 at 03:26

2 Answers2

0

Dash doesn't support arrays, Bash does.

If you are running the script interactively you might not have bash configured as your default shell, run bash before trying.

If you are running it from console:

bash script.sh

If you are running it using its path (for example ./script.sh) ensure the first line of the script is:

#!/bin/bash

And not:

#!/bin/sh
Fernando
  • 1,382
  • 8
  • 17
0

It seems that you are a beginner, some good pointers to start learning:

FAQ: http://mywiki.wooledge.org/BashFAQ
Guide: http://mywiki.wooledge.org/BashGuide
Ref: http://www.gnu.org/software/bash/manual/bash.html
http://wiki.bash-hackers.org/
http://mywiki.wooledge.org/Quotes
Check your script: http://www.shellcheck.net/

And avoid people saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.

There's many things in your code that can be improved. Better learn the good way as soon as possible. Your code looks 80's =)


A corrected version (not tested) with a more bashy way to do things:

sumsq=0 #sum of squares  
int=0 #Running sum initialized to 0  
count=0 #Running count of numbers passed as arguments  

while (($# != 0 )); do 
    numbers[$int]=$1            #Assigns arguments to integers array
    square=$(($1*$1))           #Operation to square argument first arg by itself
    squares[$int]=$square       #Square of each argument
    sumsq=$((sumsq + square))   #Add square to total
    count=$((count++))          #Increment count
    shift                       #Remove the used argument
done
Laurel
  • 5,965
  • 14
  • 31
  • 57
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • Thanks for the suggestion. One of the biggest issues is the text book the class was given for examples and such was last updated in 2003. It hasn't been an issue for 85% of what I am learning, but the shell scripts section could use some supplemental material. – snaper Jan 04 '15 at 03:55