How to define a variable In a while loop?
I'm writing a bash package management script and I have a variable, $conflict, that is set to "1" if a system-package conflict exists (the system contains the same files that the package does). Here's the loop where the script checks for conflicts:
Code:
#### CHECK FOR PACKAGE - SYSTEM CONFLICTS |
Shouldn't the
echo $conflict be outside of the while loop ? |
Quote:
|
Since you are breaking out of the loop the echo $conflict that is in the loop will show "0" (since conflict only get set to 1 before breaking out of the loop.
The echo $conflict that is outside of the loop should show "1" if the condition is met. This simple script will show you" Code:
#!/bin/bash |
Perhaps try to "define" the variable before the loop?
|
ya I guess, just do conflict=0 before beginning of while loop.
|
Quote:
Code:
#!/bin/bash Quote:
|
it seems to me that variable keeps its value within the loop...
try "export $conflict " and echo $conflict ( to see the value)... inside the loop |
The problem is the pipe. When you pipe a new instance of the shell is executed as a separate process to receive data on stdin. When you make a variable assignment inside this new pipe process the variable is lost when the process exits at the end of the pipe.
|
Quote:
" -r Make variables read-only. These variables cannot then be assigned values by subsequent assignment statements, nor can they be unset. " |
Here's a short example that demonstrates your situation.
Code:
#!/bin/bash |
A workaround is to put the entire "stuff | while blah; do halb; done" construct inside backquotes and echo $conflict at the end. i.e.
Code:
conflict=`stuff | ( while blah; do whatever; done; echo $conflict )` Code:
echo >&2 "Oh no Mr. Bill!" |
We have the same problem about variable scoping, since I also need to get the presently login user and then, check if corresponding user belongs to a group.
I have wrote several test code with regards to variable scoping outside while loop. But, I could not find any related variable declaration to make the variable value within WHILE loop, I also use export and using extra conditions. The result leads me to conclude that variable within While Loop is only accessible from within the loop itself. So, I decided to save the value to a file then read it back outside the while loop -- it resolves my problem, please see below code if you could find it useful: whoami | while read line; do #if [[ "$userName" = "" ]] ; then userName=$line #fi echo $userName if [ ! $userName == "" ]; then echo "userName=$userName" > ~/getuser.bash break fi done chmod 755 ~/getuser.bash echo "username value outside while loop=$userName" if [ -f getuser.bash ] ; then source getuser.bash fi echo "username value outside while loop in getuser.bash file=$userName" Output: username value outside while loop= username value outside while loop in getuser.bash file=hpal |
@hpal -- You are responding to a thread that is over 5 years old. The OP either solved his problem or gave up long ago.
|
or dead.
but FYI use export: Code:
$ export x=fish |
export passes variables to subprocesses, but that doesn't help getting the data back to the parent:
Code:
$ export x=fish |
Another way to fix the problem is to place the input into a file,
then redirection to have the while loop process the file i.e. ----------------------------------------------------------- Doesn't work due to pipe causing subprocess being created: ----------------------------------------------------------- export conflict=0 cat $filename | while read inline do conflict=1 done echo $conflict # get 0 -------------------- WORKS! -------------------- export conflict=0 while read inline do conflict=1 done < $filename echo $conflict # get 1 ! Since no pipe, no subprocess created, values in while loop retained |
All times are GMT -5. The time now is 07:26 AM. |