[SOLVED] less than condition in if statement not working as desired
Linux - NewbieThis 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
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
less than condition in if statement not working as desired
In following bash script I am finding that irrespective of thrdN > 10 i.e. for thrdN=12 or 24 , ${thrdN}-lt$TEN ] condition is satisfied ? What am I doing wrong ?
I wanted to ensure that always 'SimName' string has same legnth e.g. if thrdN = 2 then SOME_LONG_NAME_THRD02 and if thrdN = 12 then SOME_LONG_NAME_THRD12.
I am using centOS 6.2.
Thanks in advance,
Code:
SERIES_NAME="PerfCentOS"
PATIENT_ID="U032"
SIM_N_START=502
COUNT=0
MEM_GB=8
SCRATCH="/home/ssr/Desktop/PerfCentOS"
MESH_SRC_DIR="/home/ssr/Desktop/PerfCentOS"
ADINA_IN_SRC_DIR="/home/ssr/Desktop/PerfCentOS"
ADINA_HOME="/opt/adina/8.8.2"
TEN=10
for matN in 1 2 4 # if in series order then use like(( matN=1 ; matN<=3; matN++))
do
for thrdN in 1 2 3 4 6 8 12 24
do
#Set simulation number
SIM_N=$(($SIM_N_START + $COUNT))
COUNT=$(($COUNT + 1))
echo "Simulation number = ${SIM_N}"
# Make directory for simulation
if [ ${thrdN}-lt$TEN ]
then
echo " THREAD=${thrdN} less than $TEN"
if [ $matN-lt${10} ]
then
SimName="sim0${SIM_N}_${SERIES_NAME}_${PATIENT_ID}_MAT0${matN}_THRD0${thrdN}"
else
SimName="sim0${SIM_N}_${SERIES_NAME}_${PATIENT_ID}_MAT${matN}_THRD0${thrdN}"
fi
else
echo " THREAD=${thrdN} more than equal to 10"
if [ $matN-lt${10} ]
then
SimName="sim0${SIM_N}_${SERIES_NAME}_${PATIENT_ID}_MAT0${matN}_THRD${thrdN}"
else
SimName="sim0${SIM_N}_${SERIES_NAME}_${PATIENT_ID}_MAT${matN}_THRD${thrdN}"
fi
fi
done
done
Last edited by samasat; 05-27-2012 at 11:53 AM.
Reason: Formating correction to replace "Quote" with "Code"
TEN=10
SOMETHING_ELSE=12
if [ ${SOMETHING_ELSE} -lt ${TEN} ]; then
echo Less than 10!
fi
This does not:
Code:
TEN=10
SOMETHING_ELSE=12
if [ ${SOMETHING_ELSE}-lt${TEN} ]; then
echo Less than 10!
fi
It would seem the if statement can't make sense of "12-lt10". I can't say I blame it.
I would recommend putting the variables inside double quotes ([ "${SOMETHING_ELSE}" -lt "${TEN}" ]), as the test will otherwise throw an error should the variable happen to be undefined or empty.
I would recommend putting the variables inside double quotes ([ "${SOMETHING_ELSE}" -lt "${TEN}" ]), as the test will otherwise throw an error should the variable happen to be undefined or empty.
Alternatively the more robust [[ ... ]] can be used.
The differences between [[ ... ]] and [ ... ] are detailed here.
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Go back and edit your opening post to change them, please.
Actually, for integer calculations like this, ((..)) should be used instead (assuming you're using bash or ksh).
Code:
if (( SOMETHING_ELSE < TEN )); then
The benefits of this are that you can use standard math operators instead of letter-style options, and there's no need to prefix variables with "$".
The [[..]] operator is recommended for string/file tests and complex expressions.
Avoid using the old [..] test unless you specifically need POSIX-style portability.
"[", being a traditional command, needs to see every entry following it as a separate argument. That means each one has to be separated by whitespace. Variables should also always be quoted to avoid problems with word-splitting.
[[..]] and ((..)), however, are keywords, and thus allow greater flexibility in syntax (although there are still limits in what they can handle).
Finally, since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them. And please don't use the full "${var}" brace-enclosed variable form when you don't need it. It does nothing except cause additional typing and clutter up the code.
By the way, from your description it looks like what you really want to do is ensure that the digit is zero-padded before using it. That can be accomplished by a simple printf command.
Be careful not to use zero-padded numbers in arithmetic operations though, because then the shell will treat them as octal values, rather than decimals. Do the number padding just before you have to print the output.
Thanks Ser and Catkin.
That made my code work.
Actually earlier I tried those spaces since I was aware that there should be space after '[' and before ']' ( and now I know from David's links that in fact all variables needs space in between).
That time I tried for inner if condition as given below:
Code:
if [ ${matN} -lt ${10} ]
then
SimName="sim0${SIM_N}_${SERIES_NAME}_${PATIENT_ID}_MAT0${matN}_THRD0${thrdN}"
else
SimName="sim0${SIM_N}_${SERIES_NAME}_${PATIENT_ID}_MAT${matN}_THRD0${thrdN}"
fi
and its output:
Code:
Simulation number = 502
THREAD=1 less than 10
./PerfCentOS.pbs: line 33: [: 1: unary operator expected
But now I understand that ${10} is wrong and I should have used simply 10. in fact to get around with this I used TEN=10 declaration in parent if statement.
Thanks David for you in depth help with a lot of information. The first link explains all nuances very well. Even though I have not yet grasped completely '[' and '[[' '((' I guess I understand that you mentioned in your post ( [[--]], ((--)) being bash only feature and that ((--)) facilitates use of arithmetic < , > for integers) still not clear how it really helps.
And also I sorry that out of ignorance I used wrong formating. I have corrected it now.
I attemted you suggestion by using:
Code:
if (( thrdN < TEN ))
then
and it works.
Thanks for your suggestions about all caps formating convention for Environment variables and regarding "${var}". I was told by my colleague that it is safer so I was using that.
Last edited by samasat; 05-28-2012 at 11:31 AM.
Reason: Correcting typo - replace old $TEN by TEN in line "with this I used $TEN=10 declaration for parent if statement."
By the way, from your description it looks like what you really want to do is ensure that the digit is zero-padded before using it. That can be accomplished by a simple printf command.
Be careful not to use zero-padded numbers in arithmetic operations though, because then the shell will treat them as octal values, rather than decimals. Do the number padding just before you have to print the output.
Thanks David. You got my intent correct. I though printf only outputs on screen and I won't be able to get it to write to a variable string. However, I am just starting to do bash scripting and actually I am not having Linux background. So I am happy I learnt some things.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.