ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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...
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.
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...
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....
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.