How to Run a String in a Variable as a Shell Command

Asked 2 years ago, Updated 2 years ago, 68 views

for multiple endpoints
 curl https://endpoint-dparam1=correct-o/dev/null-w%{http_code}
curl https://endpoint-dparam1 = wrong-o/dev/null-w% {http_code}
curl https://endpoint-dparam1=-o/dev/null-w% {http_code}

I would like to create a script that validates that each result is 200 404 400 by changing one parameter as shown in

That's why
Paste multiple lines of commands that move by pasting them into the terminal directly into the source code
I want to put it in the variable once as a string and then expand it and run it.

ENDPOINT=$(cat<<EOS
curl https://endpoint1\
-d param2 = abc
)

echo$ENDPOINT

RESULT=$(${ENDPOINT}-dparam1=correct-o/dev/null-w%{http_code})

echo$RESULT

I wrote like this, but even if I put the command that RESULT should be 200, I get 400 errors.
The contents of $ENDPOINT are set correctly, and if you copy the output to the terminal and run it, it will be 200.

VAR=$ (command string)
I think can store the standard output in variables, but can't we use variables in the command string?

How do I get the standard output from a shell variable by executing the contents of a string as a command?

Specified in Hear DocumentationI want to run a command string containing a new line

Instead of asking a similar question and variableizing the string,
I was asked if I could make it into a function.

param1 is a common required parameter for most endpoints in the record ID
I want to repeat the same validation on multiple endpoints.
I want to parameterize it to the endpoint

I haven't even executed the above content yet, so I don't know if I'm calling it correctly in the future.
Finally, I would like to write like this

#A function that validates three patterns by receiving endpoints (and individual parameters) as arguments

validate_param1()
{
# Correct =>200 OK
  RESULT=$($@-dparam1=correct-o/dev/null-w%{http_code})
  if ["$RESULT"!='200']; then echo "failed at Line $LINENO"; exit;fi

# Not Present = > 404 Not Found
  RESULT=$($@-dparam1=wrong-o/dev/null-w%{http_code})
  if ["$RESULT"!='404']; then echo "failed at Line $LINENO"; exit;fi

# Empty = > 400 Invalid Request
  RESULT=$($@-dparam1=-o/dev/null-w%{http_code})
  if ["$RESULT"!='400']; then echo "failed at Line $LINENO"; exit;fi 
}

# Apply validation with individual parameters for each endpoint

ENDPOINT = $(cat<<EOS)
curl https://endpoint1\
-d param2 = abc
)
validate_param1$ENDPOINT

ENDPOINT = $(cat<<EOS)
curl https://endpoint2\
-d param3 = def
)
validate_param1$ENDPOINT

ENDPOINT = $(cat<<EOS)
curl https://endpoint3\
-d param4 = ghi
)
validate_param1$ENDPOINT

  :

I would appreciate it if you could tell me how to write better

Now I know the cause

COMMAND=$(cat<<EOS
curl-s https://httpbin.org/post\
-d param = '{"x":1}'
EOS)

The string above echo$COMMAND# appears.
RESULT=$($COMMAND)
echo$RESULT

# Write the displayed string exactly as it is in $()
RESULT=$(curl-s https://httpbin.org/post-dparam='{"x":1}')
echo$RESULT

If you write a test code like this,

 curl-s https://httpbin.org/post-d param='{"x":1}'
{ "args": {}, "data": "", "files": {}, "form": { "param": "'{\"x\":1}'" }, "headers": { "Accept": "*/*", "Content-Length": "15", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "curl/7.64.1", "X-Amzn-Trace-Id": "Root=1-5e681d1c-980920a82239923c1a3bd596" }, "json": null, "origin": "164.70.253.210", "url": "https://httpbin.org/post" }
{ "args": {}, "data": "", "files": {}, "form": { "param": "{\"x\":1}" }, "headers": { "Accept": "*/*", "Content-Length": "13", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "curl/7.64.1", "X-Amzn-Trace-Id": "Root=1-5e681d1d-21605a46dca0cbf3d3679611" }, "json": null, "origin": "164.70.253.210", "url": "https://httpbin.org/post" }


Paste it correctly and move
"param":"{\"x\":1}"
The POST parameter should be
"param":"{\"x\":1}"
and ' were included

But I don't know how to fix it

Stick it to the terminal and write the moving string in the ear document
Is it impossible to run it once as a string in a variable?

bash

2022-09-30 11:37

1 Answers

This is an example of how to substitute the resulting status code for a variable while executing the curl command by referring to the value of the variable.

$exportSOja="https://ja.stackoverflow.com"
$ status_code=`curl-o/dev/null-w'%{http_code}\n'-s${SOja}`
$ echo$status_code
200


2022-09-30 11:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.