How to pass dynamic parameters in curl request - shell

I am passing dynamic value to testing method and executing the curl request. There is an issue with $PARAMETERS.
When I execute the following method, I get error as below
Error:-
curl: option -F: requires parameter
curl: try 'curl --help' or 'curl --manual' for more information
Function:-
testing() {
local URL=$1
local HTTP_TYPE=$2
local PARAMETERS=$3
# store the whole response with the status at the and
HTTP_RESPONSE=$(curl -w "HTTPSTATUS:%{http_code}" -X $HTTP_TYPE $URL $PARAMETERS)
echo $HTTP_RESPONSE
}
URL='https://google.com'
HTTP_TYPE='POST'
PARAMETERS="-F 'name=test' -F 'query=testing'"
result=$(testing $URL $HTTP_TYPE $PARAMETERS)
FYI, above is a sample method and am using shell script. Kindly tell me how to solve this?

Since you are passing multiple command arguments in your 3rd argument, shell will do splitting as you are using unquoted variable inside your function.
You should better use shell array to store PARAMETERS argument.
Have your function as:
testing() {
local url="$1"
local httpType="$2"
shift 2 # shift 2 times to discard first 2 arguments
local params="$#" # store rest of them in a var
response=$(curl -s -w "HTTPSTATUS:%{http_code}" -X "$httpType" "$url" "$params")
echo "$response"
}
and call it as:
url='https://google.com'
httpType='POST'
params=(-F 'name=test' -F 'query=testing')
result=$(testing "$url" "$httpType" "${params[#]}")
Additionally you should avoid using all-caps variable names in your shell script to avoid clashes with env variables.
In case you're not using bash, you can also use:
params="-F 'name=test' -F 'query=testing'"
result=$(testing "$url" "$httpType" "$params")
Code Demo

Related

incorrect syntax in bash

