Extracting a json value to a variable in zsh shell script [duplicate] - bash

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
I'm trying to fetch the value of a currency from remote api, so I can later run it through some calculation. However, I cannot assign this value to a variable.
#!/usr/bin/env zsh
result = $(curl -s -X GET "https://openexchangerates.org/api/latest.json?app_id=SOME_KEY" | jq '.rates.GBP')
echo $result
This results in :
> ./script.sh:5: command not found: result

You can't have spaces around an equals sign in a Bourne-like shell (e.g., sh, bash, zsh). What happens is that when zsh sees this line:
result = $(curl -s -X GET "https://openexchangerates.org/api/latest.json?app_id=SOME_KEY" | jq '.rates.GBP')
it thinks that result is the name of a command and that the equals sign and what follows it are the arguments of that command. To avoid this, just do this:
result=$(curl -s -X GET "https://openexchangerates.org/api/latest.json?app_id=SOME_KEY" | jq '.rates.GBP')
ETA: I do notice that the line number for the error is 5, not 3 as I would expect. I don't know if that's because of CR/LF line ending issues, if there's something missing from the script that you showed us, or whatnot.

Related

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 ...

Can't manage to give two arguments from a fil to bash script : command not found [duplicate]

This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I'm new to bash script, it is interesting, but somehow I'm struggling with everything.
I have a file, separated by tab "\t" with 2 infos : a string and a number.
I'd like to use both info on each line into a bash script in order to look into a file for those infos.
I'm not even there, I'm struggling to give the arguments from the two columns as two arguments for bash.
#/!bin/bash
FILE="${1}"
while read -r line
do
READ_ID_WH= "echo ${line} | cut -f 1"
POS_HOTSPOT= echo '${line} | cut -f 2'
echo "read id is : ${READ_ID_WH} with position ${POS_HOTSPOT}"
done < ${FILE}
and my file is :
ABCD\t1120
ABC\t1121
I'm launching my command with
./script.sh file_of_data.tsv
What I finally get is :
script.sh: line 8: echo ABCD 1120 | cut -f 1: command not found
I tried a lot of possibilities by browsing SO, and I can't make it to divide my line into two arguments to be used separately in my script :(
Hope you can help me :)
Best,
The quotes cause the shell to look for a command whose name is the string between the quotes.
Apparently you are looking for
while IFS=$'\t' read -r id hotspot; do
echo "read id is: $id with position $hotspot"
done <"$1"
You generally want to avoid capturing things into variables you only use once, but the syntax for that is
id=$(echo "$line" | cut -f1)
See also Correct Bash and shell script variable capitalization and When to wrap quotes around a shell variable?. You can never have whitespace on either side of the = assignment operator (or rather, incorrect whitespace changes the semantics to something you probably don't want).
You have a space after the equals sign on lines 5 and 6, so it thinks you are looking for an executable file named echo ABCD 1120 | cut -f 1 and asking to execute it while assigning the variable READ_ID_WH to the empty string, rather than assigning the string you want to the variable.

How to run curl in bash script and keep the result in a variable [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
This should have been easy but for some reason it's not:
Trying to run a simple curl command, get it's output, and do stuff with it.
cmd='curl -v -H "A: B" http://stackoverflow.com'
result=`$cmd | grep "A:"`
...
The problem - the header "A: B" is not sent.
The execution of the curl command seems to ignore the header argument, and run curl twice - second time with "B" as host (which obviously fail).
Any idea?
Your problem here is that all you are doing on the first command is just setting cmd to equal a string.
Try using $(...) to execute the actual command like so:
cmd=$(curl -v -H "A: B" http://stackoverflow.com)
The result of this will be the actual output from with curl request.
This has been answered many-times see here for example Set variable to result of terminal command (Bash)
Keep in mind that cURL debug output is redirected to STDERR - this is so you can pipe the output to another program without the information clobbering the input of the pipe.
Redirect STDERR to STDOUT with the --stderr - flag like so:
cmd="curl -s -v http://stackoverflow.com -H 'Test:1234' --stderr -"
result=`$cmd | grep "Test:"`
echo $result
./stackoverflow.sh
> 'Test:1234'

Bash: including variables in command-line arguments [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 6 years ago.
I have the following bash script, which is supposed to spider a range of IP addresses.
#!/bin/bash
a = 0
for i in `seq 1 255`;
do
a = a + 1
echo $i
wget -r --spider -D --header="Accept: text/html" --user-agent="Order Of The Mouse: Stress Tester" 139.162.246.$a:80
done
However, at the moment it doesn't include the variable a. How do I properly include a variable in a command line argument when writing a bash script?
Current output looking like this:
/root/burningWood/scripts/StressTest/tester.sh: line 5: a: command not found
254
Spider mode enabled. Check if remote file exists.
--2016-08-28 13:23:10-- http://139.162.246./
Resolving 139.162.246. (139.162.246.)... failed: Name or service not known.
wget: unable to resolve host address ‘139.162.246.’
Found no broken links.
I've made a couple of changes to your code:
#!/bin/bash
for i in {1..255} # <-- use brace expansion instead of seq, no semicolon needed
do
# a = a + 1 <-- variable $a is redundant, just use $i
wget -r --spider -D --header="Accept: text/html" \
--user-agent="Order Of The Mouse: Stress Tester" "139.162.246.$i:80"
done
I moved part of the call to wget onto a new line so you could see the change more clearly.
Note that there are no spaces around an assignment, so if you wanted to use the variable a, you would assign to it like a=$(( a + 1 )).
bash math needs special syntax - here's one way to do it
a=$((a + 1))

How to run curl command with parameter in a loop from bash script? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 7 years ago.
I have a curl command I would like to execute in a for loop.
For example I wanted to loop 1-100 times and when curl command runs it uses iterator variable value in the curl command itself.
something like
#!/bin/bash
for i in {1..10}
do
curl -s -k 'GET' -H 'header info' -b 'stuff' 'http://example.com/id=$i'
done
--notice here I want var i to be changing with every curl.
Anything helps
Thanks.
Try this:
set -B # enable brace expansion
for i in {1..10}; do
curl -s -k 'GET' -H 'header info' -b 'stuff' 'http://example.com/id='$i
done
See: Difference between single and double quotes in Bash

Resources