How does `read` saves the output in the `$REPLY` variable [duplicate] - bash

This question already has answers here:
What happens when reading into a variable in a pipeline?
(2 answers)
Closed 1 year ago.
I wonder why I can't read the line from output of pipe. The following is my command:
find / -name sysinit.target 2> /dev/null | head -n1 | read
I expected to find the line read from the output of head to be recorded in the system variable $REPLY, yet there is nothing in that variable. Why is the line output by head not recorded by read in the variable $REPLY?

It doesn'r work because the pipeline is run in a subshell. Take a look at the lastpipe shell option to work around it or just use process substitution.
read < <(...)
Also as a suggestion maybe use IFS= and -r.

Related

how to run bash for each line in command output [duplicate]

This question already has answers here:
How can I loop over the output of a shell command?
(4 answers)
Closed 2 months ago.
i am building a bash script that is supposed to put each line of the output of one command to an variable and then run some commands on that, i am doing it like
for i in `cmd`
do
echo i=$i
lang=$(echo $i | cut -d '"' -f 6)
echo lang=$lang
#some stuff
done
my problem is that for is using space and newlines for separation to different $i's and i want it to do create only new $i's with newline delimiters cause every line may have a cupple of spaces and i want them no matter that handled as it own...
google but found nothing really helping me, only suggestions to use xargs which dosnt help me cause i need to use not one command but a cupple after creating some variables and running some if statements that desiside which command is to run if any...
If you want to read cmd's output line by line you can do it using
while loop and bash's internal read command
cmd | while IFS= read -r i
do
echo "i=${i}"
lang="$(echo "${i}" | cut -d '"' -f 6)"
echo "lang=${lang}"
#some stuff
done
Use " around a variable's de-reference to avoid problems with spaces inside it's value.

How to read from a file in bash script? [duplicate]

This question already has answers here:
Looping through the content of a file in Bash
(16 answers)
Closed 10 months ago.
I have a simple bash script below that outputs into a file threat info from the domain 1605158521.rsc.cdn77.org. The domain is read from B1Dossier.
#!/bin/bash
baseurl=https://csp.infoblox.com
B1Dossier=/tide/api/data/threats/state/host?host=1605158521.rsc.cdn77.org
APIKey=<REDACTED>
AUTH="Authorization: Token $APIKey"
curl -H "$AUTH" -X GET ${baseurl}${B1Dossier} > /tmp/result
This time, I want the script to get information from multiple domains. For example, I have a file (domfile) with the following domains with each being on a new line:
cdn.js7k.com
example.org
www.hdcctvddns.com
How can I turn my script to execute on each domain from a file (domfle)?
you can use a for loop something like this:
while read -r line; do
echo "$line"
done < <file_path>
you can also use cat to read the file, and use xargs you can execute any command you want per line. but if used incorrectly can lead to command injection.
If for some reason you need to use this syntax over the for loop. take a look at the comments, and research more about command injection with xargs sh.
cat <file_path> | xargs ...

Need help capturing the output of a command that is itself a variable in linux bash [duplicate]

This question already has answers here:
How to run script commands from variables?
(3 answers)
Execute command in a variable don't execute the latter part of a pipe
(1 answer)
Running a command that is stored in a variable (including pipes and redirects)
(3 answers)
Closed 5 years ago.
#!/bin/bash
# 1st part
ret=$(ps aux | grep -v grep) # thats OK
echo $ret
# 2nd part
cmd="ps aux | grep -v grep" # a problem with the pipe |
ret=$($cmd)
echo $ret
How can I use a command-string as I have in the 2nd part? Think the pipe is the problem. Tried to escape it but it did not help. Get some snytax error of ps.
Thanks!
You need eval:
ret=$(eval "$cmd")
Using eval is not recommended here. It can lead to unexpected results, especially when variables can be read from untrusted sources (See BashFAQ/048 - Eval command and security issues.
You can solve this in a simple way by defining and calling a function as below
ps_cmd() {
ps aux | grep -v grep
}
and use it in the script as
output="$(ps_cmd)"
echo "$output"
Also a good read would be to see why storing commands in a variable is not a good idea and has a lot of potential pitfalls - BashFAQ/050 - I'm trying to put a command in a variable, but the complex cases always fail!

Bash: execute content of variable including pipe [duplicate]

This question already has answers here:
How to run script commands from variables?
(3 answers)
Execute command in a variable don't execute the latter part of a pipe
(1 answer)
Running a command that is stored in a variable (including pipes and redirects)
(3 answers)
Closed 5 years ago.
#!/bin/bash
# 1st part
ret=$(ps aux | grep -v grep) # thats OK
echo $ret
# 2nd part
cmd="ps aux | grep -v grep" # a problem with the pipe |
ret=$($cmd)
echo $ret
How can I use a command-string as I have in the 2nd part? Think the pipe is the problem. Tried to escape it but it did not help. Get some snytax error of ps.
Thanks!
You need eval:
ret=$(eval "$cmd")
Using eval is not recommended here. It can lead to unexpected results, especially when variables can be read from untrusted sources (See BashFAQ/048 - Eval command and security issues.
You can solve this in a simple way by defining and calling a function as below
ps_cmd() {
ps aux | grep -v grep
}
and use it in the script as
output="$(ps_cmd)"
echo "$output"
Also a good read would be to see why storing commands in a variable is not a good idea and has a lot of potential pitfalls - BashFAQ/050 - I'm trying to put a command in a variable, but the complex cases always fail!

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

Resources