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 ...
Related
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.
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.
This question already has answers here:
Looping through the content of a file in Bash
(16 answers)
Closed 1 year ago.
Absolute beginner here.
I have a list of passwords in a file named "pw.prn".
Welcome99
ABCDEFGH
12545678
lakers2021
gododgers
I wish to run each line in the file against a hashing program (mkpasswd -m sha-512) and output the password concatenated with a semi-colon then results of the hashing process to a new file:
So the output would look like this:
Welcome99:$6$xDb6xNDzqtnwzVLz$LMA3CNodueIyZavW3CIGDdcl19cekNNG8EB5Hc/vMzZGUSRhbueNCkYRlyaGKAb/VjW0cBiCHdJLt4iL08gBn/
ABCDEFGH:$6$mANzCeK.SUgSD$ID/E6NYPp4cddHCevI.yua3HotbA/a7fZ7xjSk7dUI6fayuTMsO9SCdSA7MFcgh8SUcmNqrqqE4IxAoIEcmFb0
12345678:$6$CwjNF9B1Q8bkwohy$N4eZcj6YPxxbA1MYz0k9t96nCcj9VsZmzrvgqTd9tp2yXbzAdb3hWyjBq6nquMwFbKMJw9ZXs3Uqj.gfnozUS0
lakers2021:$6$fENvTJijoQgyjWMo$W37vZ364wQugW.W7k9Gl8OfJLl8DfR3tpFO/O4oPTCazJgNkJfNE4WiP4z8qSM8H1.ZJrMUWVAYdYOxt0GSHG1
gododgers:$6$1JdXTdpguO0$ZwFoDtZZ2byDemiLv5JAuea6ucAdtYQUTC4EppX2PMzSLaYtMm/ENpBZZAy70Ceuu6yAjXYtggrSOINTRWoBi0
Unfortunately, I have no "code that I have tried" as I do not even know where to being. Is this a for loop? While? I have tried searching using bash script with interactive answers from a file but have not been able to piece anything together.
I hope that my example provides enough information to understand what I am looking for.
I am running this on Ubuntu Linux
Thank You
You could use a while + read loop. See How can I read a file (data stream, variable) line-by-line (and/or field-by-field)
Something like.
#!/usr/bin/env bash
while IFS= read -r line; do
printf '%s:%s\n' "$line" "$(mkpasswd -m sha-512 "$line")"
done < pw.prn
In bash script how do I reference a file as the input for an interactive prompt
Use a variable (positional parameter "$1")
#!/usr/bin/env bash
while IFS= read -r line; do
printf '%s:%s\n' "$line" "$(mkpasswd -m sha-512 "$line")"
done < "$1"
Then
./myscript pw.prn
Assuming the name of the script is myscript and the file in question is pw.prn
This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
I encounter a problem when launching a script using sudo, though I have no problem and the script works fine without sudo.
The Line is :
mapfile -t dataList< <( tac /tmp/result.log | grep 'Command' | cut -d" " -f1 )
The error is "Syntax error near the unexpected symbol " < ".
The sudo command is :
sudo -u victor /tmp/parse.sh
Thank you all for help...
Sounds like a different shell is executing your script, one which doesn't understand the used syntax.
Your script /tmp/parse.sh might lack the #!/bin/bash (or similar) in its head line, and a different shell (root's login shell?) might be used to execute it.
This could be fixed by adding the missing #! line in the script header (recommended), or by calling the shell explicitly:
sudo -u victor bash /tmp/parse.sh
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