How do I change a variable to something only if it's empty?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I change a variable to something only if it's empty?
# 1  
Old 08-03-2011
How do I change a variable to something only if it's empty?

I feel like it is just a matter of using the $ operators correctly, but I can't seem to get it...

Code:
hostname="network"
ip="192.168.1.1"
netmask=""

variables=( $hostname $ip $netmask  )

for var in ${variables[@]}
do

      if [ "$var" == "" ]; then
                $var="--"
       fi
done

echo hostname: $hostname
echo ip: $ip
echo netmask: $netmask

Basically I have these variables with values in them already and I want to check if any of them are empty. If they are, I want to replace it with "--".

so output should be:

hostname: network
ip: 192.168.1.1
netmask: --
# 2  
Old 08-03-2011
Code:
mTst=$(echo ${mTst} | sed 's/^$/--/')

# 3  
Old 08-03-2011
that works, but if I use that in the for loop, there is an error..

Code:
for var in ${variables[@]} 
do       
$var=$(echo ${var} | sed 's/^$/ --/') 
done

I can't do var= ... because that'll just create a new variable
# 4  
Old 08-03-2011
You can test to see it is "empty" be sure to use quotes around the variable name when testing it:

Code:
# if the variable has been set but is now "empty"
if [ -z "$var" ] ; then
  # do something
fi
# or parameter substitution, when the variable is not set
${var:-"default value for var"}

I dunno exactly which works for you.
# 5  
Old 08-03-2011
What about this, maybe:
Code:
#!/bin/bash

hostname="network"
ip="192.168.1.1"
netmask=""

variables=( $hostname $ip $netmask  )

echo hostname: ${variables[0]:=--}
echo ip: ${variables[1]:=--}
echo netmask: ${variables[2]:=--}

exit 0

Or this, if you do not need the array:
Code:
#!/bin/sh

hostname="network"
ip="192.168.1.1"
netmask=""

echo hostname: ${hostname:=--}
echo ip: ${ip:=--}
echo netmask: ${netmask:=--}

exit 0


Last edited by tukuyomi; 08-03-2011 at 05:54 PM.. Reason: Added second code
# 6  
Old 08-03-2011
I think the only one that worked was

Code:
hostname=$(echo ${hostname} | sed 's/^$/--/')
ip=$(echo ${ip} | sed 's/^$/--/')
netmask=$(echo ${netmask} | sed 's/^$/--/')

Thanks!
# 7  
Old 08-03-2011
To display the spaces, place the entire line between double quotes:
Code:
echo "hostname: $hostname"
echo "ip: $ip"
echo "netmask: $netmask"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue using empty variable in grep command

Hi, I'm writing a shell script and trying to grep a variable value, it works fine as long as there is a value in /tmp/list.out which is captured in $DSK but sometimes the file tends to be empty and that is where I'm having an issue while using grep which returns nothing. I know I can use something... (15 Replies)
Discussion started by: mbak
15 Replies

2. Shell Programming and Scripting

How to check whether a variable is empty or contains some value?

hi, i want to check whether a a variable contains some value or is empty in a shell script. so if the variable contains some value i want to do some job and if the variable doesnt contain any value then i need to skip that job. here is a sample script read_filenames.sh contains ... (5 Replies)
Discussion started by: Little
5 Replies

3. Red Hat

Compare Empty Variable

Hello All, I am running the below code in my script. I want if jk is empty nothing should be appened to the file total_usage. but apparently its not happening.Kindy let me know how to do it. ################################### jk=`ps auxf |grep -w $inputline|tr -s " "|cut -d... (0 Replies)
Discussion started by: ajaincv
0 Replies

4. Shell Programming and Scripting

read variable from file show empty

Hi Folks, I am trying to read input from single line from a file and pass the read variable to one of the commands. However when I run the script it keeps the variable to be empty. I can however echo the variable ( but why it is empty when it goes to the command). Any help will be... (6 Replies)
Discussion started by: bhadu
6 Replies

5. Shell Programming and Scripting

Change existing variable value only user enters non-empty string.

I haven't checked any installation script to see how this is done.. But I could not even do following simple task. How do I Change existing variable value only when user enteres non-empty string. ? #!/usr/bin/ksh uid="scott" # Assign new value user enters to uid, else leave it... (7 Replies)
Discussion started by: kchinnam
7 Replies

6. Shell Programming and Scripting

How to check a variable for empty and a newline

I have a variable with a new line. I want to check this variable for empty or a new line Can anyone advise (4 Replies)
Discussion started by: Muthuraj K
4 Replies

7. Shell Programming and Scripting

How to check the variable is empty with spacing

How to check the variable is empty or not? aaa=" " how to check aaa variable is empty or just spacing? If only spacing inside.. it will asume it is empty. some are 6 spacing, or 8 spacing.. as long as variable is empty with spacing.. anyone can help me? (2 Replies)
Discussion started by: ryanW
2 Replies

8. Shell Programming and Scripting

Change output if file is empty

I'm very new to writing scripts, so here is my problem...I have the following code already written (in perl) system "rm file2"; open(FILE2, ">file2"); open(MYINPUTFILE, "file"); while(<MYINPUTFILE>) { my($line) = $_; chomp($line); print file2 "$line\n"; print... (2 Replies)
Discussion started by: ddrew78
2 Replies

9. UNIX for Dummies Questions & Answers

Trying to capture empty variable.

sorry for the title of this thread- I figured out how to capture empty variable by using- if then ........ This thread originally had two parts in it (one for capturing empty variable and the other to get the date time in 14 char format). Once I figured out the solution for the first part... (6 Replies)
Discussion started by: radhika
6 Replies

10. Shell Programming and Scripting

Empty Variable

I am trying to assign list of files that created that day to a variable 'filename' filename=`ls -ltr |grep "Mar 6" | awk '{print$9}'` If there are no files created that day, my script just hangs, i need help with some login here. i am also trying to figure out a way to look for files that... (3 Replies)
Discussion started by: gundu
3 Replies
Login or Register to Ask a Question