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 03-11-2011, 11:01 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
replacing empty values in bash


Hi,

I'm executing the below bash script, but when the m variable doesn't get any value it becomes empty. I don't want that.

Code:
for i in "$@"
do
    m=$(grep TOTAL_MAPS  $i | awk '{ print $2}');
    totalmaps+="<string>"$m"</string>"
done
Sometimes I get the string:
Code:
<string></string>
But I want in that cases return

Code:
<string>0</string>
how can i do that in bash?
 
Old 03-11-2011, 11:02 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Code:
m=0;
for i in "$@"
do
    m=$(grep TOTAL_MAPS  $i | awk '{ print $2}');
    totalmaps+="<string>"$m"</string>"
done
 
Old 03-11-2011, 11:08 AM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
I think that this don't work after the first iteration.
For example, I get the value 9 in the first iteration , and in the 2nd iteration it doesn't exist a value. In that case I'll get

Code:
<string>9</string>
<string></string>
which is wrong.

I think that the solution is to put an if clause, but the problem is that I don't know how can I check if the variable m has content or not.

Last edited by xeon123; 03-11-2011 at 11:10 AM.
 
Old 03-11-2011, 11:23 AM   #4
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
the solution is:

Code:
if [ "$m tr -d ' '" == "" ]
 
1 members found this post helpful.
Old 03-11-2011, 11:50 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Gees ... no need to reinvent the wheel. How about we just use bash builtins:
Code:
[[ $m ]] || m=0
 
Old 03-11-2011, 12:05 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Quote:
Originally Posted by xeon123
Code:
if [ "$m tr -d ' '" == "" ]
This doesn't make sense, did you mean
Code:
if [ "$(echo $m | tr -d ' ')" == "" ]
Quote:
Originally Posted by grail
Gees ... no need to reinvent the wheel. How about we just use bash builtins:
There is a particular builtin just for this:
Code:
${m:=0}
 
Old 03-11-2011, 12:12 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Quote:
There is a particular builtin just for this:
True enough and I thought about it, but based on what had been shown I thought he might get less lost with a test construct
 
  


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 scripting: check for numeric values linuxLuser Programming 11 11-14-2011 11:11 AM
updating a text file (replacing the existing parameters with the new values) rastin_nz Programming 4 11-10-2010 03:10 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 10:57 AM
bash; reading values from a file km4hr Programming 16 07-28-2005 03:07 PM
bash - comparing a variable to several values davee Programming 3 05-05-2003 08:26 AM


All times are GMT -5. The time now is 04:27 PM.

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