In Shellscript Assign Variable Based on Curl Output

Assign output to variable in Bash

In shell, you don't put a $ in front of a variable you're assigning. You only use $IP when you're referring to the variable.

#!/bin/bash

IP=$(curl automation.whatismyip.com/n09230945.asp)

echo "$IP"

sed "s/IP/$IP/" nsupdate.txt | nsupdate

In ShellScript Assign Variable Based on Curl Output

There should no have spaces when assign url, try this :-

url=$(curl -s ...)

note : curl -s is to suppress the progress meter

Saving cURL -w variables to a bash variable

It's not possible just like that coz every command run in its own subshell, create own vars and so on. Try this code:

export somevar=test; curl -kso /dev/null -w "$(export somevar=%{time_connect}; env | grep somevar)\n" https://www.google.com; env | grep somevar

The output(in my case) will be:

somevar=0,004898
somevar=test

So the way to do it is to output directly to var.

somevar=$(curl -kso /dev/null -w "%{time_connect}" https://www.google.com)

$ echo $somevar
0,005093

Or to an array, if you need multiple items.

somearray=( $(curl -kso /dev/null -w "%{time_connect} %{time_namelookup} %{time_appconnect}" https://www.google.com) )

$ echo ${somearray[@]}
0,005095 0,004317 0,165347

How to capture the output of curl to variable in bash

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";

Shell script -- how to store curl command WITH variables result into another variable?

This is the command in your command substitution:

curl -XGET '${search_host}/${index}/_mapping'

This command will not work on its own. The problem that you are observing is due to the single quotes, not to the use of command substitution, $(...).

Inside single-quotes, variables are not expanded. Since you need variable expansion, use double-quotes:

curl -XGET "${search_host}/${index}/_mapping"

Putting the above in your original command:

local sample_mapping="$(curl -XGET "${search_host}/${index}/_mapping")"

Note that quotes and command substitutions can nest : the fact that there are double quotes inside the command substitution has no effect on the double quotes outside of the command substitution.

Example

Let's define some variables:

$ search_host=http://google.com
$ index=index.html

Now, let's try the command with single-quotes:

$ a=$(curl -XGET '$search_host/$index')
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: $search_host

The above failed with an error message similar to the one you observed: Could not resolve host: $search_host

Let's try again with double-quotes:

$ a=$(curl -XGET "$search_host/$index")
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 229 100

229 0 0 520 0 --:--:-- --:--:-- --:--:-- 521

The above succeeded.



Related Topics



Leave a reply



Submit