Print error for missing argument - linux

I am trying to write a script which will check number of arguments for first and second number; if both variable entered, it will do the calculation; if one argument is missing, it will print error message.
Here what I've done so far:
#!/bin/bash
echo -n "Enter the first number: "
read num1
echo -n "Enter the second number: "
read num2
if [ $# -le 1 ]; then
echo "Illegal number of arguments"
exit
else
echo "The sum is: " $(( num1 + num2 ))
fi
I am always getting error message even though I enter both of the numbers. What am I missing? Please help.

Test Your Assigned Variables, Not Positional Parameters
Your variables num1 and num2 aren't positional parameters, and so the special parameter $# is probably not what you think it is. You should change your conditional to check that both variables are set. For example:
declare -i num1 num2
read -p 'Enter the first number: ' num1
read -p 'Enter the second number: ' num2
if [ -z "$num1" ] || [ -z "$num2" ]; then
echo "Illegal number of arguments" >&2
else
echo "The sum is: $((num1 + num2))"
fi

Looks like you are messing up command line arguments and variables you are reading interactively. $# has nothing to do with variables you declared and/or read from command line. It is the number of command line arguments. You need to check the variables you attempted to read from console:
#!/bin/sh
echo -n "Enter the first number: "
read num1
echo -n "Enter the second number: "
read num2
[ -z $num1 ] || [ -z $num2 ] && echo "Illegal number of arguments" && exit 1
echo "The sum is: " $(( num1 + num2 ))
On the other hand, if you really want to check command line arguments, the script will be even simpler:
#!/bin/sh
[ -z $2 ] && echo "Illegal number of arguments" && exit 1
echo "The sum is: " $(( $1 + $2 ))
Here $1 refers to the first argument, $2 refers to the second argument and so on. $0 refers to the name of the script itself.

So, the way you have your program setup right now, it takes input through the read command, but that isn't the same as passing in an argument.
You pass an argument through the CLI, for instance:
./sum.sh 5 2 # => 7
Where sum.sh is the name of the file and 5 and 2 are your arguments.
So, the reason you keep getting "Illegal number of arguments" is because in bash the $# variable holds the number of arguments, but since you're reading the values in from the code, $# will always less than 1 because no arguments have been provided.
What I think you're looking for is something like this:
#!/bin/bash
if [ $# -le 1 ]; then
echo "Illegal number of arguments"
exit
else
echo "The sum is: " $(( $1 + $2 ))
fi
This article is pretty good if you want to learn more: http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.html

Related

how can I complete this?

the question is:
use a script to take two numbers as arguments and output their sum using (i) bc, (ii) expr. include error-checking to test whether two arguments were entered.
My answer:
echo " The first number is"
read a
echo " The second number is"
read b
c=`echo "scale=2; $a + $b"|bc`
echo _____________________________________________
echo " The sum of two numbers using bc:$c "
echo
d=`expr $a + $b`
echo " The sum of two numbers using expr:$d "
echo _____________________________________________
I can't include the error checking in this program. how can i do it? please help!
Get the numbers from arguments $1 and $2, not by prompting for input.
Check the number of arguments using $#.
if [ $# -ne 2 ]
then
echo "Usage: $0 number1 number2" >&2 # write error message to stderr
exit 1
fi
a=$1
b=$2
# rest of your script goes here

Making a script to take input from a user and use error checking to test if two arguments were entered: Linux

My Linux class just started to begin scripting and I am having trouble with one of my questions. The question is as follows: Use a script to take two numbers as arguments and output their sum using bc.
This is my first script. To get the script to execute you need to do chmod +x filename
This is what I have so far:
#!/bin/bash
read -p "Enter in a numeric value: " num1
read -p "Enter in a second numeric value: " num2
if [ $# -ne 2 ] ; then
echo "Enter in two numeric arguments"
else
echo "The sum of the entered values are: "
echo "$num1 + $num2"|bc
fi
I keep running into an error. When I enter in two values it displays "Enter in two numeric arguments". It shouldn't do that because if I enter in two values, the if statement will evaluate to false and go to the else statement. Is my logic wrong or am I approaching this the wrong way?
You can use a while loop and break.
n=0
ex=""
while [ 1 ]
do
read -p "Enter read in a numeric value" num
n=$(( n + 1))
if [[ $n -eq 2 ]]
then
ex=${ex}${num}
echo "Sum of the two values are"
echo ${ex} | bc
break
else
ex=$num" + "
fi
done

Run Linux shell script with arguments

Can somone help me with this: so i have this script
#!/bin/bash
echo -n "Enter a value for X:(999 to exit): "
read x
until [[ $x == 999 ]]
do
echo -n "Enter a value for Y: "
read y
echo "X="$x
echo "Y="$y
((a=y+x))
echo "X+Y="$a
((s=y-x))
echo "X-Y="$s
((m=y*x))
echo "X*Y="$m
((d=y/x))
echo "X/Y="$d
((m=y%x))
echo "X%Y="$m
echo -n "Enter a value for X:(999 to exit): "
read x
if [[ $x == 999 ]];
then
exit 0
fi
done
exit 0
but i didnt know how to write the rest of it, the missing thing is:
Use the two command line arguments when the script starts if the user supplied them, and then prompt for more numbers to continue in the loop.
Am guessing the arguments you are looking from the user are x and y values. The easiest way to check if user provided arguments is to use $# which gets you the number of arguments given by the user.
So use it like this:
if [ "$#" -eq 2 ]; #2 arguments provided by user
then
x=$1
...
fi

Detect number of argument and the value passed into the bash

I want to make sure my bash script can correctly detect user's input argument. Specifically, user can only pass 1 or 2 or 3 into the script, otherwise the script will be terminated. I wrote the following code:
#!/bin/bash
for args in $#
do
echo $args
done
if [ "$#" != 1 ] && [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
exit 1
fi
This script can only capture when user does not give any input, but cannot check whether user indeed input the number 1 not other values.
By the way, I am not sure how to express "input argument can accept number 1 or 2 or 3".
$# is an integer, so you have to use integer comparison. You can for example say:
if [ "$#" -ne 1 ]; then
echo "illegal number of parameters"
exit 1
fi
To check that the parameter is either 1, 2 or 3, you can use this regular expression (see something related):
if [[ ! $1 =~ ^(1|2|3)$ ]]; then
echo "the number is not 1, 2 or 3"
exit 1
fi
To express "input argument can accept number 1 or 2 or 3" I would for example say "we can just accept the argument being either 1, 2 or 3".
First off you can detect if the string is null or empty simply by doing the following:
if [ -z "$1" ]
then
echo "Argument $1 contains nothing"
fi
That I would say is your first step, and will allow you to filter out args that have no content.
Following on from that, you'd most likely need to do some comparison work on $1, $2 & $3
I'll just check something and come back to this in a moment.
Update
Just had to go find one of my scripts and check something... :-)
One way I've handled the checking of parms in the past is something like the following
#!/bin/sh
while [ $# -gt 0 ] || [ "$#" -le 4 ]; do
case "$1" in
*[!1-9]*) echo "Text: $1";;
*) echo "Number: $1"
esac
case "$2" in
*[!1-9]*) echo "Text: $2";;
*) echo "Number: $2"
esac
case "$3" in
*[!1-9]*) echo "Text: $3";;
*) echo "Number: $3"
esac
shift
done
Basically a simple regex, if I have more than 0 parameters or less than 4 parameters then I allow it through to a case statement, which then checks the content of each parameter.
This one just has an echo in, but you could just as easy set some flags, and then decide how to continue based on those flags.
For simple range checking however, you might just want to use a one liner similar to the following:
if [[ $# -gt 0 && $# -lt 4 ]]; then echo "Correct number of parameters"; fi
Again setting a flag to use later rather than echoing the results.
I assume you mean the input can only be 1, 2 or 3, so using a case statement is the best way. $1 is the variable that stores your argument, if it is equal to 1 case will execute the code in the block corresponding to the value 1 and so on.
case "$1" in
1) ...
;;
2) ...
;;
3) ...
;;
*) echo "Invalid argument"
;;
esac

