Changing variables in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing variables in a loop
# 1  
Old 03-03-2009
Changing variables in a loop

I'm having a spot of trouble. I'm trying to test three variables for a NULL value in a bash shell script. If a null value is detected in the variable to set it to set the variable to a default value.


here is what I have:

testResponseA=3
testResponseB=
testResponseC=4

for test in "$testResponseA" "$testResponseB" "$testResponseC"; do
echo loop start checking $test
if [ "$test" = "" ]; then
test=999
echo $test

fi
echo loop end
done

echo $testResponseA
echo $testResponseB
echo $testResponseC


I realize that the test is being set and testResponseB is not getting the new value. How do I set the variable with the null value to the default value?
# 2  
Old 03-03-2009
Quote:
Originally Posted by kaltekar
I'm having a spot of trouble. I'm trying to test three variables for a NULL value in a bash shell script. If a null value is detected in the variable to set it to set the variable to a default value.

Code:
: ${testResponseA:=defaultA} ${testResponseB:=defaultB} ${testResponseC:-defaultC}

echo $testResponseA
echo $testResponseB
echo $testResponseC

# 3  
Old 03-03-2009
I don't quite understand. How does this replace the null value in testResponseB without resetting testResponseA of testResponseC?
# 4  
Old 03-03-2009
Quote:
Originally Posted by kaltekar
I don't quite understand. How does this replace the null value in testResponseB without resetting testResponseA of testResponseC?

Read the Parameter Expansion section of the bash man page:

Code:
   ${parameter:=word}
       Assign Default Values. If parameter is unset or null,
       the expansion of word is assigned to parameter. The
       value of parameter is then substituted. Positional
       parameters and special parameters may not be assigned to
       in this way.


Last edited by vgersh99; 03-03-2009 at 06:26 PM.. Reason: fixed BB Code
# 5  
Old 03-03-2009
Thanks, that seems to have done it.
# 6  
Old 03-04-2009
CFAJ's given the best response, but as an exercise here's the answer to the question as you posed it:

Code:
[rick@kangaroo ~]$ cat test.sh
#!/bin/bash

testResponseA=3
testResponseB=
testResponseC=4

for t in testResponseA testResponseB testResponseC
do
echo loop start checking $t

if [[ "${!t}" -eq "" ]]
then
eval $t=999
fi
echo loop end
done

echo $testResponseA
echo $testResponseB
echo $testResponseC
[rick@kangaroo ~]$ ./test.sh
loop start checking testResponseA
loop end
loop start checking testResponseB
loop end
loop start checking testResponseC
loop end
3
999
4
[rick@kangaroo ~]$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Connecting and changing variables in Bash script

#!/bin/bash X=$(</home/cogiz/computerhand.txt) # (3S 8C 2H 6D QC 8S 4H 5H) Y=$(</home/cogiz/topcardinplay.txt) # KS A=( "${Y::1}" ) B=( "${Y:1}" ) for e in ${X}; do if ]; then # searching for valid cards K,S or 8 ... (0 Replies)
Discussion started by: cogiz
0 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Writing a loop to changing the names of files in a directory

Hi, I would like to write a loop to change the names of files in a directory. The files are called data1.txt through data1000.txt. I'd like to change their names to a1.txt through a1000.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

3. Shell Programming and Scripting

Changing variable name in for loop

Hi All please help if possible. I am a Unix novice. I have a similar question to the one posted by yonderboy at about a year ago. However his solution does not work for me. The pseudo code for my problem is as follows: for fund in 1 2 3 4 if (FTP is successfully) then FILE_SENT_fund... (2 Replies)
Discussion started by: Seether
2 Replies

4. Shell Programming and Scripting

Need help in for loop with 2 variables

Hi, I need help on for loop need to add domain and IP In domain list 1.com 2.com 3.com In Ip list 1.1.0.1 1.2.0.1 1.3.0.1 1.com 1.1.0.1 2.com 1.2.0.1 3.com 1.3.0.1 I need to excute this command (4 Replies)
Discussion started by: ranjancom2000
4 Replies

5. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

7. Shell Programming and Scripting

Changing directories using variables.

I can't seem to solve this problem. :mad::mad: Please assist. Thanks! #!/bin/bash UserDir="$(echo ~$1)" echo "Changing directory with variables" cd "$UserDir" echo "Changing directory without variables" cd ~pearsn pwd Output: pearsn$ sh -x ./test.bash pearsn ++ echo '~pearsn' +... (6 Replies)
Discussion started by: twinturbos52
6 Replies

8. Solaris

Changing default 'FORMAT' variables.

In FORMAT->ANALYZE->SETUP there's a couple variables you can set for the various functions. However, everytime I exit format it reverts back to the defaults. Is there a file I can edit somewhere to change these default settings? (0 Replies)
Discussion started by: cheetobandito
0 Replies

9. Shell Programming and Scripting

changing filenames in a directory to a number within a loop

hey guys. i'm new to shell scripting but not new to programming. i want to write a script that will take all the files in the current directory that end with a particular filetype and change all their names to a number in order. so, it would take all the jpg files and sort them in alphabetical... (30 Replies)
Discussion started by: visitorQ
30 Replies

10. Shell Programming and Scripting

changing environment variables

hi friends, i'm new to shell scripting,can i know how to change the environment variables without altering anythng in .bash_profile as the change in it is for a specific user but i want the change to be available to every user who logs in. bye. (1 Reply)
Discussion started by: amit4g
1 Replies
Login or Register to Ask a Question