LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   linux command : if then fi (https://www.linuxquestions.org/questions/linux-newbie-8/linux-command-if-then-fi-458368/)

Xeratul 06-26-2006 03:54 AM

linux command : if then fi
 
Hi,

it's just for a newbie question:

how to convert this basic code in sh script code of linux ?

Basic code:
Code:

if ( "$HOSTNAME" = "orion" and "$tmpvariable" = "copy" ) then
            print " Hello"
endif

I could find on the web this , but it doesnt work :-( :
Linux sh code:
Code:

#!/bin/sh
if [ "$HOSTNAME" = "orion" -a "$tmpvariable" = "copy" ]  ; then
            echo " Hello"
fi

Thank you :)

SlackDaemon 06-26-2006 04:02 AM

That script works like a charm. You just need to make sure that your variables are exported.

export tmpvariable

also assuming that you've set execute permissions on the script

chmod +x <scriptname>

best of luck!

Tinkster 06-26-2006 04:02 AM

Works here ... maybe the variables haven't been exported properly?


Cheers,
Tink

P.S.: Heh - beat by a couple of seconds...

theYinYeti 06-26-2006 04:02 AM

That should work!
Anyway, you may want to try this:
Code:

if [ "$HOSTNAME" == "orion" ] && [ "$tmpvariable" == "copy" ]; then
  echo " Hello"
fi

Yves.

Xeratul 06-26-2006 04:46 AM

Quote:

Originally Posted by theYinYeti
That should work!
Anyway, you may want to try this:
Code:

if [ "$HOSTNAME" == "orion" ] && [ "$tmpvariable" == "copy" ]; then
  echo " Hello"
fi

Yves.

Thank you for so much replies and so fast !

I have two questions.

In my script, I should then add export:
Code:

#!/bin/sh
echo "enter the variable 01"
read tmpvariable
export tmpvariable
if [ "$HOSTNAME" = "orion" -a "$tmpvariable" = "copy" ]  ; then
            echo " Hello"
fi

Hostname is already exported.
I dont really know why the export is very necessary yet. Actually why is it needed in such example ? :scratch:

I just tried thsi:
Quote:

#!/bin/sh
echo "enter the variable 01"
read tmpvariable
HOSTNAME="orion"
#export HOSTNAME
#export tmpvariable
if [ "$HOSTNAME" = "orion" -a "$tmpvariable" = "copy" ] ; then
echo " Hello"
fi
this one is working.


and my second question is :
Code:

[ "$HOSTNAME" == "orion" ]
Code:

if [ "$HOSTNAME" = "orion" ]
we can find both form, it's different than in basic. More flexible.
Which form is better to be used with = or == ?
What's the difference ?

this one :
Quote:

if [ "$HOSTNAME" = "orion" ] && ["$tmpvariable" = "copy" ] ; then
echo " Hello"
fi
says " COMMAND not found" :( :-(

and this alternate one:
Quote:

#!/bin/sh
echo "enter the variable 01"
read tmpvariable
HOSTNAME="orion"
#export HOSTNAME
#export tmpvariable
if [ "$HOSTNAME" == "orion" ] && ["$tmpvariable" == "copy" ] ; then
echo " Hello"
fi
says " COMMAND not found" line 7 also :( :-(

non working one:
Quote:

#!/bin/sh
echo "enter the variable 01"
read tmpvariable
HOSTNAME="orion"
export HOSTNAME
export tmpvariable
if [ "$HOSTNAME" == "orion" ] && ["$tmpvariable" == "copy" ] ; then
echo " Hello"
fi

That was very interesting !! Thank you !!

Tinkster 06-26-2006 05:05 AM

Quote:

Originally Posted by Xeratul
Thank you for so much replies and so fast !

I have two questions.

In my script, I should then add export:
Code:

#!/bin/sh
echo "enter the variable 01"
read tmpvariable
export tmpvariable
if [ "$HOSTNAME" = "orion" -a "$tmpvariable" = "copy" ]  ; then
            echo " Hello"
fi

Hostname is already exported.
I dont really know why the export is very necessary yet. Actually why is it needed in such example ? :scratch:

No, not in this example; but in your first attempt there was no
mention of the variable being defined; and if it's supposed to
be picked up from the environment it has to be exported.

Quote:

Originally Posted by Xeratul
I just tried thsi:

this one is working.

Defined in same scope.


Quote:

Originally Posted by Xeratul
and my second question is :
Code:

[ "$HOSTNAME" == "orion" ]
Code:

if [ "$HOSTNAME" = "orion" ]
we can find both form, it's different than in basic. More flexible.
Which form is better to be used with = or == ?
What's the difference ?

== is definitely better form :}


Cheers,
Tink

Xeratul 06-26-2006 05:18 AM

Ahhhh !! I understand !

Code:

#!/bin/sh
echo "enter the variable 01"
read tmpvariable
HOSTNAME="orion"
if [ "$HOSTNAME" = "orion" ] &&  [ "$tmpvariable" == "copy" ]  ; then
            echo " Hello"
fi

is working great !

Code:

#!/bin/sh
echo "enter the variable 01"
read tmpvariable
HOSTNAME="orion"
if [ "$HOSTNAME" == "orion" ] &&  [ "$tmpvariable" == "copy" ]  ; then
            echo " Hello"
fi

is working great too !


I found why the one there is not working:
Because only of the space too close :
Quote:

#!/bin/sh
echo "enter the variable 01"
read tmpvariable
HOSTNAME="orion"
export HOSTNAME
export tmpvariable
if [ "$HOSTNAME" == "orion" ] && ["$tmpvariable" == "copy" ] ; then
echo " Hello"
fi
damn, we have to be very very careful with spaces !
that's very quick to make a mistake.
I think even before == we should add space char.

and working too :
Code:

#!/bin/sh
echo "enter the variable 01"
read tmpvariable
HOSTNAME="orion"
if [ "$HOSTNAME" == "orion" -a  "$tmpvariable" == "copy" ]  ; then
            echo " Hello"
fi

Thank you very much !!
I learned a lot !

Xeratul 06-26-2006 08:36 AM

and sometimes we can see that there is [[ ]] double
what could be the deep purpose or use with this .. ?
I tried but had some troubles (nothg was working )...
via : if [[ "$HOSTNAME" == "orion" ]] ; then
bla bla


Quote:

#

Avec la version 2.02, Bash a introduit la commande de test étendue [[ ... ]], réalisant des comparaisons d'une façon familière aux programmeurs venant d'autres langages. Notez que [[ est un mot clé, pas une commande.

Bash considère [[ $a -lt $b ]] comme un seul élément, renvoyant un état de sortie.

Les constructions (( ... )) et let ... renvoient aussi un état de sortie de 0 si les expressions arithmétiques qu'elles évaluent se résolvent en une valeur non nulle. Ces constructions d'expansion arithmétique peuvent donc être utilisées pour réaliser des comparaisons arithmétiques.

let "1&lt;2" renvoie 0 (car "1&lt;2" se transforme
en "1")
(( 0 && 1 )) renvoie 1 (car "0 && 1" donne "0")

This script:
Quote:

#!/bin/sh
echo "enter the variable 01"
read tmpvariable
HOSTNAME="orion"
if [[ "$HOSTNAME" == "orion" -a "$tmpvariable" == "copy" ]] ; then
echo " Hello"
fi
Says : syntax error

Also,
Quote:

Perform a mkdir if a directory does not exist:

test ! -d tempdir && mkdir tempdir
this is meaning that we'll have an if ... then ... else
it's kind of like a simple if ... then ...fi
Quote:

commande1 && commande2
Exécute commande2 si et seulement si commande1 s'est bien déroulée. Généralement utilisé dans les conditions des structures if, while et until.

theYinYeti 06-26-2006 12:40 PM

I don't have time for a good answer. Meanwhile, just one tip: don't forget the spaces around [ and ].

Yves.

chrism01 06-26-2006 09:39 PM

Here's a bit about -a vs && :
http://www.dartmouth.edu/~rc/classes...ests2.html#top
See also here for [ ] vs [[ ]]
http://www.tldp.org/LDP/abs/html/tes...ml#DBLBRACKETS

Xeratul 06-27-2006 03:36 AM

Quote:

Originally Posted by chrism01

Thank you very much for your Both Support in SH programing !!

(I have to debug it now ... :-) :-) ) (errors errors...)

To be continued

Greetings !


All times are GMT -5. The time now is 09:29 AM.