Setting a variable within if block


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting a variable within if block
# 1  
Old 08-18-2009
Setting a variable within if block

Hi,

i have a variable which i would like to set inside an if block

for example
Code:
IS_VAR=0
if [ "true" = "true" ]
then
 IS_VAR=1
fi
echo IS_VAR

the last echo statement gives 0.So setting variables in the if block doesnt have effect outside the block?Is there any workaround for this?

Thanks ,
Padmini

Last edited by Franklin52; 08-18-2009 at 10:01 AM.. Reason: Please use code tags!
# 2  
Old 08-18-2009
nothing like that.. $ is missing from your last echo statement
Code:
home/vidya_perl> cat vv
IS_VAR=0
if [ "true" = "true" ]
then
IS_VAR=1
fi
echo $IS_VAR
home/vidya_perl> vv
1

# 3  
Old 08-18-2009
Hi,

i use echo $IS_VAR only.

and the example is slightly modified here

its like,
I
Code:
S_VAR=0
ls | while read input
do
if [ $input = "hi" ]
then
  IS_VAR=1
  echo $IS_VAR
  break
fi
done
echo $IS_VAR

for the first echo , i get 1 and for the second i get 0.

Last edited by Franklin52; 08-18-2009 at 10:02 AM.. Reason: Please use code tags!
# 4  
Old 08-18-2009
Quote:
Originally Posted by padmisri
Hi,

i use echo $IS_VAR only.

and the example is slightly modified here

its like,
IS_VAR=0
ls | while read input
do
if [ $input = "hi" ]
then
IS_VAR=1
echo $IS_VAR
break
fi
done
echo $IS_VAR

for the first echo , i get 1 and for the second i get 0.
Yes,looks the initialization inside the while block has no effect outside the while block.
But if you remove while then output 1 and 1.
Code:
#!/bin/sh

IS_VAR=5
input="hi"

if [ $input = "hi" ];then    
    IS_VAR=1
    echo $IS_VAR
fi

echo $IS_VAR

# 5  
Old 08-18-2009
The pipe starts a new shell (child) so the assigned value of the variable is lost after the loop.

Try something like this:

Code:
IS_VAR=0

list=$(ls)

for input in $list
do
if [ $input = "hi" ]
then
  IS_VAR=1
  echo $IS_VAR
  break
fi
done
echo $IS_VAR

Regards
# 6  
Old 08-18-2009
Quote:
Originally Posted by Franklin52
The pipe starts a new shell (child)
The script does work for me...

Code:
> cat Test
#!/bin/sh

touch hi
echo Outside $PPID $$
ls | while read input
do
if [ $input = "hi" ]
then
  echo Inside $PPID $$
  IS_VAR=1
  echo Inside $IS_VAR
  break
fi
done
  echo Output $PPID $$
echo Outside $IS_VAR
> ./Test
Outside 13426702 2252968
Inside 13426702 2252968
Inside 1
Output 13426702 2252968
Outside 1

Which OS are you using, Padmini?

Edit: Wow... It works on AIX but not on Linux or Solaris. I do apologise.

Last edited by Scott; 08-18-2009 at 10:37 AM..
# 7  
Old 08-18-2009
It doesn't work in ubuntu.
This is the output:
Code:
Outside 9559 9803
Inside 9559 9803
Inside 1
Output 9559 9803
Outside

Are you using AIX?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

setting the PS1 variable

Hi i'm new to unix, can anyone assist in me setting the PS1 variable in unix (3 Replies)
Discussion started by: user@123
3 Replies

2. Shell Programming and Scripting

Help with setting a variable!

I am working within a while loop and i am trying to set a variable that will read out each count of the files. the problem is the count variable i have set up gives me a total and not the individual count of each file. in the data area there is 4 abc.dat and 1 def.dat. how can i do this??? ... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

3. UNIX for Dummies Questions & Answers

setting a variable

In my script, I have the following command.... du -sk `ls -ltd sales12|awk '{print $11}'`|awk '{print $1}' it returns the value 383283 I want to modify my script to capture that value into a variable. So, I try doing the following... var1=`du -sk `ls -ltd sales12|awk '{print... (5 Replies)
Discussion started by: tumblez
5 Replies

4. Shell Programming and Scripting

Setting variable

How do you set a varible with information that contains a string and also another variable? For example: subject="Attention: $name / This $type needs your attention" The $xxxx are of course other variables that I instantiated earlier. Is it like Java where you have to use double quotes and... (1 Reply)
Discussion started by: briskbaby
1 Replies

5. UNIX for Dummies Questions & Answers

Setting a variable (need syntax help)

I need some syntax help (working in a bash shell) I have a variable which is a filename with an extension, and I need to create another variable with the same name but a different extension To explain, the input file should be called something like "filename.L1" and the output file should be... (1 Reply)
Discussion started by: Slanter
1 Replies

6. Shell Programming and Scripting

Variable setting help please

L=0 cat test.sh | while read line do L='expr $1 + 1' echo $L done echo $l >>> the echo $L at the end produces 0 but i actually want it to produce the number of lines - any idea why this is happening? (16 Replies)
Discussion started by: penfold
16 Replies

7. UNIX for Dummies Questions & Answers

setting PS1 variable

Hi, I am trying to set my current prompt with the current directory iam working on by $PS1=$PWD but it is blank. please help. (4 Replies)
Discussion started by: papachi
4 Replies

8. UNIX for Dummies Questions & Answers

Setting a variable

I want to set a variable to be any number of dashes. Rather than doing the following: MYVAR="------------------" I'd like to be able to set the variable to, say, 80 dashes but don't want to have to count 80 dashes. Is there a way to do this? (2 Replies)
Discussion started by: photh
2 Replies

9. Programming

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies

10. UNIX for Dummies Questions & Answers

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies
Login or Register to Ask a Question