Shell Script failing on alpine linux while working in mac terminal - bash

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

Related

Script don't recognize strings (Docker Windows)

I have an exe file which executing some commands in docker container (ubuntu) that should make a build and run application. In some step I need to make a copy of dist file and command looks like this
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v //f/bigaboo/repos/ledger://f/bigaboo/repos/ledger --workdir=//f/bigaboo/repos/ledger bbcli_executor bash -c "cp ./public/index.php.dist ./public/index.php"
When I just run this command in the terminal all works well but if it's under exe control I have this output
./public/index.php.dist: -c: line 1: unexpected EOF while looking for matching `"'
./public/index.php.dist: -c: line 2: syntax error: unexpected end of file
2022/10/31 22:14:42 Failed to create index.php`. Error -> exit status 2
Looks like script don't recognize strings under exe.
I've tried to wrap cp in '',`` and \x22 but I've got the same result but with different symbols also I've tried to change encoding also without success :(

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?

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.

Linux running a command inline to CURL command

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.

PSQL run from shell script - command not found?

I try to execute an PSQL from shell, and the thing is - it returns the error "command not found". I have a shell script in which there're lines:
ID3=`more DATA/Id3.txt`
psql -h localhost test test -Atc "SELECT id, reference, timestamp FROM restricted WHERE id='`$ID3`'"
In the Id3.txt there's only the ID. When the psql command is written and executed direct through prompt - there's no problem at all and correct value is returned. When executed with a .sh file - error "command not found" is brought up. I have no clue why. Maybe anyone have a idea?
In your script try to add which psql to see whether you can find the executable
Run below command on your console: whereis psql
And then replace psql inyour script with the output of above command. This

Resources