Variable problem in for loop with if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable problem in for loop with if statement
# 1  
Old 06-17-2008
Question Variable problem in for loop with if statement

Hi,

Again a little problem.
Do not understand good why an empty string is not detected.
Here is the program:

#!/bin/ksh

APR=`date | grep Apr | awk '{print $2$3}'`
MAY=`date | grep May | awk '{print $2$3}'`
JUN=`date | grep Jun | awk '{print $2$3}'`

echo "Variable Apr has value: $APR"
echo "Variable May has value: $MAY"
echo "Variable Jun has value: $JUN"
echo

for ITEM in $APR $MAY $JUN
do
if [[ "${ITEM}" = "" ]]
then
echo "The checked item has no value."
else
echo "The checked item has value ${ITEM}"
fi
done

Output:

Variable Apr has value:
Variable May has value:
Variable Jun has value: Jun17

The checked item has value Jun17

I expected it a little different.
Maybe this is an issue:

# echo $MAY | od -c
0000000 \n
0000001

Thanks in advance,

ejdv
# 2  
Old 06-17-2008
Hi ejdv,

what is your expected out put from the manipulated date values.

Please past your expected value as well as the sample input date value aslo.
it would be easy to send the solutions.

Regards,
MPS
Bangalore
# 3  
Old 06-17-2008
You just need to wrap items in double quotes.
Better to use also curly brackets:

Code:
for ITEM in "${APR}" "${MAY}" "${JUN}"

output:
Code:
Variable Apr has value:
Variable May has value:
Variable Jun has value: Jun17

The checked item has no value.
The checked item has no value.
The checked item has value Jun17

# 4  
Old 06-17-2008
Hi,

# date
Tue Jun 17 11:31:11 GMT 2008

Expected output:

Variable Apr has value:
Variable May has value:
Variable Jun has value: Jun17

The checked item has no value.
The checked item has no value.
The checked item has value Jun17

I do not understand good why $APR and $MAY do not show up has being empty strings.

ejdv
# 5  
Old 06-17-2008
MySQL

Quote:
Originally Posted by ynir
You just need to wrap items in double quotes.
Better to use also curly brackets:
[/CODE]
Thanks, that works wonders.
Are you able to explain what is does exactly ?

Just because $ITEM, ${ITEM} and "${ITEM}" give the same value for the variable.
And I would like to learn from it :-)

ejdv
# 6  
Old 06-17-2008
$APR and $MAY are translated into nothing, therefore the original loop you wrote is translated as:
for ITEM in 'Jun17'

while when you use double quotes it is translated as:
for ITEM in '""' '""' '"Jun17"'

you can run your script with bash -x to learn how the shell behaves.
# 7  
Old 06-17-2008
Thanks for explaining.
And indeed I had to use the -x option to see what went on.
But I doubt if I would have find the solution :-)

ejdv
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Awk: problem for loop through variable

Hi, input: AAA|1 my script (the function is just an example): gawk 'BEGIN{FS=OFS="|"} function repeat(str, n, rep, i){ for(i=1; i<=n; i++) rep=rep str return rep } { variable_1=repeat($1,$2) variable_2=repeat($1,$2+1) variable_3=repeat($1,$2+3) ... (5 Replies)
Discussion started by: beca123456
5 Replies

2. Shell Programming and Scripting

While loop within if statement

Hi, I'm a rookie who is trying to learn this stuff. What I need help with is putting together a non complicated "while" loop within the below "if" statement. I also need the while loop to keep looping until the user types a key to end the loop. Please reveal the proper insertion points. Thank... (4 Replies)
Discussion started by: jefferj54
4 Replies

3. Shell Programming and Scripting

String variable concatenation through loop problem

Hi Team!! Please can anyone tell me why the following line does not work properly? str3+=$str2 it seems that str3 variable does not keep its value in order to be concatenated in the next iteration! Thus when i print the result of the line above it returns the str2 value What i want to do is to... (8 Replies)
Discussion started by: paladinaeon
8 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. Shell Programming and Scripting

Problem assigning cmd output to variable then using in IF statement

Hi, I'm using the bourn shell on a Sun Solaris Unix system. I am relatively new to UNIX scripting so please bear with me... I'm having a couple issues: 1) I need to have a variable $FSIZE set with the output of a command each time the script runs. (the command looks for a file and... (8 Replies)
Discussion started by: dqrgk0
8 Replies

6. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

7. Shell Programming and Scripting

problem with for loop and a variable with spaces...Hi

Hi there, I don't understand the following behavior: toto:~$ for word in un "deux trois"; do echo $word; done un deux trois toto:~$ sentence='un "deux trois"' toto:~$ for word in $sentence; do echo $word; done un "deux trois" toto:~$ sentence="un 'deux trois'" toto:~$ for word in... (10 Replies)
Discussion started by: chebarbudo
10 Replies

8. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

9. Shell Programming and Scripting

Problem with variable expension using for loop.

Hi, I have a problem with expending variables when used in a for loop: #!/bin/ksh VAR1=aaa VAR2=bbb VAR3=ccc for ITEM in VAR1 VAR2 VAR3 do echo "${ITEM}" done This gives: VAR1 VAR2 VAR3 (2 Replies)
Discussion started by: ejdv
2 Replies

10. UNIX for Dummies Questions & Answers

if statement in a while loop

#!/usr/bin/ksh echo Please enter while read n do echo $n >> datafile done question: How can I enject an if statement that if the users enter 0 (zero) the program will exit? this is what I have but not working #!/usr/bin/ksh echo Please enter number while read n do if $n=0 then... (2 Replies)
Discussion started by: bobo
2 Replies
Login or Register to Ask a Question