How does this .sh Shell Script have a Syntax Error? - xcode

I am attempting build a unity project with Xcode and I have a file CrashHunterScript.sh that consists of this:
if [ "$CONFIGURATION" == "Release" ]
then
exit 0
fi
UPLOADER_PATH=$(find $PROJECT_DIR/.. -type f -iname dSYMUploader | sort | uniq | head -n 1)
echo ${UPLOADER_PATH}
if [ -e ${UPLOADER_PATH} ]
then
${UPLOADER_PATH} ${BUILT_PRODUCTS_DIR} ${INFOPLIST_PATH} ${PRODUCT_BUNDLE_IDENTIFIER}
fi
Then this is what Xcode is telling me, with the last 4 lines being the error:
export XCODE_PRODUCT_BUILD_VERSION=10B61
export XCODE_VERSION_ACTUAL=1010
export XCODE_VERSION_MAJOR=1000
export XCODE_VERSION_MINOR=1010
export XPCSERVICES_FOLDER_PATH=appTest.app/XPCServices
export YACC=yacc
export arch=undefined_arch
export variant=normal
/bin/sh -c /Users/me/Library/Developer/Xcode/DerivedData/Unity-iPhone-faweajoqridawgcwcslethajilfw/Build/Intermediates.noindex/Unity-iPhone.build/Debug-iphoneos/Unity-iPhone.build/Script-4252488AB701AFBAC2FB7A9F.sh
/Users/me/UnityProjects/appTest/Builds/appTestBuild/CrashHunterScript.sh: line 2:
: command not found
/Users/me/UnityProjects/appTest/Builds/appTestBuild/CrashHunterScript.sh: line 15: syntax error: unexpected end of file
Command PhaseScriptExecution failed with a nonzero exit code
I am no expert with shell scripts but I did a bit of searching around and can't tell where the syntax error is. Any ideas?

Related

bash script result a syntax error near unexpected token

I am trying to script my SCREEN access and automate as much as possible my connection to my bastion host.
here my bash code:
#!/bin/bash
# set TERM to xterm-256color
export TERM=xterm-256color
# here we source bashrc
. .bashrc
# Detecting Command Line Arguments
if [ "$1" != "" ]; then
# check if the screen argument exist
if ! screen -list | grep -q "$1"; then
# create screen with new argument
screen -S $1
# At this point, argument is not found on screen
else
# Create it with argument specified.
screen -x $1
# Detecting if default screen exist
elif [[ ! screen -list | grep -q "myscreen" ]]; then
# no default screen exist, Create it !
screen -S myscreen
else
# attache to the default screen
screen -x myscreen
fi
there the output:
$ ./myscreen.sh test123
./myscreen.sh: line 18: syntax error near unexpected token `elif'
./myscreen.sh: line 18: `elif ! screen -list | grep -q "myscreen" ; then'
I tried also to [[ ! EXPR ]] no much better.
any one have a idea?
I reviewed your script and saw that it is missing a fi before the elif. Of course, I write bash code everyday, so this was easy for me to spot. You can use a bash syntax checker to help review your scripts (www.shellcheck.net).
Here's the results provided by that site:
$ shellcheck myscript
Line 8:
if [ "$1" != "" ]; then
^-- SC1009: The mentioned syntax error was in this then clause.
Line 10:
if ! screen -list | grep -q "$1"; then
^-- SC1046: Couldn't find 'fi' for this 'if'.
^-- SC1073: Couldn't parse this if expression. Fix to allow more checks.
Line 18:
elif [[ ! screen -list | grep -q "myscreen" ]]; then
^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.
$
You’re missing a fi statement to close out your internal if-else:
if
if
...
else
...
fi # You were missing this line
elif
...
else
...
fi

ShellScript not working on Pre-session-command (PowerCenter)

The goal is to check the existence of a file and create a blank file if this doesn't exist, using Shell Script on the Pre-session-command (Informatica PowerCenter) like the code below:
ParamDirTrabalho=/dir/powercenter/project1
ParamArq=file.csv
ParamQtdArq=`cat ${ParamDirTrabalho}/${ParamArq} | wc -l`
if [ $ParamQtdArq == 0 ];then touch ${ParamDirTrabalho}/${ParamArq};fi
This is the error:
Message: [Pre/Post Session Command] Process id 10683. Standard output and error:
sh: line 2:
: command not found
cat: /dir/powercenter/project1
/file.csv
: No such file or directory
sh: line 4:
: command not found
I can execute successfully when pointing to a sh file with the code above. But I need to write the code inside the pre-session-command.
Please enclose parameters by double quotes.
ParamDirTrabalho="/dir/powercenter/project1"
ParamArq="file.csv"
Also pls make sure you provide RWX permission to folders.
You cannot get the WC from a file if it doesn't exist at all. That's what the error is "No such file or directory" if I understand it right. What you need to do is check if file exists or not rather than the count and then touch if it doesn't exist.
if [ ! -f filename ];then touch filename; fi
or
if [ -f filename ];then exit 0; else touch filename; fi

