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

Asked 5 months ago, Updated 5 months ago, 16 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.

curl https://endpoint1\
-d param2 = abc




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

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

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

# Empty = > 400 Invalid Request
  RESULT=$([email protected]=-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

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

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


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

Now I know the cause

-d param = '{"x":1}'

The string above echo$COMMAND# appears.

# Write the displayed string exactly as it is in $()

If you write a test code like this,

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

Paste it correctly and move
The POST parameter should be
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?


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.

$ status_code=`curl-o/dev/null-w'%{http_code}\n'-s${SOja}`
$ echo$status_code

2022-09-30 11:37

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.