Can't get bash script to run, getting errors - bash

I'm trying to run the script found here: http://blog.sebflipper.co.uk/2010/03/10/mysql-backup-as-separate-sql-files-with-rotation/comment-page-1/
bash /path/to/mysql-backup.sh
I'm getting the following errors:
/path/to/mysql-backup.sh: line 2:
: command not found
/path/to/mysql-backup.sh: line 4:
: command not found
/path/to/mysql-backup.sh: line 8:
: command not found
/path/to/mysql-backup.sh: line 10:
: command not found
/path/to/mysql-backup.sh: line 40: syntax error near unexpected token `{
'
/path/to/mysql-backup.sh: line 40: `function checkMysqlUp() {
Am I calling this command improperly?
Ok, it was the spaces, now I'm just getting the last 2 errors

Given the way the error messages are appearing, I think you downloaded the script with CRLF line endings and the shell is not liking this.
Use 'dos2unix' or 'dtou' or (if neither of the above is available, tr) to remove the carriage returns.
tr -d '\015' < /path/to/mysql-backup.sh > /path/to/other-mysql-backup.sh
Then try running:
/path/to/other-mysql-backup.sh

#! /bin/bash
This line at the top of the script isn't right. It should have no spaces.

It's not liking the blank lines in there. Are you sure when you maybe copied and pasted that you didn't inject ^M (carriage returns) or some other white-space character in there?

Related

Assigning bash variable inside curl command for jenkins API

Searching stackoverflow and other google searches did not satisfy my question about assigning bash variable inside curl for the whole purpose of accessing jenkins API.
This is my simple script to access Jenkins API and it already goes wrong here:
JENKINS_USER=myUser
TOKEN=myToken
DOCK=myDock
HOST=localhost
PORT=8080
BASE_URL=https://$JENKINS_USER:$TOKEN#$HOST:$PORT/view/all/job/myProject/api/
curl -g -k ${BASE_URL}
Output from bash:
user#c012311:/mnt/c/Users/User/Desktop$ ./myScript.sh
./myScript.sh: line 6: $'\r': command not found
./myScript.sh: line 11: $'\r': command not found
curl: (3) URL using bad/illegal format or missing URL
I tried accessing my variables like so:
\"$TOKEN\"
${TOKEN}
'$TOKEN'
Yet the results are the same.
The results are preventing me from furthere progression my script for filtering my jenkins builds:
BUILD_NR=$BASE_URL/xml&xpath=//artifact/relativePath[contains(text(),$DOCK)])
And furthere more using those assigned variables inside other variables:
ARTIFACT=$BUILD_NR/[...]
How do I correctly assign variables using bash and curl?
Just run this script on Linux and seems to work fine, it connects to https://myUser:myToken#localhost:8080/view/all/job/myProject/api/ it just throws a Connection refused since there is nothing on 8080 on my machine
I think the issue is line endings. Windows-style line endings (\r\n) - you need to change them to Unix style (\n), hence your error '\r': command not found. Try running the dos2unix command on the script file
Thanks to #shellter for providing an answer for my problem.
I fixed this by adding double quotes to all variables like so:
BASE_URL="https://"${JENKINS_USER}":"${TOKEN}"#"${HOST}":"${PORT}"/view/all/job/myProject/api/"
Furthemore - by executing dos2unix myScript I dont get those annoying outputs anymore:
./myScript.sh: line 6: $'\r': command not found
./myScript.sh: line 11: $'\r': command not found

Running Unix Shell script via batch file

I have a unix shell script which I successfully run through Cygwin. As the script is to be used by someone with no IT knowledge I wanted it to be executed via batch file.
I created .bat file with the following content:
#echo off
C:\cygwin64\bin\bash C:\Sparkpay\MainScripts\converttocsv.sh
It fails to run though with the following errors:
C:\Sparkpay\MainScripts\converttocsv.sh: line 9: ls: command not found
C:\Sparkpay\MainScripts\converttocsv.sh: line 9: awk: command not found
C:\Sparkpay\MainScripts\converttocsv.sh: line 10: date: command not found
C:\Sparkpay\MainScripts\converttocsv.sh: line 15: cat: command not found
C:\Sparkpay\MainScripts\converttocsv.sh: line 15: sed: command not found
C:\Sparkpay\MainScripts\converttocsv.sh: line 15: uniq: command not found
C:\Sparkpay\MainScripts\converttocsv.sh: line 99: rm: command not found
C:\Sparkpay\MainScripts\converttocsv.sh: line 100: mv: command not found
I updated PATH to include c:\cygwin64\bin\bash.
Is there anything that I missed?
Thank you

Bash Error On Launch

I start up my terminal and Bash runs automatically.
When it does I get this error:
-bash: /Users/user/.bash_profile: line 1: unexpected EOF while looking for matching `''
-bash: /Users/user/.bash_profile: line 3: syntax error: unexpected end of file
How do I fix it?
There is an error in /Users/user/.bash_profile involving mismatched quotation marks. Look for mismatched quotes in the first line of that file.

bash function syntax error in cygwin

I'm using cygwin. I made a sh file like the following
#!/bin/sh
function bash {
local var="local variable"
echo $var
}
then I execute this file
./test.sh
The result returned is
./test.sh
./test.sh: line 2: syntax error near unexpected token `$'\r''
'/test.sh: line 2: `function bash {
I have no clue how to fix it and use the capability of writing function in bash scripts. Thank you in advance!
Regards,
The error message is trying to tell you there are CRLF line endings, and it doesn't like the CR ($'\r' being a bash way of representing CR, carriage return).
Using Cygwin, you need to do this before executing any bash file:
sed -i 's/\r$//' name_of_your_script.sh
Once done, you can use it normally. If you make any change in the code, use that line again.
This is because there is a problem with the CR when using bash files in Cygwin. This line eliminates those bothering CR and solves the problem.

Bash for loop error

I am trying out a simple bash script using for loop, and kept getting the following error:
'/test.sh: line 2: syntax error near unexpected token `do
'/test.sh: line 2: `do
The following is the code that is being used...
for animal in dog cat elephant
do
echo "There are ${animal}s.... "
done
However, when I tried on other machines.. it is working no problem.
Please help.
Your test.sh script has Windows-style line endings. The shell sees each \r\n sequence as a \r character at the end of the line.
The shell is seeing do\r rather than do. Since \r sends the cursor to the beginning of the line, that's why you're seeing the quotation mark at the beginning of the line. Try
./test.sh 2>&1 | cat -A
to see what's actually in the error message.
Filter your test.sh script through something like dos2unix to correct the line endings. (Be sure to read the man page first; dos2unix, unlike most text filters, overwrites its input file.)
This is a common problem on Cygwin. Did you use a Windows editor to create the script?

Resources