Link to home
Create AccountLog in
Avatar of pvinodp
pvinodp

asked on

A doubt on replacing a variable in a text according to a if else condition

//my shell file
var1=
msg="This becomes to crazy ${var1} position now"
fileNameOptionA="temp1"
fileNameOptionB="temp2"
if[condition]
   var1=${fileNameOptionA}
   display msg #This becomes to crazy temp1 position now

else

   var1=${fileNameOptionB}
   display msg #This becomes to crazy temp2 position now
fi
Avatar of tel2
tel2
Flag of New Zealand image

Do you have any questions for us, pvinodp?
is this what you are trying to do?

msg="This becomes to crazy \${var1} position now"
fileNameOptionA="temp1"
var1=${fileNameOptionA}
eval "echo $msg"
Avatar of pvinodp
pvinodp

ASKER

the variable $var1 does not seem to take the value within the if or else condition.

For both if and else condition I get the following output upon executing "display msg"

This becomes to crazy  position now.

THere is no value in the variable $var1 on both conditions.
$msg is set well before the if condition and hence irrespective of what the outcome is, $msg will always be with the initial value of $var (null)

### this should work ###
fileNameOptionA="temp1"
fileNameOptionB="temp2"
var1=fileNameOptionB

if[condition]
   var1=${fileNameOptionA}
fi
echo "This becomes to crazy ${var1} position now"
###
is this what you are trying to do?
#!/bin/bash
var1=
msg='This becomes to crazy ${var1} position now'
fileNameOptionA="temp1"
fileNameOptionB="temp2"
if [ condition ] ; then
   var1=${fileNameOptionA}
   eval "echo $msg"  #This becomes to crazy temp1 position now

else

   var1=${fileNameOptionB}
   eval "echo $msg"  #This becomes to crazy temp2 position now
fi
Avatar of pvinodp

ASKER

@ozo[ 38884932]:
yes you are correct.

But my command is :: dialog --msgbox  "${msg}" 30 30

SO I tried ::  dialog --msgbox  " eval ${msg}" 30 30 ,, but this did NOT work.

Also tried:
 textT=eval $msg
dialog --msgbox  "${textT}" 30 30  // this also dint work
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of pvinodp

ASKER

thanks