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"
Related
This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 6 months ago.
I am getting bad substitution error on running the following shell script. (Line numbers written just for reference):
Line 11> SCENARIO_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
Line 12> SCENARIO_PATH="${SCENARIO_DIR}/scenarios"
The error in TeamCity is
| ./k6-run-all.sh: line 12: syntax error: bad substitution
Please note that on running this in local, I do not get this error and the scenario path is correctly extracted. But when I run this on TeamCity (which runs on Docker) it is giving me the above error.
Scenario path in my local is: /Users/sonaliagrawal/Documents/antman/src/scenarios/full-card-visa
Scenario path in TeamCity is extracting correctly despite the error which is:
//scenarios/full-card-visa
Solution tried:
Since in TeamCity, SCENARIO_DIR is itself just / hence I wrote an if then else to handle it, but it didn't help solve the substitution error, it just corrected the path to /scenarios/full-card-visa. The code I had added is as follows-
SCENARIO_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
echo "Scenario directory $SCENARIO_DIR"
SCENARIO_PATH=""
if [[ "$SCENARIO_DIR" = "/" ]]; then
SCENARIO_PATH="/scenarios"
else
SCENARIO_PATH="${SCENARIO_DIR}/scenarios"
fi
Reference:
In case it helps, Dockerfile is as follows:
FROM loadimpact/k6:0.34.1
COPY ./src/lib /lib
COPY ./src/scenarios /scenarios
COPY ./src/k6-run-all.sh /k6-run-all.sh
WORKDIR /
ENTRYPOINT []
CMD ["sh", "-c", "./k6-run-all.sh"]
It's because your script isn't being executed as a bash script. Put the following on the top of the .sh file.
#!/bin/bash
This question already has answers here:
"ls: not found" after running "read PATH"
(2 answers)
Getting "command not found" error in bash script
(6 answers)
Closed 12 months ago.
I am trying to gather all of my TODOs and make one file out of them.
#!/bin/bash
# Script for getting all TODOS
PATH="~/documents/obsidian-notes/"
OUTPUT="/home/fish/documents/obsidian-notes/TODO.md"
echo "#TODO" > $OUTPUT
grep -hr --exclude-dir=plugins "\bTODO\b:.*" $PATH | awk '{print $0,"\n"}' >> $OUTPUT
If I run each line in my prompt it works perfectly. When I add them to a script and make it executable and run it I get
./obtodo.sh: line 10: grep: command not found
./obtodo.sh: line 10: awk: command not found
./obtodo.sh: line 12: chown: command not found
I tried running as sudo and I made the script executable with chmod a+x
$PATH is a special variable to the shell. It defines the list of directories to be searched when executing subcommands.
Bash won't know where grep, awk or chown are. Please use different variable name instead of $PATH.
Try
#!/bin/bash
# Script for getting all TODOS
xPATH="~/documents/obsidian-notes/"
OUTPUT="/home/fish/documents/obsidian-notes/TODO.md"
echo "#TODO" > $OUTPUT
grep -hr --exclude-dir=plugins "\bTODO\b:.*" $xPATH | awk '{print $0,"\n"}' >> $OUTPUT
This question already has an answer here:
in bash, heredoc inside function returns syntax error
(1 answer)
Closed 2 years ago.
I'm trying to put the result in a variable but it didn't work
Syntax error: end of file unexpected (expecting ")")
#!/bin/bash
test="$(lftp -u ns454575.ip-154-145-259.eu,NGFYv85MTI ftpback-rbx2-588.mybackup.ovh.net << EOF
cd /backup/site
ls
bye
EOF)"
You shouldn't add something after here docs. You can validate this by using shellcheck.
#!/bin/bash
test="$(lftp -u ns454575.ip-154-145-259.eu,NGFYv85MTI ftpback-rbx2-588.mybackup.ovh.net <<-EOF
cd /backup/site
ls
bye
EOF
)"
p.s. you know that test is a command as well? :-)
This question already has an answer here:
Find "command not found" when executed in bash loop
(1 answer)
Closed 4 years ago.
When running this script:
#!/bin/sh -ex
if [[ $# -ne 1 ]]; then
echo "./import-public-ssh-key.sh <absolute path to public key>"
exit 1;
fi
PATH=$1
KEY=$(basename ${PATH})
I get:
./import-public-ssh-key.sh: line 9: basename: command not found
without the subshell basename works:
$ basename /Users/mles/.ssh/id_rsa.pub
id_rsa.pub
Why is basename not working in the subshell? I'm on a mac if this is relevant.
You reset the PATH. Don't do that. The shell searches all the directories listed in PATH, and you have changed it so that PATH no longer contains the directory that contains basename.
The following bash script creates a directory if not exists and writes a file to it. Now, if directory exists, it retrieves a list of files of this directory encapsulated in an array. This is the code:
if [ -d $ETC_DIR ]; then
echo " * wan27 has been found on your system"
echo " * checking for installed versions"
versions=( `ls $ETC_DIR` ) # line 27
else
echo " * First time installation! Creating etc directory now..."
mkdir $ETC_DIR
echo "$VERSION\n$USERi\n`date +%Y%m%d%H%M%S`\n$ROOT_DIR" > $ETC_DIR/install_$VERSION.txt
fi
And this is what the terminal outputs:
27: Syntax error: "(" unexpected (expecting "fi")
So, line 27 actually is this one in first excerpt of code:
versions=( `ls $ETC_DIR` )
What am I doing wrong? I've tried added semi-colons as well but ended up with the same result...
Your script relies on a Bash feature (creating an array using assignment with parentheses), but it's being run by the Bourne shell.
Change the first line of your script to:
#!/bin/bash