Unable to set Global variable outside while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to set Global variable outside while loop
# 1  
Old 05-09-2015
Hammer & Screwdriver Unable to set Global variable outside while loop

Below is my code:
Code:
 
count=0
if 
...
...
else
...
find * -prune -type d | sort -r -n |  while read d; do
count=1
 if [ "$count" == "1" ]; then
 echo "Count1:$count"
 ...
 ...
 break 2;
 fi
...
done 
...
fi
echo "Count2:$count"
if [ "$count" != "1" ]; then
echo "Problem"
fi

Output:
Count1:1
Count2:0
Problem

I did not expect "Problem" to be printed as i was expecting Count2:1.
I tried putting "( while read d; do" but i am not sure where should i close ")" to get it working.
Where am i going wrong and how should i resolve this.

Last edited by mohtashims; 05-09-2015 at 05:35 AM..
# 2  
Old 05-09-2015
Deja-vu: Question around Global Variable
  1. count is not changed anywhere (visible)
  2. You parse entries d, but dont use them.
  3. HERE, Problem is printed because $count NOT equals 1 (as it still is set to 0 and 0!=1, so it is printed).

Same question as in your other thread, what do you want to achieve?
# 3  
Old 05-09-2015
Quote:
Originally Posted by sea
Deja-vu: Question around Global Variable
  1. count is not changed anywhere (visible)
  2. You parse entries d, but dont use them.
  3. HERE, Problem is printed because $count NOT equals 1 (as it still is set to 0 and 0!=1, so it is printed).
Same question as in your other thread, what do you want to achieve?
1. count is changed and set to 1 just below the find statement and I wish that value to be taken over to the last if condition.

2. -d is used but its not relavent to the problem so i did not share the entire code. Let me know if you need that to debug the issue.

3. My expectation / need is $count SHOULD equals 1

Last edited by mohtashims; 05-09-2015 at 06:09 AM..
# 4  
Old 05-09-2015
The count=1 in your code is set in the while loop is in the last command in a pipeline. In the shell you're using the last command in a pipeline is run in a subshell. Variables set in a subshell are not available when you get back to the parent shell. So, everything in your script from where count is updated to where you use that updated value need to be in the same subshell. So...:
Code:
count=0
if 
...
...
else
...
find * -prune -type d | sort -r -n | ( while read d
    do
     count=1
     if [ "$count" == "1" ]; then
     echo "Count1:$count"
     ...
     ...
     break 2;
     fi
     ...
    done 
    ...
    fi
    echo "Count2:$count"
    if [ "$count" != "1" ]; then
    echo "Problem"
    fi
)

Note that break 2 breaks out of two enclosing loops and there is only one enclosing loop in this code. If you are trying to break out of a loop that contains the pipeline you've shown us, it won't work because you can't break out of a loop in a parent environment, only out of loops in the current (subshell) shell execution environment.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-09-2015
Hammer & Screwdriver

I tried the '()' suggestion you gave however, i get this error and the script wont run.
script.sh: line 69: syntax error near unexpected token `fi'
script.sh: line 69: `fi '


Even if i remove the break statement i see the above error while running the script.

Also, I need to break from the If statement as well as the while loop, hence I needed break 2.

Can you help me fix this ?

Last edited by mohtashims; 05-09-2015 at 02:01 PM..
# 6  
Old 05-09-2015
Quote:
Originally Posted by mohtashims
I tried the '()' suggestion you gave however, i get this error and the script wont run.
script.sh: line 69: syntax error near unexpected token `fi'
script.sh: line 69: `fi '


Also, I need to break from the If statement as well as the while loop, hence I was using break 2 and that may be causing the above error.

Can you help me fix this ?
The break n command breaks out of n enclosing for, until, or while loops. The number of if statements present inside those loops do not matter (no matter how deeply nested in if statements the break is).

I can't help you fix a syntax error on line 69 of a script when I have seen less than twenty lines of your script and large portions of that are:
Code:
...

Obviously, the closing parenthesis I added has to go after the done that ends your while loop. It may need to be several lines after the done if you use $count outside the while loop.
# 7  
Old 05-09-2015
In that case let me just post my case and what i m trying to do.

I m in a directory that has a few sub directories.

I need to grep for a string in all files in all sub directories starting with the sub directory with the latest time stamp.

If grep string is found i need to break off and stop the search [this is why i was using break].

If the desired string is not found I need to grep for the string in the other sub directories.

After the process completes I should display the results of the grep if the desired search string has been found in any of the files or else Display a message that "No records Found"

I hope you can give me an idea as my expertise did not help.

Last edited by mohtashims; 05-09-2015 at 04:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to compare to a previous value of a variable in a while loop for a file

Hello All, I'm working on a script that will actually read a file consisting of data like the below:(ReportID,Sub_reportID,Sub_reportName) 1,1,ABC 1,2,DEF 1,3,GHI 2,1,JKL 2,2,MNO 3,1,PQR I want to read the Sub Report details for a Report_ID using while loop and write these values into... (6 Replies)
Discussion started by: venkat_reddy
6 Replies

2. Programming

Global variable in for loop (BASH)

Hello, I'm trying to read the variable "pause" from a for loop without luck. The function is dependant on the outcome of the test within the loop. If i run this, pause is always 0 within the function. Any ideas? Thanks. pause=0 users=1 (for (( ; ; )) do speed=`cat speed.log` ... (7 Replies)
Discussion started by: shadyuk
7 Replies

3. Solaris

Netra X1 LOM: Unable to change any variable via the "set" command

I'm posting here as it didn't seem quite right in the hardware section (as it's LOM commands). My apologies if I have that wrong though :) I've finally gotten round to configuring the LOM on my Netra X1, but I can't get it to change the hostname via the "set" command: lom>show hostname... (2 Replies)
Discussion started by: Smiling Dragon
2 Replies

4. Shell Programming and Scripting

Unable to set my PATH variable

Hello All, Hope you can understand my problem from the below code. $ cat ~/.profile PS1=`whoami`@`hostname`':$PWD $ ' export PATH="$PATH:.:/logarchive/utility/util:/usr/sbin:" $ echo $PATH /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:.:/usr/sbin: $ echo $SHELL /usr/bin/ksh ... (6 Replies)
Discussion started by: sathyaonnuix
6 Replies

5. Shell Programming and Scripting

use variable to set the range of a for loop

Hi; For sure there's an easy answer to this one that I am not finding.. I first set a variable, say b1a:] max=5 then I want to use max to set the range for a for loop like so (it should run for i in 1:5) b1a:] for i in {1..$max}; do echo $i; done {1..5} I would like the output... (2 Replies)
Discussion started by: jbr950
2 Replies

6. UNIX for Dummies Questions & Answers

set global rm command

Hi, My team does not have root access however many a times my members logon to the unix server and fire the rm command mistakenly on wrong folder and later realise that they have made a mess. The rm -i option prompts for a confirmation before actually deleting the files/folders. I want to... (5 Replies)
Discussion started by: shifahim
5 Replies

7. Shell Programming and Scripting

Unable to access variable outside loop

I am unable to access the value set inside the loop from outside loop . Thought of taking this to forum , I had seen other replies also , where a pipe takes the execution to another shell and mentioned thats the reason we do not get the variable outside loop . But I am getting an issue and I am... (1 Reply)
Discussion started by: Armaan_S
1 Replies

8. Shell Programming and Scripting

unable to access a variable not local to a while loop

I have a while loop like this cat ${filename} | while read fileline do ... done I need to access a variable value $newfile inside this while loop How will i do that?? (6 Replies)
Discussion started by: codeman007
6 Replies

9. Shell Programming and Scripting

set variable in while loop?

Dear All, Can anyone advise why this script isn't run as expected? =========================== status=0 cat /etc/passwd | while read line; do status=1 done echo $status =========================== it always return 0 , but not 1. why? anything wrong? Thanks. (1 Reply)
Discussion started by: tiger2000
1 Replies

10. Shell Programming and Scripting

global variable not being set

In ksh I thought a global variable was any variable in a script or function that did not have the typeset command. I have a global in my calling script which I increment in a function, but the value does not change in the calling script. Here is the code: function f_open_log { typeset -r... (5 Replies)
Discussion started by: robotball
5 Replies
Login or Register to Ask a Question