This question already has an answer here:
Why command "read" doesn't work?
(1 answer)
Closed 6 years ago.
I am trying to get a user input inside a while loop in a shell script.
ls| while read p
do
echo $p
read key
done
I cannot read the input from stdin(keyboard) because my input is piped through ls.
Can someone help me to get a user input inside the loop?
You can read from stdin using file descriptor 0. or by just using /dev/tty . Where $$ is pid of your current process.
ls| while read p
do
echo $p
read key < /proc/$$/fd/0
# OR read key < /dev/tty
done
Related
This question already has answers here:
Read values into a shell variable from a pipe
(17 answers)
Why variable values are lost after terminating the loop in bash? [duplicate]
(1 answer)
Closed 17 days ago.
First of all, I am sorry, I am learning the bash and I am a newbie.
Please find the below script.
grep "error" /var/log/syslog | while read line
do
echo $line
done
If I am not wrong,The above script will grep the keyword "error" in /var/log/syslog and will send it inside the while loop as STDIN and output will be displayed.
Also please loop the below script.
echo "hello" | read hi
echo $hi
So when I run this script I am not getting any output, why is that?
should I use any loop? only then I will get output?
This question already has answers here:
How can I wait for certain output from a process then continue in Bash?
(5 answers)
Closed 1 year ago.
I'm executing a command in a bash shell and need to wait until a command echos a string. This is the pseudo code for what I have in mind. What do I set currentline to such that the until loop exits when the text output I'm waiting for echos?
currentline=""
until [[$currentline | grep -m 1 "the text output I'm waiting for"]]; do
echo " == Waiting for the text == "
currentline=???
sleep 1;
done
EDIT: the text output I'm waiting for tells the script the executed command is ready to receive input. The command does not terminate after the text output I'm waiting for. I want the script to stop sleeping and continue.
EDIT #2: The script runs on a remote CI/CD service, Codemagic, so I don't think I can use tail or any command that needs the path to the log file? (As described here)
So just:
while IFS= read -r line; do
if [[ "$line" == "the text .... " ]]; then
break;
fi
done < <(your-cmd)
if you want to keep your-cmd running, use coproc.
coproc your-cmd
while ..... as above ...
done <&"${COPROC[0]}"
This question already has answers here:
Output for loop to a file
(4 answers)
Loop and append/write to the same file without overwriting
(2 answers)
Closed 5 years ago.
I have a shell script that extracts certain values from a text file (input to it via the terminal). The script does the extraction as intended except, it doesn't print the output to the file correctly.
The script is:
#!/bin/bash
input_file=$1
while read -r LINE
do
IFS="=" read -r -a params <<< "$LINE"
if [ -n "${params[2]}" ]
then
IFS=" " read -r -a param_opcode <<< "${params[2]}"
echo "${param_opcode}"
fi
done < "$input_file"
The output on the terminal is as follows:
0xd2800140
0xd2800061
0x8b010000
0x8b000042
0xd1000821
0xd28001e5
0xd28000a6
0x9ac608a5
0xe7ff0010
0xe7ff0010
However, when I try to write this to a text file bu doing:
echo "${param_opcodes}" > log.txt
It prints only this to the file:
0xe7ff0010
I tried >> but I don't want to append to it. I want the file to be overwritten every time, I run the script.
Your redirection to log file isn't right because it's inside the while loop.
Use the redirection at the end of the while loop:
while read -r LINE; do
...
done < "$input_file" > "log.txt"
This question already has answers here:
While-loop subshell dilemma in Bash
(4 answers)
Closed 8 years ago.
I occured a problem and I can't find why does it run.
The follow codes is both used to count the line of file 'file.in' , but the first can't change the value of $line_count
The first code is :
#!/bin/bash
line_count=0
cat file.in | while read line; do
let ++line_count
done
echo $line_count
the second code is :
#!/bin/bash
line_count=0
while read line; do
let ++line_count
done < file.in
echo $line_count
Due to use of pipe your first code sample is executing while loop in a sub-shell hence changes made in line_count variable get lost after sub shell exits.
This question already has answers here:
rewinding stdin in a bash script
(4 answers)
Closed 7 years ago.
I have a shell script where I pass a txt file to the script as follows:
./run.sh < list.txt
Within the script, I am doing a "while read LIST do ... end"
It all works well, and the script executes using the list.
However, now I want to have a second while read LIST do ... end in the same shell script. I want it to read again from the original list I'm passing it on execution, but it doesn't work. It reads the list.txt file for the first loop, but not the second.
What do I do to make the script read list.txt each time I'm asking for it?
You can't read stdin twice. Try passing list.txt on the command-line rather than redirecting it.
./run.sh list.txt
Then in your script:
while read LINE; do
...
done < "$1"
while read LINE; do
...
done < "$1"
Alternatively, save the contents of stdin off the first time you read through it. For instance:
# First loop, save stdin in an array.
LINES=()
while read LINE; do
LINES+=("$LINE")
...
done
# Second loop, iterate over the array.
for LINE in "${LINES[#]}"; do
...
done