LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-24-2009, 04:22 AM   #1
luvshines
Member
 
Registered: Apr 2009
Posts: 65

Rep: Reputation: 16
Question A bit confused with && and || operators in shell script


I am totally new to scripting and was just trying to write a simple script in shell but am facing a problem with logic i am trying to write

#!/bin/sh

[ $1 -gt 10 ] && echo "Greater than ten" && exit 1 || ( echo "Less than or equal to 10" && exit 0)
echo "Still in the script"
exit 1

This script gives the output
[root@luvshines ~]# ./test.sh 5
Less than 10
Still in the script
[root@luvshines ~]# echo $?
1

If i remove the braces, it works fine

Looks like the expression within the braces is executed in another shell within the shell script, or maybe i am getting it totally wrong.

Please help. When i tried to debug and put braces around the first expression before the ||, then the output was

[root@luvshines ~]# ./test.sh 15
Greater than 10
Less than 10
Still in the script
[root@luvshines ~]# echo $?
1
[root@luvshines ~]#
 
Old 11-24-2009, 04:53 AM   #2
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Mageia1 KDE openbox
Posts: 2,807
Blog Entries: 33

Rep: Reputation: 139Reputation: 139
Hi, could I recommend Jerry Peeks Linux Magazine articles, wizard boot camp series?

http://www.jpeek.com/articles/linux_magazine.html

I'm no coder, hope this helps you

Regards Glenn
 
Old 11-24-2009, 05:44 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,016

Rep: Reputation: 91
yes,

Code:
(this is done in a subshell)
I use it sometimes for instance here:

(IFS=:;ls $PATH)

so it doesn't mess up IFS for the rest of the script.
 
Old 11-24-2009, 07:43 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.1, Debian Squeeze
Posts: 7,058
Blog Entries: 21

Rep: Reputation: 788Reputation: 788Reputation: 788Reputation: 788Reputation: 788Reputation: 788Reputation: 788
Quote:
Originally Posted by bigearsbilly View Post
yes,

Code:
(this is done in a subshell)
I use it sometimes for instance here:

(IFS=:;ls $PATH)

so it doesn't mess up IFS for the rest of the script.
The same thing can be done with IFS=: ls $PATH which is an instance of var=val [var=val ...] command as described here. Strange but true.
 
Old 11-24-2009, 10:39 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,332

Rep: Reputation: 294Reputation: 294Reputation: 294
Like bigearsbilly said, parenthesis create a subshell, you should use curly braces instead:
Code:
[ $1 -gt 10 ] && { echo "Greater than ten" && exit 1; } || { echo "Less than or equal to 10" && exit 0; }
echo "Still in the script"
exit 1
Notice that the syntax for curly braces is slightly different, they must be seperated by spaces, and a semicolon or newline is required before the closing brace.
 
Old 11-24-2009, 12:29 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,016

Rep: Reputation: 91
Quote:
Originally Posted by catkin View Post
The same thing can be done with IFS=: ls $PATH which is an instance of var=val [var=val ...] command as described here. Strange but true.
er, did you actually try this?
:-)

it don't work on mine. even in bash
 
Old 11-24-2009, 01:37 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.1, Debian Squeeze
Posts: 7,058
Blog Entries: 21

Rep: Reputation: 788Reputation: 788Reputation: 788Reputation: 788Reputation: 788Reputation: 788Reputation: 788
Quote:
Originally Posted by bigearsbilly View Post
er, did you actually try this?
:-)

it don't work on mine. even in bash
Oops!! Sorry bigearsbilly, it doesn't work.

I figure ... using your form, the $PATH is turned into its value by the parent shell and before parsing with IFS set to : whereas in my form the IFS work is done before $PATH is turned into its value so the : characters are not changed into word delimiters. A plausible hypothesis but
Code:
c:~$ IFS=':' /bin/ls /usr/local/bin:/usr/bin:/bin:/usr/games
/bin/ls: cannot access /usr/local/bin:/usr/bin:/bin:/usr/games: No such file or directory
c:~$ IFS=':' /bin/echo "$IFS"
Now I'm embarrassed and confused !
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash background (&) with control operators (&&) int0x80 Programming 1 02-11-2009 01:54 PM
C & kernel data structure & shell script vishalbutte Programming 7 01-13-2006 09:38 AM
Japanese canna won't work : Warning: かな漢字変&am OrganicOrange84 Debian 3 06-30-2005 03:28 PM
Phục hồi dữ liệu bị mất???, cứ pollsite General 1 06-27-2005 01:39 PM
shell script + change shell && continue darkRoom Programming 6 02-25-2005 03:50 AM


All times are GMT -5. The time now is 08:39 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration