Variable loses value outside loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable loses value outside loop
# 1  
Old 10-03-2017
Hammer & Screwdriver Variable loses value outside loop

Hi,

I am looking for a global variable found_flag to be set a value that can be accessed outside the loop anywhere in the bash shell script.

Code:
 more test.sh
 found_flag=0
 searchdir=/web/bea_apps/applications
  
 find . -type f \! -name tasty.tar | $AWK -F/ '{print $NF}' | while IFS= read -r entry; do
  
 found_flag=0
 find $searchdir -type f -name $entry | grep -v LOGS | while IFS= read -r second_entry; do
  
 mv $second_entry $second_entry.$NOW.bkp
 cp -R $entry `dirname $second_entry`/
 found_flag=1
 echo "Displaying the value of found_flag:$found_flag"
  
 done
  
 echo "Displaying the value of found_flag outside while loop:$found_flag"
  
 done

Output: bash -x test.sh
Code:
 
 + find . -type f '!' -name client.tar
 + /usr/xpg4/bin/awk -F/ '{print $NF}'
 + IFS=
 + read -r entry
 + found_flag=0
 + find /web/bea_apps/applications -type f -name sd.ear
 + grep -v CURRENT
 + IFS=
 + read -r second_entry
 + mv /web/bea_apps/applications/sd.ear /web/bea_apps/applications/sd.ear.10032017.bkp
 ++ dirname /web/bea_apps/applications/sd.ear
 + cp -R sd.ear /web/bea_apps/applications/
  
 + found_flag=1
 + echo 'Displaying the value of found_flag:1'
 Displaying the value of found_flag:1
 + IFS=
 + read -r second_entry
 + echo 'Displaying the value of found_flag outside while loop:0'
 Displaying the value of found_flag outside while loop:0

In the RED BOLD output above I am expecting the found_flag to have the value=1 instead of zero.

Can you please suggest a quick way how I can set the value globally so it reflects outside the loop as well ?

Using sub-shells would be my last priority.

Last edited by rbatte1; 10-03-2017 at 07:35 AM.. Reason: Converted QUOTE tags to CODE tags and removed unnecessary FONT formatting (probably pasted in)
# 2  
Old 10-03-2017
Hi,

by piping the output of find/awk/grep to the loop a subshell is created, thats why the variable in the parent shell is unchanged.
Avoid piping to the loop, bash for example offers process substitution that can be used here:
Code:
$ cat x.sh
echo piping
a=0
ls *.sh |while read i; do
   echo $i
   a=$(( a + 1 ))
   echo inside: $a
done
echo outside: $a
echo =====
echo process substitution
a=0
while read i; do
   echo $i
   a=$(( a + 1 ))
   echo inside: $a
done < <(ls *.sh)
echo outside: $a

$ ./x.sh
piping
hw.sh
inside: 1
x.sh
inside: 2
outside: 0
=====
process substitution
hw.sh
inside: 1
x.sh
inside: 2
outside: 2

# 3  
Old 10-03-2017
Try setting the lastpipe option in bash

Quote:
shopt [-pqsu] [-o] [optname ...]
Toggle the values of settings controlling optional shell behavior.
.
.
.
lastpipe
If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current shell environment.
I think ksh operates like this per default...
# 4  
Old 10-03-2017
I'm curious about this:

Code:
find . -type f \! -name tasty.tar | $AWK -F/ '{print $NF}' | while IFS= read -r entry; do

And then you search for $entry later ... why not just search for tasty.tar?

How about:

Code:
entry=$(find . -name 'tasty.tar' -type f)
if [ ! -f "$entry" ]
then
        echo "tasty.tar not found" >&2
        exit 1
fi

entry=$(basename "$entry")

# 5  
Old 10-04-2017
RedHat

Quote:
Originally Posted by RudiC
Try setting the lastpipe option in bash



I think ksh operates like this per default...
I m trying this option for a solution.

Code:
#!/bin/bash
shopt -s lastpipe

However, I get the below error.

Code:
./run.sh: line 2: shopt: lastpipe: invalid shell option name

My bash version is

Code:
bash-3.2$ bash --version
GNU bash, version 3.2.57(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2007 Free Software Foundation, Inc.


Last edited by mohtashims; 10-04-2017 at 08:24 AM..
# 6  
Old 10-04-2017
That version of bash is 10 years old.

You could try using ksh instead.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Infinite "while" loop subshell loses current date variable

I have a simple script to log network connectivity to a set of systems. However, as expected the date appended to the log never changes because the new variable is lost when the loop starts again. Can someone clue me in on how to get around this issue? #!/bin/bash LOG=/tmp/netlog... (3 Replies)
Discussion started by: woodson2
3 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

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

4. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

5. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

6. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

7. UNIX for Dummies Questions & Answers

FTP loses write permissions

Hi Guys, i have learned today that when you ftp a file with full write permissions (777) to another destination it loses the w options. so a file that was once -rwxrwxrwx(before FTP) is now -rw-r--r-- (after FTP). why does this happen? and is it configurable? Regards, (8 Replies)
Discussion started by: brian112
8 Replies

8. UNIX Desktop Questions & Answers

Grep result loses formatting

I am searching for a string in a file and then redirecting the contents in another file... however the formatting is not preserved.. Can you please help me on this ... (5 Replies)
Discussion started by: blackeyed
5 Replies

9. UNIX for Dummies Questions & Answers

copy loses the text format

Hi I try to copy part of text from one file to another file. My problem is the text in the new file loses all the format. My code is: #!/bin/sh while red line do if then echo "$line" >> ./new_file else break fi done < "./old_file" Is there a way to modify... (3 Replies)
Discussion started by: tiger99
3 Replies

10. AIX

AIX System Loses communication

We run an RS/6000 SP Frame. One of the nodes running (AIX 5), in the frame, seems to run fine for a few weeks and then will no longer communicate with the outside world. I can not telnet to the server or ping the server. This is true for both the ethernet and serial connection. I believe what... (3 Replies)
Discussion started by: k11rh
3 Replies
Login or Register to Ask a Question