LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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
 
Search this Thread
Old 12-13-2001, 04:51 AM   #1
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Rep: Reputation: 30
bash shell scripting - && and ||


hm... i've been wondering how to use && and || in writing a bash shell script

i mean... where do you put them if you want to test for 2 conditions?
eg.

for n in $filesizes
do
if $n -ge 0 && $n -le 300
then
echo $n
fi
done

i am guessing the above is not correct as i tried it and it kinda didn't work...

appreciate help on this.
 
Old 12-13-2001, 05:05 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
Try making it something like this:

for n in $filesizes
do
if [ $n -ge 0 ] && [ $n -le 300 ]
then
echo $n
fi
done
 
Old 12-13-2001, 08:05 AM   #3
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Original Poster
Rep: Reputation: 30
beg your pardon... forgot the test keyword... the code should have been:

===
for n in $filesizes
do
if test [ $n -ge 0 ] && [ $n -le 300 ]
then
echo $n
fi
done
===

just to make sure... is the above correct?
 
Old 12-13-2001, 08:24 AM   #4
bluecadet
Member
 
Registered: Oct 2001
Distribution: MD81 RH71
Posts: 555

Rep: Reputation: 30
hehe, no that's now doubly wrong!

[ IS test:

ls -l `which [`
lrwxrwxrwx 1 root root 4 Dec 5 21:51 /usr/bin/[ -> test*

see? [ needs ], but test doesn't. i would presume this is just done by checking the program name inside of test, and simply being fussy on purpose.

remember that the && is totally external to the test statement, and fits in between the test results and the if, integrated in neither
 
Old 12-13-2001, 09:14 AM   #5
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
How does that work then with the [ and the ]. I always assumed that was just part of the syntax of an if statement. I can see how it would work the same as the /usr/bin/test but then how would it work if there is no /usr/bin/[ present?

I tried the script with both test and with [] on linux, dec alpha and sunos and they all worked the same. But on the sun machine there is no [ file.

Linux:
/usr/bin/[ -> /usr/bin/test
/usr/bin/test = executable

Dec Alpha:
/usr/bin/[ = executable
/usr/bin/test = executable

Did a checksum on both files and they are the same

SunOs:
/usr/bin/[ doesn't exist
/usr/bin/test = executable


I'm just curious how it works. Anyone got an explanation for that?
 
Old 12-14-2001, 02:12 AM   #6
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Original Poster
Rep: Reputation: 30
i'm not too familiar with bash scripting... i'm thinking along C (as u probably can see -> what a lousy pun!) and that's why i wrote stuff that way...

so if i'm not wrong bluecadet, it should go something like this?
EITHER:
for n in $filesizes
do
if test $n -ge 0 && if test $n -le 300
then
echo $n
fi
done

OR:
for n in $filesizes
do
if [$n -ge 0] && if [$n -le 300]
then
echo $n
fi
done


if not, please correct the example to illustrate your point... in case i code 1,000,000 lines before u do so...
 
Old 12-14-2001, 05:04 AM   #7
bluecadet
Member
 
Registered: Oct 2001
Distribution: MD81 RH71
Posts: 555

Rep: Reputation: 30
well, a version that actually does something:

for n in `ls -l | cut -b35-42`
do
if [ $n -ge 0 ] && [ $n -le 300 ]
then
echo $n
fi
done

I'm not sure how aware you are of teh for loop in script, but it operates VERY differently from a standard c for, as it cycles through a list of inputs, so just passing it a $value will not normally be any use, although you could actually assign the ls statment to a variable and it would work, as long as all values are space delimited, i personally don't like that look tho, a bit confusing

it can be hard to get to grips with order of operation, in my code the && operates between the test executions and the if. You also need to remember that scripts are executed at a system level, ,and not interpreted, therefore you must have a space between [ and $n etc... for the same reason you'd never even think to type emacsmyletter.
 
Old 12-14-2001, 04:41 PM   #8
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Original Poster
Rep: Reputation: 30
yea i know bash is a quite far cry from C. what i meant was that i wanted to know if the functionality of the && in C is also available in bash scripting... since it seems to be so in the man pages... but i want to know how it is implemented...
 
Old 12-14-2001, 05:54 PM   #9
bluecadet
Member
 
Registered: Oct 2001
Distribution: MD81 RH71
Posts: 555

Rep: Reputation: 30
well, i've no idea how the code is written but naturally shell cript is just a bundle of different programs linked via streams, so unless they really start messing around with a programs output, there's not really a huge amount of intuitivity to play with. At least from a C background,. if shell was all you ever knew, then it'd make perfect sense i guess... the same functionality is there, sometimes you just gotta look at thing sideways... be more concerned with scope etc....
 
Old 12-15-2001, 10:45 AM   #10
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Original Poster
Rep: Reputation: 30
okay how about this:

what if i want to list all files with read permissions in a particular directory that must be under 2000 bytes in size...

don't i need the && operator somewhere?
 
Old 12-15-2001, 04:37 PM   #11
bluecadet
Member
 
Registered: Oct 2001
Distribution: MD81 RH71
Posts: 555

Rep: Reputation: 30
well, there's literally dozens of ways you could do that, different ways to obtain the data, like you could either use a cut with ls to get e list of sizes, or use test to compare the sizes (i think that's possible). but yeah, you can use &&'s if you want... my example did include one...
 
  


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
Bash scripting and && operator Dark_Helmet Programming 16 09-18-2005 10:57 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
BASH scripting: confused about redirection & file descriptors funkymunky Programming 1 06-07-2004 08:47 AM
Shell scripting & piping dimavo Programming 8 04-24-2003 06:27 PM


All times are GMT -5. The time now is 11:34 AM.

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