LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash shell scripting - && and || (http://www.linuxquestions.org/questions/programming-9/bash-shell-scripting-and-and-and-%7C%7C-10012/)

gui10 12-13-2001 03:51 AM

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.

Mik 12-13-2001 04:05 AM

Try making it something like this:

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

gui10 12-13-2001 07:05 AM

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?

bluecadet 12-13-2001 07:24 AM

hehe, no that's now doubly wrong! :D

[ 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

Mik 12-13-2001 08:14 AM

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?

gui10 12-14-2001 01:12 AM

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... :D

bluecadet 12-14-2001 04:04 AM

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.

gui10 12-14-2001 03:41 PM

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

bluecadet 12-14-2001 04:54 PM

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

gui10 12-15-2001 09:45 AM

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?

bluecadet 12-15-2001 03:37 PM

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


All times are GMT -5. The time now is 07:52 AM.