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

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

Related

how can I solve Error oh my bash error after updating?

After I Updated Oh my bash this error, and every time I start the console it appears. This causes me to lose all things from oh my bash. It shows some syntax errors, but I don't know how to solve them.
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
-bash: /home/luismanzur/.oh-my-bash/lib/utils.sh: line 75: syntax error near unexpected token `$'{\r''
'bash: /home/luismanzur/.oh-my-bash/lib/utils.sh: line 75: `function _omb_util_setexit {
_omb_util_glob_expand: command not found
: command not found
: command not found
-bash: /home/luismanzur/.oh-my-bash/aliases/general.aliases.sh: line 26: syntax error near unexpected token `$'{\r''
'bash: /home/luismanzur/.oh-my-bash/aliases/general.aliases.sh: line 26: `function _omb_alias_general_cp_init {
_omb_util_function_exists: command not found
_omb_util_glob_expand: command not found
_omb_util_add_prompt_command: command not found
_omb_util_command_exists: command not found
(base) luismanzur#tululo:~$
How can I solve it?
display/attach the contents of
utils.sh
and
general.aliases.sh
Using google search I found:
https://gitcode.net/pattern/ohmybash/oh-my-bash/-/blob/master/aliases/general.aliases.sh?from_codechina=yes
content being:
#!/usr/bin/env bash
alias cp='cp -iv' # Preferred 'cp' implementation
alias mv='mv -iv' # Preferred 'mv' implementation
alias mkdir='mkdir -pv' # Preferred 'mkdir' implementation
alias ll='ls -lAFh' # Preferred 'ls' implementation
alias less='less -FSRXc' # Preferred 'less' implementation
alias nano='nano -W' # Preferred 'nano' implementation
alias wget='wget -c' # Preferred 'wget' implementation (resume download)
alias c='clear' # c: Clear terminal display
alias path='echo -e ${PATH//:/\\n}' # path: Echo all executable Paths
alias show_options='shopt' # Show_options: display bash options settings
alias fix_stty='stty sane' # fix_stty: Restore terminal settings when screwed up
alias fix_term='echo -e "\033c"' # fix_term: Reset the conosle. Similar to the reset command
alias cic='set completion-ignore-case On' # cic: Make tab-completion case-insensitive
alias src='source ~/.bashrc' # src: Reload .bashrc file
26th line being valid:
alias path='echo -e ${PATH//:/\\n}' # path: Echo all executable Paths

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.

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