echo and read with pipe issue [duplicate] - bash

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?

Related

In Shell script process variables in text [duplicate]

This question already has answers here:
bash replace variable name in string with variable value
(2 answers)
Closed 2 years ago.
I have a file 'test.txt', the contents is "SomeText_$(date '+%Y%m%d')".
When reading this into a variable with:
txt=`cat test.txt`
Then I try to print with
echo $txt
This prints: "SomeText_$(date '+%Y%m%d')"
How do I print this so I receive "SomeText_20200904"
The posted echo will display the content from $txt but not execute anything else.
The second line here with eval will read and process what follows then execute the result as a shell command
txt=`cat test.txt`
eval echo $txt

Store into var the result of grep command shell script [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 3 years ago.
I'am creating a shell script to extract a number from a particular line, where one particular string appears (isDone), for that i use a grep, i find the line and can echo it, but i can't store the grep output to a var.
$text var got inumerous tags, one of them having the string "isDone", thats the line i want:
code:
short_str="isDone"
echo "$text" | grep "$short_str"
output:
< s:key name="isDone">1</s:key >
now i want to store that output from grep into a file, and then extract the value (on this case is 1)
what have i tried:
store="$("$text" | grep "$short_str")"
echo "$store"
but that outputs all the file, what am i doing wrong?

Assign output of command to variable in BASH [duplicate]

This question already has an answer here:
Bash how do you capture stderr to a variable? [duplicate]
(1 answer)
Closed 3 years ago.
I would like to assign output of a specific command to variable (nginx -v). My trouble can be seen at printscreen bellow, it still prints output to stdout.
Thank you for your help
a=$(nginx -v 2>&1)
echo $a
Bash how do you capture stderr to a variable?

Error when storing the first line of a file to a variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 7 years ago.
I am doing basic programs with Linux shell scripting(bash)
I want to read the first line of a file and store it in a variable .
My input file :
100|Surender|CHN
101|Raja|BNG
102|Kumar|CHN
My shell script is below
first_line=cat /home/user/inputfiles/records.txt | head -1
echo $first_line
I am executing the shell script with bash records.sh
It throws me error as
/home/user/inputfiles/records.txt line 1: command not found
Could some one help me on this
The line
first_line=cat /home/user/inputfiles/records.txt | head -1
sets variable first_line to cat and then tries to execute the rest as a command resulting in an error.
You should use command substitution to execute cat .../records.txt | head -1 as a command:
first_line=`cat /home/user/inputfiles/records.txt | head -1`
echo $first_line
The other answer addresses the obvious mistake you made. Though, you're not using the idiomatic way of reading the first line of a file. Please consider this instead (more efficient, avoiding a subshell, a pipe, two external processes among which a useless use of cat):
IFS= read -r first_line < /home/user/inputfiles/records.txt

I can't change the value of line_count at the right of pipe , why? [duplicate]

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.

Resources