Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This 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!


  Search this Thread
Old 12-31-2011, 05:39 PM   #1
Registered: Oct 2010
Posts: 30

Rep: Reputation: 1
Scripting Help--Check empty string condition (not null, but empty!)

hello. This is the part that I'm having trouble with:

#put result of command acpi into variable var

if [ -z $var ]
etc, etc, etc

when I run it, it says "Line 4, too many arguments."

There are instances when the command acpi does not return anything and I'm checking for that.

I tried googling it, but every tutorial says "use -z".

thank you.
Old 12-31-2011, 06:16 PM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
1) "Too many arguments", and probably your other problems, are due to Bash Pitfall #4.

2) When using bash or ksh, it's recommended to use [[...]] for string tests, and ((..)) for numerical tests. They don't suffer from all the weaknesses of the old-style [..] test.

3) $(..) is highly recommended over `..`.

4) Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Don't use fancy font faces or quote tags.

Last edited by David the H.; 12-31-2011 at 06:18 PM.
1 members found this post helpful.
Old 12-31-2011, 06:28 PM   #3
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Originally Posted by sungchoiok View Post
if [ -z $var ]
By not quoting $var it will be subject to word splitting. If there are any whitespace characters in $var then -z would find two arguments instead of one.

I agree with everything David the H. wrote, so be sure to read the links he gave you. Your code will be better for what you learn. If you don't understand the information from those pages, then ask specific questions.
Old 12-31-2011, 09:29 PM   #4
Registered: Oct 2010
Posts: 30

Original Poster
Rep: Reputation: 1
sorry for not using the [code] tags, and thank you for the generous answers.
Old 01-01-2012, 04:46 PM   #5
Senior Member
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 2,731

Rep: Reputation: 1130Reputation: 1130Reputation: 1130Reputation: 1130Reputation: 1130Reputation: 1130Reputation: 1130Reputation: 1130Reputation: 1130
quick an ddirty

Your example would have worked if you test has not been
 [ -z $var ]
but had been instead
 [ -z "$var" ]
LEarning more is good, but doing more with less is good also.


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
[SOLVED] HOSTNAME variable is being an empty string alexa-lol Programming 1 05-20-2011 08:15 AM
[SOLVED] replace string with empty string of dir path ted_chou12 Linux - Newbie 3 04-02-2011 07:43 AM
How to use If Else condition when returns empty data? dwarf007 Programming 2 04-27-2010 04:00 AM
Bash Scripting Empty file john8675309 Linux - Software 2 08-08-2007 05:05 PM
Overflow on /dev/null, please empty the bit bucket. little_ball Slackware 17 01-15-2004 08:34 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration