Running Unix Shell script via batch file - bash

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

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

What does this line "BUILD_TARGET=${1:-none}" means in shell scripting?

I just started learning shell scripting so forgive me if this is too basic to ask here. I want to run this sh script
(https://github.com/daid/Cura/blob/SteamEngine/package.sh).
But I cant understand what this line ( BUILD_TARGET=${1:-none} ) does?
Here is the excerpt:
#!/usr/bin/env bash
set -e
set -u
# This script is to package the Cura package for Windows/Linux and Mac OS X
# This script should run under Linux and Mac OS X, as well as Windows with Cygwin.
#############################
# CONFIGURATION
#############################
##Select the build target
BUILD_TARGET=${1:-none}
#BUILD_TARGET=win32
#BUILD_TARGET=darwin
#BUILD_TARGET=debian_i386
#BUILD_TARGET=debian_amd64
#BUILD_TARGET=debian_armhf
#BUILD_TARGET=freebsd
##Do we need to create the final archive
ARCHIVE_FOR_DISTRIBUTION=1
##Which version name are we appending to the final archive
export BUILD_NAME=15.04.6
TARGET_DIR=Cura-${BUILD_NAME}-${BUILD_TARGET}
##Which versions of external programs to use
WIN_PORTABLE_PY_VERSION=2.7.2.1
When I tried to see how it works by running in Cygwin, it throws the following error,
$ bash package.sh
package.sh: line 2: $'\r': command not found
: invalid option 3: set: -
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
: invalid option 4: set: -
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
package.sh: line 5: $'\r': command not found
package.sh: line 8: $'\r': command not found
package.sh: line 12: $'\r': command not found
package.sh: line 21: $'\r': command not found
package.sh: line 27: $'\r': command not found
package.sh: line 30: $'\r': command not found
package.sh: line 48: syntax error near unexpected token `$'{\r''
'ackage.sh: line 48: `{
What I am missing in Cygwin?
Update 1 : I solved 'command not found' by converting the line endings to unix compatible ones. More can be found here, '\r': command not found - .bashrc / .bash_profile
$1 is the first command line argument
${1:-none} means if the first command line argument is unset or null, "none" is substituted. Otherwise, the value of the first command line argument is substituted.
For more info, check [ shell parameter expansion ].

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.

Command Line OSX: Configuring the PATH variable

I've tried to set up a folder called bin in my user directory. Sadly, my first attempt of appending the new directory was incorrect as I missed a ". I tried opening up the .bash_profile to try and delete my first attempt, but due to not really knowing what I was doing when saving I get these errors when I open the bash.
Last login: Mon Dec 23 11:13:39 on ttys000
-bash: /Users/daz/.bash_profile: line 2: unexpected EOF while looking for matching `"'
-bash: /Users/daz/.bash_profile: line 3: syntax error: unexpected end of file
darryls-mini:~ daz$ cat ~/.bash_profile
PATH=$PATH:~/bin"
PATH="$PATH:~/bin"
darryls-mini:~ daz$
The first line after cat is the incorrect one. This is the result of me trying to delete the bash_profile file and re-saving it using pico ~/.bash_profile
The error is in the following:
PATH=$PATH:~/bin"
which is causing the unexpected EOF while looking for matching `"'.
Observe the quoting. You probably wanted to say:
PATH="$PATH:~/bin"
Why don't you just edit the file and delete the line? Alternatively, this command will do it for you (assuming that your ~/.bash_profile is otherwise empty, as it seems to be in your post):
echo 'PATH="$PATH:~/bin"' > ~/.bash_profile
The error message is telling you what the problem is:
unexpected EOF while looking for matching `"'
You close the quotes without opening them:
PATH=$PATH:~/bin"
Change it to
PATH=$PATH:~/bin
or
PATH="$PATH:~/bin"

Can't get bash script to run, getting errors

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?

Resources