Curl in combinaties wit variables - bash

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

Related

Laravel on EB: postdeploy script fails on /opt/elasticbeanstalk/bin/get-config

I am writing a postdeploy hook on my EB instance. The code below is in a file at .platform/hooks/postdeploy/my_script.sh
#!/bin/sh
# Update RDS_HOSTNAME
host_name=`/opt/elasticbeanstalk/bin/get-config environment -k RDS_HOSTNAME`
echo "RDS_HOSTNAME="'"'$host_name'"' >> /dev/null 2>&1 | sudo tee /var/app/current/aws.env.tmp.config
PROBLEM: The file aws.env.tmp.config has nothing written in it.
NOTE: The same command /opt/elasticbeanstalk/bin/get-config environment -k RDS_HOSTNAME returns the right value when I execute it on console post-deployment.
What am I doing wrong here? Any suggestions are truly appreciated.
Finally figured it out. Eureka!! For anyone else facing the above issue, there are 2 main reasons to look into:
End of Line Sequence: Please set it to LF (specially, if your local
machine is a windows machine, ensure that the EOL is correct)
No redirect to /dev/null
Once I fixed these 2 issues, the new script became:
#!/bin/sh
# Update RDS_HOSTNAME
host_name=`/opt/elasticbeanstalk/bin/get-config environment -k RDS_HOSTNAME`
echo "RDS_HOSTNAME="'"'$host_name'"' | sudo tee /var/app/current/aws.env.tmp.config
This is now working on EB postdeploy as a hook.

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.

Teamcity with Subversion post commit script on windows

We would like Teamcity to build our solutions on every commit into subversion.
Following the documentation, we are to create a .sh script :-
SERVER=https://buildserver-url
USER=buildserver-user
PASS="<password>"
LOCATOR=$1
# The following is one-line:
(sleep 10; curl --user $USER:$PASS -X POST "$SERVER/app/rest/vcs-root-instances/commitHookNotification?locator=$LOCATOR" -o /dev/null) >/dev/null 2>&1 <&1 &
exit 0
Subversion is running on a windows environment, and so the .sh file will fail.
We are trying to convert this into a .bat file of which we have :-
set SERVER=https://buildserver-url
set USER=buildserver
set PASS=password
LOCATOR=%1%
(timeout 10; curl --user %USER%:%PASS% -X POST "%SERVER%/app/rest/vcs-root-instances/commitHookNotification?locator=%LOCATOR%" -o /dev/null) >/dev/null 2>%1% <%1% &
exit 0
However, this is still failing when trying to execute with
"The system cannot find the path specified"
It seems that perhaps we havnt converted this correctly?
Are the programs you're referencing (such as curl and timeout.exe) in locations that are present in the $PATH/%PATH% variable? How about any other files you're referencing - are you specifying full paths
Side note: Did you install curl and timeout.exe on the Windows server?
Also, /dev/null does not exist on Windows; you need to redirect to NUL. You can't just change the file extension and some of your syntax and expect a bash script to work on Windows.
Were I in your shoes, I'd skip batch altogether and write the script in something modern and sane like Powershell.

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