Need to know correct syntax for conditional expression

Need to know correct syntax for conditional expression

Post by Iskandar » Fri, 09 Apr 1999 04:00:00



hi there,
    i tried to run a simple sheel script which has the following syntax,
but it doesn't work.  i hope someone could help on this.  i ' very new a
this and trying my best to learn it.   thanks.

---------------------------------------------------
#! /bin/bash
#myprogram
echo "Enter a value : "
read value
if (value='1')     <----- i'm having problem running this conditional
expression.  what is the correct syntax?
then
 echo "You have entered " $value  <-- can 't get this to work or
else
 echo "You  have entered a non " $value <--- here correctly
fi
-------------------------------------------------------

iskandar

kindly email me the reply. thanks again.

 
 
 

Need to know correct syntax for conditional expression

Post by Ken Pizzi » Fri, 09 Apr 1999 04:00:00



>#! /bin/bash
>#myprogram
>echo "Enter a value : "
>read value
>if (value='1')     <----- i'm having problem running this conditional
>expression.  what is the correct syntax?
>then
> echo "You have entered " $value  <-- can 't get this to work or
>else
> echo "You  have entered a non " $value <--- here correctly
>fi

Well, for bash, ksh, and other modern Bourne-class shells, that
test is probably best written:
  if [[ $value = 1 ]]
(But also see the "case" rewrite below.)

For maximal portability to older shells, one could instead use:
  if test 1 = "$value"
or, if you prefer the alternate visual presentation of that command:
  if [ 1 = "$value" ]
(You may want to go to deja-news and find some recent threads about
this syntax to understand why I wrote it in that order and with
the variable quoted.)

On the other hand, if it's maximal portablity you're after, it's
probably better to rewrite the if-then-else as a case statement:
  case $value in
    1) echo "You have entered $value" ;;
    *) echo "You  have entered a non $value" ;;
  esac

                --Ken Pizzini

 
 
 

Need to know correct syntax for conditional expression

Post by Kurt J. Lanz » Fri, 09 Apr 1999 04:00:00



> hi there,
>     i tried to run a simple sheel script which has the following syntax,
> but it doesn't work.  i hope someone could help on this.  i ' very new a
> this and trying my best to learn it.   thanks.

> ---------------------------------------------------
> #! /bin/bash
> #myprogram
> echo "Enter a value : "
> read value
> if (value='1')     <----- i'm having problem running this conditional
> expression.  what is the correct syntax?
> then
>  echo "You have entered " $value  <-- can 't get this to work or
> else
>  echo "You  have entered a non " $value <--- here correctly
> fi

Doesn't bash use [ ] arounf a test, just like Bourne? Don't the ( )
surround code to run in a subshell? Don't you also need more spaces?
Or a good book?
 
 
 

Need to know correct syntax for conditional expression

Post by Michael J. Parmele » Fri, 09 Apr 1999 04:00:00


Hello,

This is the correct format for your conditional statement:

if [ "$value" = "1" ] ;
 then
   whatever
 else
    whatever
fi

regards
mike p.


> hi there,
>     i tried to run a simple sheel script which has the following syntax,
> but it doesn't work.  i hope someone could help on this.  i ' very new a
> this and trying my best to learn it.   thanks.

> ---------------------------------------------------
> #! /bin/bash
> #myprogram
> echo "Enter a value : "
> read value
> if (value='1')     <----- i'm having problem running this conditional
> expression.  what is the correct syntax?
> then
>  echo "You have entered " $value  <-- can 't get this to work or
> else
>  echo "You  have entered a non " $value <--- here correctly
> fi
> -------------------------------------------------------

> iskandar

> kindly email me the reply. thanks again.