Operating System - HP-UX
1748223 Members
4579 Online
108759 Solutions
New Discussion юеВ

Re: if statement in the scripting

 
Anjaneyulu
Frequent Advisor

if statement in the scripting

Hi,

I am executing one program . But that is showing errors. Please is help me.

My program is:

echo $int1
echo $int2
if [ "$int1" -eq "$int2" ]
then
echo "command was successful"
elif
echo "command was unsuccessfull"
fi

I want in the execution time i want to assign the numbers for int1 and int2. Please tell me how to assign the numbers in execution time.

thanx & Regards,
Anji.
12 REPLIES 12
Aneesh Mohan
Honored Contributor

Re: if statement in the scripting

Hi Anji,

Use

read int1 and read int2 at the begining.

Aneesh
Oviwan
Honored Contributor

Re: if statement in the scripting

Hi

to assign a value to a variable:
varname=varvalue
echo $varname

to debug your script you can set "set -x" after "#!/usr/bin/ksh"

#!/usr/bin/ksh
set -x

HTH
Aneesh Mohan
Honored Contributor

Re: if statement in the scripting

Hi again..

If you want the script to prompt you for the variable at the time of execution then you may need to do

echo "Reading the value of int1 :\c"
read int1
echo "Reading the value of int2 :\c"
read int2
if [ "$int1" = "$int2" ]
then
echo "command was successful"
else
echo "command was unsuccessfull"
fi

If you want assign a value in the script with out prompting then

int1=xyz
int2=xyz

Aneesh

Aneesh
Aneesh Mohan
Honored Contributor

Re: if statement in the scripting

Hi Anji,

I can see you were not assigned points for previous thread,please assign point if our replies helped.

Aneesh
Anjaneyulu
Frequent Advisor

Re: if statement in the scripting

Thanks to all
Anjaneyulu
Frequent Advisor

Re: if statement in the scripting

hi but also iam getting error


read int1
read int2

if [ "$int1" -eq "$int2" ]
then


echo "command was successful"
elif
then
echo "command was unsuccessfull"
fi


I tried like above . But Iam getting errors. please give me the instructions.

Regards,
Anji.
Anjaneyulu
Frequent Advisor

Re: if statement in the scripting

hi Aneesh,

Please help me. iam two errors.

fi unexpected
then unexpected
Oviwan
Honored Contributor

Re: if statement in the scripting

do not use
elif
use else:

#!/usr/bin/ksh

read int1
read int2

if [[ "$int1" -eq "$int2" ]]
then
echo "command was successful"
else
echo "command was unsuccessfull"
fi

HTH
Anjaneyulu
Frequent Advisor

Re: if statement in the scripting

Hi,

I tried Iam getting wrong output:
#!/usr/bin/ksh

read int1
read int2



if [["$int1" -eq "$int2"]]
then


echo "command was successful"
else

echo "command was unsuccessfull"

fi


OUTPUT:-

$ sh test1
3
3
test1: [[3: not found
command was unsuccessfull