help - scripting command

hello

I'n new to the unix environment :smiley:
I'm trying to script a command that takes 2+ integers and adds them together in bash.

eg

$ add 10 12 13
35

probably kind of basic but i also have to verify that they are indeed integers and that the "add" command has at least 2 parameters.

any ideas?

#! /bin/bash

if [ $# -lt 2 ]
then
    echo "Less than 2 paramaters"
    exit
fi

# The below if condition checks if the number contains a decimal point or not.
# Actually in the for-loop below expr used will not work if the number is not an integer. If you try doing "expr 1.1 + 2.2", it'll say something like this: "expr: non-numeric argument"

echo $* | grep -qe "\."
if [ $? -eq 0 ]
then
    echo "One of the input in not an integer"
    exit
fi

sum=0
for x in $@
do
    sum=`expr $sum + $x`
done

echo "The sum is $sum"

awesome i will try this out!

thanks!

There's no need for external commands (grep and expr).

Use a function to determine whether an argument is an integer:

is_int()
{
  case $1 in
    *[!0-9]*|"") return 1 ;;
  esac
}

Then check each arg before using it:

total=0
for int
do
  is_int "$int" || exit 1
  total=$(( $total + $int ))
done
printf "Total: %s\n" "$total"

thanks

is there a way after the "then" to just continue in the script without displaying the error message?

if [ $? -eq 0 ]
then
    echo "One of the input in not an integer"
    exit
fi

If you just want to continue in the script, why would you want a check to see if the number is an integer or not. Remove the whole if-block.

well im doing something similar just checking if only a number is entered no letters can be entered. If a number is entered it should continue to the next part of the bash script. If a letter is entered, user has to try again until a number is entered

i think its an until loop but i cant get it to work

read -p "How many tags to enter:" cars
        echo $cars | grep "[a-zA-Z]"
        if [ $? -eq 1 ]
        then
        read -p "Please enter car year:" yearmodel
        else
        read -p"Invalid number. Please re-enter:" $cars
        exit

this is the next part of the script

  read -p "Please enter car year:" yearmodel

im using an until loop but it wont stop looping

Try using "break" to come out of the loop once work is done.

@gangsta
Please let us know exactly what Operating System and version and Shell you are running. The "read -p" is not "read with a prompt string" in POSIX Shell.

uname -a
echo $SHELL

It would really help if you replied to questions posed on your own threads.
Repeatedly posting the same code segment from a much larger script is not good.
Have you read cfajohnson's post?

this is what i have

      
read -p "How many cars to enter:" cars
until "$cars" [ -ne 1 ] && grep "[a-zA-Z]"
do
read -p "Invalid number. re-enter:" cars
done

How about this example in bash :

while true
do
   read -p "How many cars to enter: " tot
   [ $tot -ge 0 ] 2>/dev/null && break
   echo "Invalid number. re-enter"
done
num=0
while [ $num -lt $tot ]
do
    read -p "Enter car #$((++num)): " cars[num]
done
num=0
while [ $num -lt $tot ]
do
   echo "Car #$((++num)) is ${cars[num]}"
done

or, using for (( )) loops:

while true
do
   read -p "How many cars to enter: " tot
   [ $tot -ge 0 ] 2>/dev/null && break
   echo "Invalid number. re-enter"
done
for((num=1; num<=tot; num++)) {
    read -p "Enter car #$num: " cars[num]
}
for((num=1; num<=tot; num++)) {
   echo "Car #$num is ${cars[num]}"
}
1 Like

how do i make the error message only to be displayed if a wrong answer is diplayed?

while true
do
   read -p "How many cars to enter: " tot
   [ $tot -ge 0 ] 2>/dev/null && break
   echo "Invalid number. re-enter"
done

That's what it does for me now:

$ cat gangsta
#!/bin/bash
while true
do
   read -p "How many cars to enter: " tot
   [ $tot -ge 0 ] 2>/dev/null && break
   echo "Invalid number. re-enter"
done
$ ./gangsta
How many cars to enter: two
Invalid number. re-enter
How many cars to enter: -1
Invalid number. re-enter
How many cars to enter: 3
$./gangsta
How many cars to enter: 5
$

@gangsta: What's the purpose of "2>/dev/null" in "[ $tot -ge 0 ] 2>/dev/null && break"? What do you think STDERR would contain upon this conditional check that you want to direct it to /dev/null?

yea same thing for me but i only want it to show "How many cars to enter" the one time after that if a wrong answer is entered it should only be "Invalid number. Re-enter"

It will contain things like:

Input: two
line 5: [: two: integer expression expected
Input:
line 5: [: -ge: unary operator expected

@gangsta: you want this then:

read -p "How many cars to enter: " tot
while true
do
   [ $tot -ge 0 ] 2> /dev/null && break
   read -p "Invalid number. re-enter: " tot
done
1 Like
#!/bin/bash
is_int()
{
  case $1 in
    *[!0-9]*|"") return 1 ;;
  esac
}

while :
do
  read -ep "How many cars to enter: " tot
  is_int "$tot" && [ "$tot" -gt 0 ] && break
  printf "%s\n"  "Invalid number: $tot"\
                 "Please enter a number greater than 0"\
                 "Press ENTER to continue"
  read
done
1 Like