Erroring out for passed argument less than 1, what am I doing?

I'm trying to get a script to echo a message when a number like -9 is entered.
The arguments have to be passed from the command line
This is what I have now.
#!/bin/bash
#Assign1part1
if (( $# != 1 )); then
echo "Error: Must only enter one argument" >&2
exit 1
fi
if (( $1 -lt 1 )); then
echo "Error: Argument must be a positive integer" >&2
exit 1
fi
seq -s, $1 -1 1
(( ... )) is not test.
$ (( -1 < 1 )) ; echo $?
0
$ (( -1 > 1 )) ; echo $?
1
You need to use [[ and ]], not (( and )). The former is testing, the latter is expression evaluation which allows for != but not -lt.
On top of that, your first error message is slightly off, making it sound like you've entered more arguments than you should have, even in the case where you enter none. It would be better phrased as something like "Must enter exactly one argument".
And, since $# is numeric, I prefer using the numeric comparisons, -ne rather than != in this particular case.
In other words:
#!/bin/bash
#Assign1part1
if [[ $# -ne 1 ]]; then
echo "Error: Must enter exactly one argument" >&2
exit 1
fi
if [[ $1 -lt 1 ]]; then
echo "Error: Argument must be a positive integer" >&2
exit 1
fi
seq -s, $1 -1 1
Running that with certain test data gives:
pax> testprog 5
5,4,3,2,1
pax> testprog 9
9,8,7,6,5,4,3,2,1
pax> testprog
Error: Must enter exactly one argument
pax> testprog 1 2
Error: Must enter exactly one argument
pax> testprog -7
Error: Argument must be a positive integer

Resources