Can't read variable that was stored from within a while loop, when out of the while loop
The pipe operator creates a subshell, see BashPitfalls and BashFAQ. Solution: Don't use cat
, it's useless anyway.
#!/bin/bash
postPriority=0
while read namesInFile
do
postPrioity=500
echo "wCan't read variable that was stored from within a while loop, when out of the while loop How to use variable in a while loop outside of the loop in java? Why can't I deee ---> $postPrioity <--- 1"
done < /files.txt
echo "wCan't read variable that was stored from within a while loop, when out of the while loop How to use variable in a while loop outside of the loop in java? Why can't I deee ---> $postPrioity <--- 2"
How to use variable in a while loop outside of the loop in java?
Put your if statement inside your for loop, but use a break:
while...
if(sCurrentLine.contains(pwd)){
System.out.println("password accepted");
break;
}
This breaks out of the for loop, so that once the password is found, it stops looping. You can't really move that if-check outside of the loop, because you want to check every line for the password until it is found, right?
If you do that, you don't need to move the sCurrentLine
variable out of the loop. You also might want to doublecheck if you want to do sCurrentLine.equals(pwd)
instead of using contains
.
Why can't I declare this variable within while loops?
Here's a visual picture of your situation:
For your first code, quarters
is declared inside a while loop, so it cannot be referenced from an outside scope.
However, for your second code, quarters
is declared within the method, so it can now be referenced within that scope.
How to store a variable from a reader outside of a while statement
Use global variable For exp....
string bin_type=string.Empty;
while (reader.Read()) {bin_type += reader.GetString(0);}
Perl, using variable from within While loop outside of the loop?
You declare variable test inside the loop, so it scope is the loop, as soon as you leave the loop the variable is not longer declared.
Add my $test;
just between $i=1
and while(..)
and it will work. The scope will now be the entire sub instead of only the loop
How to access a variable being updated in a while loop
You can try the following. First, as there's and infinite loop, importing file1 will block, so you should run the loop in a thread. And second you can wrap the integer being incremented in a list (or any other kind of objects), so you can use the reference to its current value (otherwise you will be importing a value not a reference):
# file1
import time
import threading
x = [0]
def update_var(var):
while True:
var[0] += 1
time.sleep(2.0)
threading.Thread(target=update_var, args=(x,)).start()
# file2
from file1 import x
print x[0]
Related Topics
How to Send Multicast Packets via a Specfic Interface in Linux
Telnet to Login with Username and Password to Mail Server
/Usr/Bin/Perl: Bad Interpreter: Text File Busy
Add Blank Line After Every Result in Grep
Docker Change Cgroup Driver to Systemd
How to Read Only First N Bytes from the Http Server Using Linux Command
Unzip a Bunch of Zips into Their Own Directories
How to Erase Printed Characters in a Console Application(Linux)
How to Compile Glibc 32Bit on an X86_64 MAChine
Linux: How to Detect That Ftp File Upload Is Finished