I am trying to pass the GitHub branch creating API URL as a string into a function to get the status code. But as I tried in many ways, it is not working as I think,
the original URL is :
curl -s -X POST -u [user]:[token] -d '{"ref": "refs/heads/feature/bar", "sha": "'$SHA'"}' https://api.github.com/repos/$user/$repo/git/refs
what I am trying to do is, taking some part of this URL and passing into a function as a string as follows:
new_branch_creating_url="-X POST -u $username:$password -d '{"'ref'": "'refs/heads/'$new_branch_to_be_created''", "'sha'": ""$old_sha_value""}' https://api.github.com/repos/$username/$repository_name/git/refs"
My intention is to get the status code of that URL... and for that my function is
#get status code
get_status_code(){
test_url=$1
status_code=$(curl -s -I $test_url | awk '/HTTP/{print $2}')
#echo "status code :$status_code"
if [ $status_code == 404 ]
then
echo "wrong credentials passed..."
exit 1
else
return $status_code
fi
}
and while debugging the code, I am getting like
++ curl -s -I -X POST -u myusername:tokenid -d ''\''{ref:' refs/heads/branch2, sha: '1b2hudksjahhisabkhsd6asdihds8dsajbsualhcn}'\''' https://api.github.com/repos/myusername/myrepo/git/refs
++ awk '/HTTP/{print $2}'
and also my doubt is why sometimes I am received a wrong status code from the function above which I used to get a status code?
while debugging my code:
status_code=
+ '[' == 404 ']'
git_branches.sh: line 154: [: ==: unary operator expected
+ return
+ new_branch_status_code=2
+ echo 'new branch status code ... 2'
new branch status code ... 2
+ '[' 2 == 200 ']'
actually, it is nothing there on status_code from the function, but I received status code =2,,,
not only this time, but I also received 153 instead of 409... why it is like that?
I know this is not a relevant to ask but I have no choice and also it would be very helpful if someone helps me in the early stage of my learning in the shell scripting...
thank you...
Instead of curl -s -I, use :
curl -I -s -o /dev/null -w "%{http_code}"
which will give directly http_code.
You don't need awk

Bash shell: How to get the boolean vars names as string

for bool in $jobdummyjob1 $jobdummyjob2 $jobdummyjob3
do
echo "Boolean Value is $bool"
if "$bool" ; then
echo "$alljobs"
curl -X POST https://jenkins.xxxxxxxxx.com/job/$bool/build --user xxxxxxx.yyyyyyy#xxxxxx.com:ewfwedf3f234523555235235235235235
fi
done
All I need is to take somehow the names of jobdummyjob1, 2, 3 and put them in the URL as a string. Those vars are booleans so when I do this I get true or false in the URL. I do not need the variable value, but its name.
First I run the 'for' and I go through each object. Each object contains boolean value. Then, I do the true/false check and if true, I need to get the string name of the same variable and put it in the URL . This is a Jenkins job.
You can use variable indirection:
for name in jobdummyjob1 jobdummyjob2 jobdummyjob3
do
bool=${!name}
echo "Boolean Value is $bool"
if "$bool" ; then
echo "$alljobs"
curl -X POST https://jenkins.xxxxxxxx.com/job/"$name"/build --user xxxxxxxx.xxxxxxxx#xxxxxxxx.com:xxxxxxxx
fi
done
But it's cleaner to use an associative array:
declare -A bools
bools=([jobdummyjob1]=true [jobdummyjob2]=false [jobdummyjob3]=true)
for name in "${!bools[#]}" ; do
bool=${bools[$name]}
if ...
done

ksh remote function calling another remote function2

I am having problem to run simple code below:
#!/bin/ksh
set -x
function test_me
{
set -x
date
}
function check_me
{
set -x
ssh ${HST2} "$(typeset -f test_me); test_me"
}
ssh ${HST1} "$(typeset -f); check_me"
Fails with syntax error at line 5: `;;' unexpected
Though I can't explain why this gives you the particular error message you see, at least I see that your code can't work:
First you run in one process (on HST1) the commands
function check_me
{
set -x
ssh ${HST2} "$(typeset -f test_me); test_me"
};check_me
On HST1 defines only the function check_me, nothing else. Then you run this check_me.
Inside this function, you refer to two things: A variable HST2, and a function test_me. There is nothing in your code which would define these entities. They are only defined in that initial process, where you do the SSH, but they are not defined on the remote process on the host $HST1.
Moreover, you don't even run a ksh on $HST1. At least I don't see and ksh invocation in your code. Actually, you just pass a function .... to $HST1, and function is not an executable.
#!/bin/ksh
set -x
function second_me
{
set -x
date
}
function first_me
{
set -x
ssh hostname2 "$(typeset -f second_me); second_me"
}
#from local_machine
ssh hostname1 "$(typeset -f); first_me"
above Fails with syntax error at line X: `;;' unexpected
But If I add extra dummy function third_me, things work fine, looks like ksh bug?
#!/bin/ksh
set -x
function third_me
{
set -x
date
}
function second_me
{
set -x
date
}
function first_me
{
set -x
ssh hostname2 "$(typeset -f second_me); second_me"
}
#from local_machine
ssh hostname1 "$(typeset -f); first_me"
Code works fine
Another work around using sub-function:
#!/bin/ksh
set -x
function first_me
{
set -x
function second_me
{
set -x
date
}
ssh hostname2 "$(typeset -f second_me); second_me"
}
#from local_machine
ssh hostname1 "$(typeset -f); first_me"

Variable not expanding in Double quotes for bash script

I have a bash script where i'm trying to call a curl which is having a variable value as input. When trying to execute the bash script the variable value is not getting expanded in double quotes.
Expected curl in script after variable expansion should be as following:
/usr/bin/curl -s -vvvv http://hmvddrsvr:8044/query/service -u iamusr:pssd -d 'statement=DELETE FROM `test_bucket` WHERE type = "Metadata" AND market = "ES" AND status = "active" AND meta(test_bucket).id="fgsd34sff334" '
Getting executed as follows when observed in debug mode:
/usr/bin/curl -s -vvvv http://hmvddrsvr:8044/query/service -u iamusr:pssd -d 'statement=DELETE FROM `test_bucket` WHERE type = "Metadata" AND market = "ES" AND status = "active" AND meta(test_bucket).id=\""$idp_sub"\" '
My bash script is as follows:
#!/bin/bash
idp_sub=""
for idp_sub in $(cat /opt/SP/jboss/home/mayur/es_idp_sub.txt)
do
/usr/bin/curl -s -vvvv http://hmvddrsvr:8044/query/service -u iamusr:pssd -d 'statement=DELETE FROM `test_bucket` WHERE type = "Metadata" AND market = "ES" AND status = "active" AND meta(test_bucket).id=\""$idp_sub"\" ' -o /opt/SP/jboss/home/mayur/es_delete_response.txt
done
How does do i expand the variable value within double quotes as shown above in expected output ?
Your double-quoted string is inside single quotes, where it won't be expanded.
Compare:
foo=bar
echo 'foo=\""$foo\"'
echo 'foo="'"$foo"'"'
In the second example, we end the single quotes, and double-quote $foo, then start new single quotes for the final '.
It's probably easier to read if we expand using printf instead:
printf 'foo=%s\n' "$foo"
That's something you might want to run as a process substitution.
BUT...
This is a wrong and dangerous way to construct an SQL query (and the web server is also poor, if it forwards arbitrary queries - I hope it has no write permissions to the data). Read about "SQL command injection" and come back to this code when you understand the issues.
Nothing inside single quotes will be expanded by bash, including any double-quotes, and variable names. The good news is you can end your single-quoted section and immediately start a double-quoted section to introduce the variable, and it will all be concatenated into a single argument for the application (curl). Try:
/usr/bin/curl -s -vvvv http://hmvddrsvr:8044/query/service -u iamusr:pssd -d 'statement=DELETE FROM `test_bucket` WHERE type = "Metadata" AND market = "ES" AND status = "active" AND meta(test_bucket).id=\"'"$idp_sub"'\" ' -o /opt/SP/jboss/home/mayur/es_delete_response.txt
You can make your code strongly injection-proof by rejecting any string containing a double-quote, but you might reject some strings that have been legitimately escaped.
If you can use the q syntax to quote the string, you can make it more injection-proof, but I guess the attacker just has to inject ]":
/usr/bin/curl -s -vvvv http://hmvddrsvr:8044/query/service -u iamusr:pssd -d 'statement=DELETE FROM `test_bucket` WHERE type = "Metadata" AND market = "ES" AND status = "active" AND meta(test_bucket).id=q\"['"$idp_sub"]'\" ' -o /opt/SP/jboss/home/mayur/es_delete_response.txt
You could then search for and reject the pattern string ]" as your anti-injection, which will allow a much wider class of legitimate strings. You would have to tell the users that you have applied q[] quoting to their input, so they don't have to.

Cannot get value from bash shell output for pg_isready to a variable

I'm trying to get the value for the bash binary call pg_isready to a bash variable
I've tried the following in my script:
#!/bin/bash
haspostgresdb = ${pg_isready -h "ipaddress"}
echo $haspostgresbd
haspostgresdb = ${pg_isready -h ipaddress}
echo $haspostgresbd
haspostgresdb = ${pg_isready -hipaddress}
echo $haspostgresbd
haspostgresdb = ${pg_isready -h"ipaddress"}
echo $haspostgresbd
All return bad substitution as the response. And I did some research and it looks like im doing it correctly
Any suggestions?
Use command substitution and get rid of blanks in the assign command:
haspostgresdb="$(pg_isready -h "ipaddress")"

Resources