Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Howdy all... I'm writing a simple script to change some security settings and I need someone to explain the -z option in a if statement. The way I understand it, if I have an empty variable then do something... for example:
Code:
for variable in '/bin/grep -i ^banner /tmp/sshd_config'; do
if [ -z "$variable" ]; then
sed -i '109i\banner /etc/motd' /tmp/sshd_config
elif [ -n "$variable" ]; then
sed -i '/^[Bb]anner/ d' /tmp/sshd_config
sed -i '108i\Banner /etc/motd' /tmp/sshd_config
fi
done
So the way I understand it, if grep comes up with nothing, then the string is empty...so in the empty "variable" add it at line 109. If there IS a line that begins with banner anywhere, then delete it and added it on line 108.
Am I misunderstanding something here because it will only run the option with -n. If I change the first option to -n then it run that option. Is the string not really empty? Thanks for the help!
#!/bin/bash
BLAH=$(grep -i ^banner blah.sh)
if [ -z "$BLAH" ] ; then
echo "BLAH has a length of zero"
fi
if [ -n "$BLAH" ] ; then
echo "BLAH has a non-zero length"
fi
produces this output
Code:
[user@machine:~]:./blah.sh
BLAH has a length of zero
On the other hand, this script
Code:
#!/bin/bash
for variable in $(grep -i ^banner bleh.sh) ; do
if [ -z "$variable" ] ; then
echo "variable has a length of zero"
elif [ -n "$variable" ] ; then
echo "variable has a non-zero length"
else
echo "variable failed all tests"
fi
done
produces no output
Code:
[user@machine:~]:./bleh.sh
[user@machine:~]:
So, I am going to go with, "The string is not really empty."
Thanks David1357... your first example worked. I will have to call out a variable instead of using a for loop. For curiosities sake, is it possible to do what I wanted with the for loop? I tried changing the ticks like you recommended but that didn't help. Thanks again for your help! Believe or not, I'm learning...
#!/bin/bash
BLAH=$(grep -i ^banner blah.sh)
if [ -z "$BLAH" ] ; then
echo "BLAH has a length of zero"
fi
if [ -n "$BLAH" ] ; then
echo "BLAH has a non-zero length"
fi
produces this output
Code:
[user@machine:~]:./blah.sh
BLAH has a length of zero
On the other hand, this script
Code:
#!/bin/bash
for variable in $(grep -i ^banner bleh.sh) ; do
if [ -z "$variable" ] ; then
echo "variable has a length of zero"
elif [ -n "$variable" ] ; then
echo "variable has a non-zero length"
else
echo "variable failed all tests"
fi
done
produces no output
Code:
[user@machine:~]:./bleh.sh
[user@machine:~]:
So, I am going to go with, "The string is not really empty."
Nope. The problem with that script is that the contents of for is never run. "for" runs an interation for each element, since there are no elements, there's no looping at all.
"test" is actually a separate command, the output of which can used by the if statement. 'test foo' and '[ foo ]' are equivalent commands.
test evaluates the expression given to it, and outputs a true-or-false exit value. "[ -z $variable ]" is really saying "the variable is a null string: T/F?", so if it's null the answer is "true", and false otherwise.
The if statement then reads that exit value to decide what to do next.
Another useful way you can test whether a variable is set or not is to use parameter substitution to define a default value.
Code:
if [ ${variable:-novalue}" = "novalue" ]; then
echo "The variable is unset."
else
echo "The variable is $variable."
If the variable is empty or unset here, then it will default to "novalue", and test will echo true. If the variable contains something, it will echo false, and if will branch to the alternate action.
This won't work correctly, however, if the variable actually contains the string "novalue" (it will test true), so there's also an operator that works the opposite way:
Code:
if [ "${variable:+setvalue}" = "setvalue" ]; then
echo "The variable is $variable."
else
echo "No variable defined."
fi
In this case, if the variable contains something, then the substitute value will be output in place of it, and test will echo true. Otherwise the test reads false.
Last edited by David the H.; 08-29-2009 at 01:01 AM.
Thank you all very much for your help. It makes sense now and also explains why I haven't been able to find any "for" examples the way I tried to do it. Thanks again for taking the time! Once again this forum and it's users don't disappoint!
The problem with that script is that the contents of for is never run.
That was implicit in what I was saying. If the string in the original post was empty, the "for" loop would not even get executed. I was merely providing what you might call a proof.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.