Assigning inside for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning inside for loop
# 1  
Old 07-25-2008
Assigning inside for loop

If I have 3 variables and I want to check if any of these is null. If one of them is null then it should be assigned a value of 0.I have the following code below.
The output should be 0 is A, 11 is B, 33 is C


Code:
$a=
$b=11
$c=33
$echo $a $b $c
$11 33

for i in "${a}" "${b}" "${c}";
do
 if [ "${i}" = "" ];then
   Assign the value to 0
 fi
done

echo After
echo $a is A, $b is B, $c is C

# 2  
Old 07-25-2008
Rather than use a loop and get into eval or nameref why not keep it simple and do something like the following
Code:
[[ -z $a ]] && a=0
[[ -z $b ]] && b=0
[[ -z $c ]] && c=0

# 3  
Old 07-25-2008
Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning values to 2 variables within for loop

Hi All, Is it possible to grep for two files and assign their names to two separate variables with for loop? I am doing the below currently: if then for fname in $( cd $dirA ; ls -tr | grep "^Ucountry_file$") do InFile=$dirA/$fname ... (4 Replies)
Discussion started by: swasid
4 Replies

2. UNIX for Advanced & Expert Users

couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '' | wc -c using this command i can count the no of times a number from 0-9 occurs in the string "hello123" but how do i save this result inside a variable? if i do x= echo "hello123" | tr -dc '' | wc -c that does not work...plz suggest..thanks (3 Replies)
Discussion started by: arindamlive
3 Replies

3. Shell Programming and Scripting

Need help in assigning output of n commands to n variables automatically inside a for loop

Please help me to automatically assign the output of awk command to the variables cs3, cs4, cs5 and cs6 using a for loop. The below code is not working. for i in 3 4 5 6 do cs$i=`awk -F"|" 'BEGIN{sum=0}{sum=sum+$'$i'}END{printf("%d\n", sum)}' css` done echo $cs3 $cs4 $cs5 $cs6 (9 Replies)
Discussion started by: thulasidharan2k
9 Replies

4. Shell Programming and Scripting

problem in assigning substr to a variable inside awk

Hi All, I have a fixed-width datafile from which i need to extract value/string starting from some position to the specified length in each of the lines. awk '{print substr($0,x,y)}' datafile --- is working fine but awk 'BEGIN{a=0}{a=substr($0,x,y);print $a}' datafile ---is giving... (3 Replies)
Discussion started by: loggedin.ksh
3 Replies

5. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

6. Solaris

For loop variable not assigning

Hi , i am using below code print the PATH value inside for loop but it's printing just PATHD & not the original value which i defined. #!/usr/bin/bash PATHD1=/spp/sp/LIVE export PATHD1 PATHD2=/spp/sp/TEST export PATHD2 j=2 for (( i = 1 ; i <= j ; i++ )) do PATH_DIR=PATH$i echo PATHD$i... (2 Replies)
Discussion started by: panneer76
2 Replies

7. Shell Programming and Scripting

For Loop and assigning Value using Sed

Hi, Please help me writing for loop to fetch data and store in variable from a text file which contains data as below: 12 46 56 5466 111 There are 40 lines of data...!!!! I jus need all data from line no 4 to 40 ie data for 4,6,8......40. (2 Replies)
Discussion started by: fidelis
2 Replies

8. Solaris

Assigning Temp IP inside zones

I want to assign ip to a zone , but i dont want that ip to be retained when the zone is rebooted , is there a way to do it ? Thx (7 Replies)
Discussion started by: skamal4u
7 Replies

9. 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

10. Shell Programming and Scripting

Assigning values to an array via for/while loop

I need to do something like this: for i in 1 2 3 4 5; do arr=$(awk 'NR="$i" { print $2 }' file_with_5_records) done That is, parse a file and assign values to an array in an ascending order relative to the number of record in the file that is being processed on each loop. Is my... (2 Replies)
Discussion started by: fiori_musicali
2 Replies
Login or Register to Ask a Question