How to capture the output of curl to variable in bash [duplicate] - bash

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
So, lets say I have the following command:
curl -I http://google.com | head -n 1| cut -d $' ' -f2
This will capture the http status code??
Now I want to assign this to variable.. in bash script
like output = "curl -I http://localhost:8088/tracks?key=9 | head -n 1| cut -d $' ' -f2"
or something like that..
How do I assign the response of above command to a variable called output in bash?
Thanks

You have two options (see this StackOverflow answer here):
Preferred: Surround the invocation in $()
Surround the invocation in back ticks
NOTE: back ticks are legacy, the former method is preferred.
output=$(curl -I http://google.com | head -n 1| cut -d $' ' -f2)
echo "$output";
output=`curl -I http://google.com | head -n 1| cut -d $' ' -f2`
echo "$output";

Related

How to pass string variable in cut shell command [duplicate]

This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 1 year ago.
Assume that I have :
.sh file having these commands :
#!/bin/bash
GIT_BRANCH="origin/release/2.4.0"
echo $GIT_BRANCH
then, I want to compute new varible from the GIT_BRANCH (operation of substring) :
So,
RELEASE_VERSION=$( $GIT_BRANCH | cut -d "/" -f3)
echo $RELEASE_VERSION
But this does return message error : bad substitution
I tried many possibilities in the RELEASE_VERSION, but no result.
like
RELEASE_VERSION=$(echo $GIT_BRANCH | cut -d "/" -f3)
RELEASE_VERSION=$("$GIT_BRANCH" | cut -d "/" -f3) and this return empty results
You are definitelly missing an echo statement. Following code works for me just fine.
GIT_BRANCH="origin/release/2.4.0"
RELEASE_VERSION=$(echo $GIT_BRANCH | cut -d "/" -f3)
echo $RELEASE_VERSION

Bash : Curl grep result as string variable

I have a bash script as below:
curl -s "$url" | grep "https://cdn" | tail -n 1 | awk -F[\",] '{print $2}'
which is working fine, when i run run it, i able to get the cdn url as:
https://cdn.some-domain.com/some-result/
when i put it as variable :
myvariable=$(curl -s "$url" | grep "https://cdn" | tail -n 1 | awk -F[\",] '{print $2}')
and i echo it like this:
echo "CDN URL: '$myvariable'"
i get blank result. CDN URL:
any idea what could be wrong? thanks
If your curl command produces a trailing DOS carriage return, that will botch the output, though not exactly like you describe. Still, maybe try this.
myvariable=$(curl -s "$url" | awk -F[\",] '/https:\/\/cdn/{ sub(/\r/, ""); url=$2} END { print url }')
Notice also how I refactored the grep and the tail (and now also tr -d '\r') into the Awk command. Tangentially, see useless use of grep.
The result could be blank if there's only one item after awk's split.
You might try grep -o to only return the matched string:
myvariable=$(curl -s "$url" | grep -oP 'https://cdn.*?[",].*' | tail -n 1 | awk -F[\",] '{print $2}')
echo "$myvariable"

working with "unclear" declared variables [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I am trying to save the specific output from a piped command to a variable.
value= ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r"
This saves the average ping to the variable "value".
However when I try to further process the variable e.g. in an echo line like:
echo "The Average ping is: $variable"
The output is
The Average ping is: $variable
Even when i try to pass the value to another Variable like:
value2= $value
the result is the same.
I read that variables in bash need to be declared in a certain way, may this be the problem in this specific case?
sh or bash:
value="`ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r"`"
bash:
value="$(ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r")"

Assigning a command output to a shell script 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 2 years ago.
How do I assign a command output to a shell script variable.
echo ${b%?} | rev | cut -d'/' -f 1 | rev
${b%?} gives me a path..for example: /home/home1
The above command gives me home1 as the output. I need to assign this output to a shell script variable.
I tried the below code
c=${b%?} |rev | cut -d '/' -f 1 | rev
echo $c
But it didn't work.
To assign output of some command to a variable you need to use command substitution :
variable=$(command)
For your case:
c=$(echo {b%?} |rev | cut -d '/' -f 1 | rev)
Just wondering why dont you try
basename ${b}
Or just
echo ${b##*/}
home1
If you want to trim last number from your path than:
b="/home/home1"
echo $b
/home/home1
b=${b//[[:digit:]]/}
c=$(echo ${b##*/})
echo ${c}
home
Just like this:
variable=`command`

Shell script cut command [duplicate]

This question already has an answer here:
Shell script, saving the command value to a variable
(1 answer)
Closed 9 years ago.
I am trying to cut ABCservice and DEFService dfrom the array and print them.What am I missing here?
urlArray=('http://server:port/ABCservice/services/ABCservice?wsdl' 'http://server:port/DEFservice/services/DEFservice?wsdl')
for url in "${urlArray[#]}"
do
service=echo $url|cut -f4 -d/
echo $service
done
Expected Output:
ABCService
DEFService
Current Output:
./test1.sh: line 6: http://server:port/ABCservice/services/ABCservice?wsdl: No such file or directory
./test1.sh: line 6: http://server:port/DEFservice/services/DEFservice?wsdl: No such file or directory
What abut this?
service=$(echo $url | cut -d"/" -f4)
echo $service
or directly
echo $(echo $url | cut -d"/" -f4)
The problems in your code:
service=echo $url|cut -f4 -d\
to save a command output in a variable, we do it like this: service=$(command).
your cut had \ as delimiter instead of /. Also it is good to wrap it with brackets: -d "/"
service=$(echo $url | cut -d/ -f6 | cut -d\? -f1)
echo $service
Using bash string function:
for i in "${!urlArray[#]}"; do
urlArray[i]="${urlArray[i]%\?*}"
urlArray[i]="${urlArray[i]##*/}"
echo ${urlArray[i]}
done
$ urlArray=('http://server:port/ABCservice/services/ABCservice?wsdl' 'http://server:port/DEFservice/services/DEFservice?wsdl')
$ for i in "${!urlArray[#]}"; do urlArray[i]="${urlArray[i]%\?*}"; urlArray[i]="${urlArray[i]##*/}"; echo ${urlArray[i]} ; done
ABCservice
DEFservice
Your delimiter should be a forward slash
echo 'http://server:port/ABCservice/services/ABCservice?wsdl' | cut -f 4 -d/
The \ is an escape character. Try \\ instead.

Resources