LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-26-2006, 04:54 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,357

Rep: Reputation: 213Reputation: 213Reputation: 213
Talking 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
 
Old 06-26-2006, 05:02 AM   #2
SlackDaemon
Member
 
Registered: Mar 2006
Distribution: RedHat, Slackware, Experimenting with FreeBSD
Posts: 222

Rep: Reputation: 30
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!

Last edited by SlackDaemon; 06-26-2006 at 05:03 AM.
 
Old 06-26-2006, 05:02 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Works here ... maybe the variables haven't been exported properly?


Cheers,
Tink

P.S.: Heh - beat by a couple of seconds...
 
Old 06-26-2006, 05:02 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 63
That should work!
Anyway, you may want to try this:
Code:
if [ "$HOSTNAME" == "orion" ] && [ "$tmpvariable" == "copy" ]; then
  echo " Hello"
fi
Yves.
 
Old 06-26-2006, 05:46 AM   #5
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,357

Original Poster
Rep: Reputation: 213Reputation: 213Reputation: 213
Wink

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 ?

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 !!

Last edited by Xeratul; 06-26-2006 at 06:04 AM.
 
Old 06-26-2006, 06:05 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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 ?
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
 
Old 06-26-2006, 06:18 AM   #7
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,357

Original Poster
Rep: Reputation: 213Reputation: 213Reputation: 213
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 !

Last edited by Xeratul; 06-26-2006 at 06:21 AM.
 
Old 06-26-2006, 09:36 AM   #8
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,357

Original Poster
Rep: Reputation: 213Reputation: 213Reputation: 213
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.

Last edited by Xeratul; 06-26-2006 at 10:36 AM.
 
Old 06-26-2006, 01:40 PM   #9
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

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

Yves.
 
Old 06-26-2006, 10:39 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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
 
Old 06-27-2006, 04:36 AM   #11
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,357

Original Poster
Rep: Reputation: 213Reputation: 213Reputation: 213
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 !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux command smart girl Linux - Newbie 3 03-30-2006 09:07 AM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 03:23 AM
unix command to linux command leonidas Linux - General 1 09-10-2004 01:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration