1832979 Members
3675 Online
110048 Solutions
New Discussion

Re: shell script error

 
SOLVED
Go to solution
Sajjad Sahir
Honored Contributor

shell script error

when i am entering mismatching number then it also showing matching.

eg:12 and 13 it showing mathcing
12 and 12 also showing matching.

#!/usr/bin/sh
echo "enter the number"
read number1
echo "enter the number"
read number2
if number1=number2
then
echo "matching"
exit
else number1!=number2
echo "Not matching"
fi


exit
18 REPLIES 18
Yogeeraj_1
Honored Contributor

Re: shell script error

hi,

when working with the variables you should use the $ sign.

e.g.

if $number1 = $number2
then

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Michael Steele_2
Honored Contributor

Re: shell script error

Also capitalize your var.'s for readability.
Support Fatherhood - Stop Family Law
Yogeeraj_1
Honored Contributor

Re: shell script error

hi again,

below the modified script:

$ vi script1.sh
#!/usr/bin/sh
echo "enter the number"
read number1
echo "enter the number"
read number2
if [ $number1 = $number2 ]
then
echo "matching"
exit
else [ $number1 != $number2 ]
echo "Not matching"
fi
exit

~
~
~
~
~
~
~
~
~
"script1.sh" 14 lines, 200 characters
$ ./script1.sh
enter the number
12
enter the number
13
Not matching
$


hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor
Solution

Re: shell script error

hi again,

below a more readable output:

$ vi script1.sh
#!/usr/bin/sh
echo "enter the number"
read NUMBER1
echo "enter the number"
read NUMBER2
if [ $NUMBER1 = $NUMBER2 ]
then
echo "matching"
exit
else [ $NUMBER1 != $NUMBER2 ]
echo "Not matching"
fi
exit

~
~
~
~
~
~
~
~
~
"script1.sh" 14 lines, 200 characters
$ ./script1.sh
enter the number
12
enter the number
12
matching
$ r
./script1.sh
enter the number
12
enter the number
13
Not matching
$



kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Sajjad Sahir
Honored Contributor

Re: shell script error

#!/usr/bin/sh
echo "enter the number"
read NUMBER1
echo "enter the number"
read NUMBER2
if $NUMBER1=$NUMBER2
($NUMBER1 = $NUMBER2 I TESTED THIS ONE ALSO)
then
echo "matching"
else
echo "Not matching"
fi
Sajjad Sahir
Honored Contributor

Re: shell script error

dear all
still it showing error
i don't why i am still confusing


#!/usr/bin/sh
echo "enter the Number"
read NUMBER1
echo "enter the Number"
read NUMBER2
if [$NUMBER1= $NUMBER2]
then
echo "matching"
exit
else [$NUMBER1 != $NUMBER2]

echo "Not matching"
fi
exit
Sajjad Sahir
Honored Contributor

Re: shell script error

Yogeeraj what is the problem in my shell script
I wrote in the same way but i am not getting
any parmeter missing or somethign like that
or file permission some thing like that

#!/usr/bin/sh

echo "enter the Number"

read NUMBER1

echo "enter the Number"

read NUMBER2

if [ $NUMBER1= $NUMBER2 ]

then

echo "matching"

exit

else [ $NUMBER1 != $NUMBER2 ]

echo "Not matching"
fi
exit
Davis Paul
Valued Contributor

Re: shell script error

Hi Sajjad,
I can execute the following script without any error.
#!/usr/bin/sh
echo "enter the Number"
read NUMBER1
echo "enter the Number"
read NUMBER2
if [ $NUMBER1 -eq $NUMBER2 ]
then
echo "matching"
exit
else [ $NUMBER1 != $NUMBER2 ]
echo "Not matching"
fi
exit

For setting the execution permission, use the command:
#chmod u+x filename.
If you want a trial run of the script without setting the execution permition use:
# sh filename
Regards,
Davis Paul.
Sajjad Sahir
Honored Contributor

Re: shell script error

dear david paul
i gave already that permission
also
./
sh etc...

any how others can excute this one without
erro
but i am getting error
i don't why?
it is amazing me
sajjad
Yogeeraj_1
Honored Contributor

Re: shell script error

hi again,

it could be a problem with spaces in the script...

am attaching the code in a text file.

Please download and run it.

$ chmod +x script1.sh
$ ./script1.sh


revert

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Davis Paul
Valued Contributor

Re: shell script error

Hi Sajjad,
Please post the error messages which you are getting now.
Regards,
Davis Paul

Sajjad Sahir
Honored Contributor

Re: shell script error

closed
Dennis Handly
Acclaimed Contributor

Re: shell script error

>closed

You haven't closed the thread.
Also, if any answers here are useful, you need to assign points before you close it.
OldSchool
Honored Contributor

Re: shell script error

actually, the test in the else case isn't necessary, as its either matching or it doesn't...since you tested (and failed) match the else should just *happen*

also, the original poster needs to be aware that the tests indicated are for matching strings , so "1" and "01", while numerically equal fail as the strings differ
Bill Hassell
Honored Contributor

Re: shell script error

It's REALLY important to keep strings and numbers separate. The strings 01 001 and 1 are very different strings. *ALWAYS* compare numbers with the numeric comparison operators:

-eq -ne -gt -ge -lt -le

Note that the strings 1 9 22 777 will compare (sort) in this order:

1
22
777
9

but as numbers, they will sort this way:

1
9
22
777



Bill Hassell, sysadmin
Aashique
Honored Contributor

Re: shell script error

Hi,
here is the correct information of the script:

#!/usr/bin/sh
echo "enter the number"
read number1
echo "enter the number"
read number2
if [ $number1 = $number2 ]
then
echo "matching"
exit
else
echo "Not matching"
fi


Thanks & Regards

Aashique
Yogeeraj_1
Honored Contributor

Re: shell script error

hi Oldschool and Bill,

I thank you for making these clarifications. I was not really aware of these *Intricacies* in shell scripting... :(

it seems that the last poster did not read your post properly, so i will post the new script with corrections made as per your recommendations:


#!/usr/bin/sh
echo "enter the number"
read number1
echo "enter the number"
read number2
if [ $number1 -eq $number2 ]
then
echo "matching"
exit
else [ $number1 -ne $number2 ]
echo "Not matching"
fi

Sorry for inconvenices!

kind regards
yogeeraj

No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Sajjad Sahir
Honored Contributor

Re: shell script error

I closed