LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 11-04-2010, 06:25 PM   #1
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,362
Blog Entries: 3

Rep: Reputation: 86
troubles with bash script tests "-z" and "-n"


(blush) I should be able to sort this out, but I'm stumped.

Here are some commands and the results:
Code:
stuff="xxx xxx"      # the blank is important
echo $stuff
xxx xxx
if [ -z stuff ]; then echo nothing; fi     # no output
if [ -n stuff ]; then echo non-zero; fi    # output
non-zero
Code:
stuff=""
echo "yyy $stuff yyy"     # output has two blanks -- check
yyy  yyy
if [ -z stuff ]; then echo nothing; fi    # no output ???
if [ -n stuff ]; then echo non-zero; fi   # output ???
non-zero
The bash man-page says:
  • -z string -- true if length of string is zero
  • -n string -- true if length of string is non-zero

I thought that stuff="" creates a string of length==0.
There is something obvious here that I am obviously missing.

I'll take my lumps, but I left my flame-suit at the home.
~~~ 0;-Dan

Last edited by SaintDanBert; 11-04-2010 at 06:26 PM.
 
Old 11-04-2010, 06:40 PM   #2
ZaSter
LQ Newbie
 
Registered: Apr 2006
Posts: 9

Rep: Reputation: 0
After setting the variable "stuff", you must from then on refer to it as $stuff. You are simply missing the dollar sign in your tests of "stuff".
 
Old 11-04-2010, 06:44 PM   #3
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
This missing dollar is one thing.
Also, you must enclose the variable in quotes for the test, otherwise empty vars will not work as you might expect:
Code:
if [ -z "$stuff" ];
 
Old 11-04-2010, 07:00 PM   #4
peonuser
Member
 
Registered: Mar 2008
Location: Where ever I am.
Distribution: Various
Posts: 173
Blog Entries: 135

Rep: Reputation: 21
You might look at this page.

http://tldp.org/LDP/abs/html/comparison-ops.html
 
Old 11-05-2010, 06:13 PM   #5
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,362
Blog Entries: 3

Original Poster
Rep: Reputation: 86
I knew that I was [not] doing something silly.

Thanks to all.

Thanks, also, for avoiding the call to rub my nose in this newbie mistake.

Cheers,
~~~ 0;-Dan
 
Old 11-05-2010, 06:17 PM   #6
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,362
Blog Entries: 3

Original Poster
Rep: Reputation: 86
... Follow-up ...

Since the solution involved quoting within scripts, I offer the following from the man bash manual page:
Code:
Enclosing  characters  in  single  quotes preserves the literal value of each character within the quotes.  A single quote may not occur between single quotes, even when preceded by a backslash.

Enclosing characters in double quotes preserves the literal value of
all  characters  within  the quotes,  with the exception of $, `, \, 
and, when history expansion is enabled, !.  The characters $ and `
retain their special meaning within double quotes.   The  backslash
retains  its  special meaning only when followed by one of the 
following characters: $, `, ", \, or <newline>.  A double quote may
be quoted within double quotes by preceding it with a backslash.   
If  enabled,  history expansion  will be performed unless an !  appearing in double quotes is escaped using a backslash.

The backslash preceding the !  is not removed.
Cheers,
~~~ 0;-Dan
 
Old 04-09-2012, 09:13 PM   #7
ZaSter
LQ Newbie
 
Registered: Apr 2006
Posts: 9

Rep: Reputation: 0
Missing dollar signs

Your "if" tests are missing the "$" on your variable names.
 
Old 04-10-2012, 09:26 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
@ZaSter

Please don't re-open old threads for trivial purposes. Especially since you (!) already said exactly the same thing two and a half years ago, in the second post.


This is a classic case of bash pitfall #4, by the way.

http://mywiki.wooledge.org/BashPitfalls
 
  


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 script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 10:35 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM


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

Main Menu
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