Customize "command not found" message in Bash

Is there someway to alter the Bash system error message template so that you can print something in addition to the original message? For example:
Macbook Air:~/Public]$ lfe
-bash: lfe: WTF command not found
or
Macbook Air:~/Public]$ lfe
-bash: lfe: #!&**! command not found
Since Bash 4.0, if the search for a command is unsuccessful, the shell searches for a function called command_not_found_handle. If it doesn't exist, Bash prints a message like this and exits with status 127:
$ foo
-bash: foo: command not found
$ echo $?
127
If it does exist, it is called with the command and its arguments as arguments, so if you have something like
command_not_found_handle () {
echo "It's my handle!"
echo "Arguments: $#"
}
in your .bashrc, Bash will react like this:
$ foo bar
It's my handle!
Arguments: foo bar
Most systems have something much more sophisticated in place, though. My Ubuntu, for example, has this in /etc/bash.bashrc:
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
function command_not_found_handle {
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1"
return $?
elif [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1"
return $?
else
printf "%s: command not found\n" "$1" >&2
return 127
fi
}
fi
and this is sourced from /etc/profile. /usr/lib/command-not-found is a Python script that uses some more Python (CommandNotFound) to basically look up packages that are named like the unknown command, or sound similar:
$ sl
The program 'sl' is currently not installed. You can install it by typing:
sudo apt install sl
$ sedd
No command 'sedd' found, did you mean:
Command 'sed' from package 'sed' (main)
Command 'seedd' from package 'bit-babbler' (universe)
Command 'send' from package 'nmh' (universe)
Command 'send' from package 'mailutils-mh' (universe)
sedd: command not found
So if you want simple customization, you can provide your own command_not_found_handle, and if you want to customize the existing system, you can modify the Python scripts.
But, as mentioned, this requires Bash 4.0 or higher.
Maybe something like:
curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc
then add the following to .bashrc too
preexec() { type "$1" >/dev/null 2>&1 || echo -n 'WTF??? '; }
reload your shell, then try enter some nonexistent command, like bububu
$ bububu
will print
WTF??? -bash: bububu: command not found
Important: read https://github.com/rcaloras/bash-preexec

Error with simple shell script [duplicate]

This question already has answers here:
I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"
(3 answers)
Closed 6 years ago.
I wrote a shell script which automatically set up environment
#!/bin/sh
set path=(/dv/project/ $path)
I change the execution bit by
chmod +x init.sh
When I run it as
./init.sh
It prompted me with error
./init.sh: line 3: syntax error near unexpected token `('
./init.sh: line 3: `set path=(/dv/project/ $path)'
What could be the problem here? Thanks!
If using of set isn't required, just try this:
#!/bin/bash
path=(/dv/project/ $path)
As I have noticed, you're trying to extend your $PATH environment variable, right? There is a better way. Try this approach:
# Extend $PATH without duplicates
function _extend_path() {
if ! $( echo "$PATH" | tr ":" "\n" | grep -qx "$1" ) ; then
PATH="$1:$PATH"
fi
}
# Add custom bin to $PATH
[ -d ~/.bin ] && _extend_path "$HOME/.bin"

bash IF statement doesn't work - getting error

i'm trying write simple IF statement in bash, but no mater what i try i allway get error, that shouldn't rise at all, can someone please take a look at my code and tell me what am I doing wrong?
#!/bin/bash
echo "Checking..."
echo
if [ -f /var/www/git/repos/last_change.txt ];
then
echo "/var/www/git/repos/last_change.txt exists."
cd /var/www &&
git fetch --all &&
git reset --hard origin/develop &&
rm -f "/var/www/git/repos/last_change.txt"
fi;
echo
echo "...done."
I allways get error:
root#machine:/var/www/git/repos# sh check.sh
Checking...
: not found: 3: check.sh: echo
check.sh: 12: check.sh: Syntax error: "fi" unexpected (expecting "then")
Everything seems to be ok, right?
I am glad for any help, thanks.
Judging by error description, most likely your file uses DOS newlines (CRLF). sh expects UNIX newlines (LF). Convert your file with dos2unix your_file_name.sh.

Resources