LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 12-31-2011, 05:39 PM   #1
sungchoiok
Member
 
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
var=`acpi`

if [ -z $var ]
then
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 sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by sungchoiok View Post
var=`acpi`
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
sungchoiok
Member
 
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
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,653

Rep: Reputation: 569Reputation: 569Reputation: 569Reputation: 569Reputation: 569Reputation: 569
quick an ddirty

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


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
[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


All times are GMT -5. The time now is 06:25 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration