Linux running a command inline to CURL command - shell

I'm trying to run the file sh /usr/local/sbin/script.sh and assigning the stderr as parameter to $msg.
curl -k -X POST https://192.168.0.25/sims/index.php -d "option=com_user&task=sendSMSalert&msg=$(/usr/local/sbin/script.sh 2>&1)"
The above script works fine on Ubuntu but when running the same script on freenas 11, I'm getting the error:
Illegal variable name.
I then tried replacing $(/usr/local/sbin/script.sh 2>&1) with (/usr/local/sbin/script.sh), but instead of output of /usr/local/sbin/script.sh command, It's just assigning the string "/usr/local/sbin" to that parameter.
Please help me.

Related

Execute command inside a bash script with space inside parameter

I'm having the following issue. When I execute this command sfdx profile:field:add -n "Account.Test" -m re -p "Test" from a Bash script, it's all fine.
However, when I try to execute the following:
sfdx profile:field:add -n "Account.Test" -m re -p "Test Test"
I get this error: 'C:\Program' is not recognized as an internal or external command
When I run the same command in the terminal, it works fine. It's just when I put it inside a bash script that this error happens.
Currently running this on Windows 10.
I'm 100% sure it's the space in that last parameter, I am just not sure how to get around it. Can anyone help?

Curl in combinaties wit variables

In My bash file i use curl
The line bellow works like a charm
curl -u "$USERNAME:$PASSWORD" --ftp-create-dirs "ftp://191.158.2.04/$DESTDIRNAS/$TIMESTAMP/"
But when i make a variable of the ip adres it dont work anymore
SERVER = "191.162.2.04"
curl -u "$USERNAME:$PASSWORD" --ftp-create-dirs "ftp://$SERVER/$DESTDIRNAS/$TIMESTAMP/"
Sorry i wil give some more info
When i run the script the script it does hang on this line.
I get a promt > but cannot used it. I must compleet reset putty to go on.
SpelChek give my this output.
$ shellcheck myscript
Line 10:
SERVER="191.162.2.04" # IP of NAS, used for ftp
^-- SC2034 (warning): SERVER appears unused. Verify use (or export if used externally).
$
Suggesting to replace this line:
SERVER = "191.158.2.04"
with:
SERVER="191.158.2.04"
Suggesting to debug your code with set -x and set +x to view the expanded command:
SERVER="191.158.2.04"
set -x #debug analysis start
curl -u "$USERNAME:$PASSWORD" --ftp-create-dirs "ftp://$SERVER/$DESTDIRNAS/$TIMESTAMP/"
set +x #debug analysis end

Shell Script failing on alpine linux while working in mac terminal

The following line fails when run in a alpine docker container:
toDelete=( $(curl --silent $url/_cat/indices\?format=json | jq -r '.[].index | select(startswith('\".kibana\"'))') )
The following error message appears:
run.sh: line 1: syntax error: unexpected "("
When I run the command in the terminal on my mac, everything works properly. The brackets are added so that the result (variable toDelete) is interpreted as array and can be looped through with a for loop like so:
for index in "${toDelete[#]}"; do
curl -X DELETE $url/$index
done
Any help in how to solve this problem is appreciated!
Marking down the answer.
The issue was with the interpreter.
worked after making the below change.
["/bin/ash", "run.sh"]
the passed one was
["/bin/sh", "run.sh"]

Weird behavior of calling docker run from bash script

I try to run docker run from bash script and docker says:
“is not a docker command”
If I print the docker command line before I called docker and I copy it to clipboard and paste it to command line it works well!
here is the command in bash script:
local args="run ${nw_param} ${opts} --name ${img} ${repository}/${img}:${tag}"
docker ${args}
the current echo of args string is:
run --net=ehvb-network -d --restart=always --name my-module my-private-registry:5000/my-module:0.0.1-1555334810
When I copied this string to the clipboard and paste it to command line it works well.
I use Debian stretch. My script is using bash (#!/bin/bash)
When I remove ${opts} it runs from bash. Opts currently contains “-d --restart=always”. When I try to use only -d or only --restart=always it works well. But when I try to use both together it doesn’t work well.
And I try to define opts like this:
opts=’–restart=always -d’
the message from docker is:
docker: Error response from daemon: invalid restart policy ‘always -d’, but the print message contains:
opts:–restart=always -d
Somebody removes --restart=
The problem was that, I used variables coming from other bash command in my script (like curl, ps etc). All of these variables end with carriage return \r. When I try to insert these variables into a docker parameter string \r are inside it. I need to add:
| sed 's/\r//' to all of these commands.

Bash script: Turn on errors?

After designing a simple shell/bash based backup script on my Ubuntu engine and making it work, I've uploaded it to my Debian server, which outputs a number of errors while executing it.
What can I do to turn on "error handling" in my Ubuntu machine to make it easier to debug?
ssh into the server
run the script by hand with either -v or -x or both
try to duplicate the user, group, and environment of the error run in your terminal window If necessary, run the program with something like "su -c 'sh -v script' otheruser
You might also want to pipe the result of the bad command, particularly if run by cron(8), into /bin/logger, perhaps something like:
sh -v -x badscript 2>&1 | /bin/logger -t badscript
and then go look at /var/log/messages.
Bash lets you turn on debugging selectively, or completely with the set command. Here is a good reference on how to debug bash scripts.
The command set -x will turn on debugging anywhere in your script. Likewise, set +x will turn it off again. This is useful if you only want to see debug output from parts of your script.
Change your shebang line to include the trace option:
#!/bin/bash -x
You can also have Bash scan the file for errors without running it:
$ bash -n scriptname

Resources