I have a couple of errors in my script which I built in CentOS then deployed to Unix. I have the shebang #!/bin/bash on top of my scripts and execute my script using bash myscript.sh
LINE in script:
existing[0]=""
ERROR: existing[0]=: not found
LINE in script: not sure if its this -
while IFS='' read -r line || [[ -n "$line" ]]; do
or this one -
if [[ $sftp_status != 0 ]]; then
ERROR: syntax error at line 118: `i=$' unexpected
LINE in script:
i=$((i + 1))
if you have the shebang line on top, you can set the execute permission and run the script as ./<scriptname>. You dont need the bash <scriptname>.
Those syntax seems to valid to me and i will doubt if this is bash. Try /bin/bash <scriptname> and see if it helps.
Related
On my gitlab CI I am running the following simple script (.gitlab-ci.yml):
STR=$(cat $FILE)
if grep -q "substring" <<< "$STR"; then echo "ok"; fi
Unfortunatley this gives me the error
/bin/sh: eval: line 100: syntax error: unexpected redirection
Running the same command locally as a script is working as expected:
#!/bin/sh
FILE="./file.txt"
STR=$(cat $FILE)
if grep -q "substring" <<< "$STR"; then
echo "ok"
fi
The file has the content:
This has a substring somewhere
/bin/sh is not bash and <<< is a bash extension not available on every shell. Install bash, change shebang to /bin/bash and make sure the script is run under bash or use posix compatible syntax printf "%s\n" "$str" | grep...
Note: UPPER CASE VARIABLES are by convention reserved for exported variables, like IFS COLUMNS PWD UID EUID LINES etc. Use lower case variables in your scripts.
I am getting this error:
[[: command not found
Here is my code:
#!/usr/bin/env bash
IS_PYSTACHE_INSTALLED=$(pip list --format=columns | grep "pystache ")
if [[ ! -z "${IS_PYSTACHE_INSTALLED}" ]]; then
echo " > Installing pystache"
pip install pystache
else
echo " > Pystache is already installed"
fi
What am I doing wrong?
if you are unsure of your default shell, go to the terminal and do a echo $SHELL, if you see #/bin/bash then chmod your script and run it like this ./filename.sh but make sure your hashbang line is #!/usr/bin/env bash. If you want to change your default shell to bash use the chsh command
I'm trying to run a script in Vagrant (vagrant ssh), however I am getting the error below:
tests/unit/runTests.sh: 4: tests/unit/runTests.sh: Syntax error: "(" unexpected
Command: sh tests/unit/runTests.sh
The script runs fine outside for vagrant and the only script info I've been able to find about vagrant are related to provisions, which is not what I'm doing in this case.
Script:
#!/bin/bash
i=0
fails=()
for d in tests/unit/*/ ; do
eval "vendor/bin/peridot -c tests/unit/peridot.php $d"
if [ $? -eq 1 ]
then
fails[$i]=$d
fi
wait
done
for ((j=0; j < ${#fails[#]}; j++)) do
echo "${fails[$j]}"
done
I've read about "(" syntax errors being related to dash, but I am using the suggestions that those posts had (#!/bin/bash).
Thanks for any suggestions.
Command: sh tests/unit/runTests.sh
It means that you (vagrant) is trying to run your script in 'sh', not in 'bash'. In this case, the first line (#!/bin/bash) is ignored and 'sh' interpret doesn't expect '(' character.
#!/bin/bash
if [ ! -f readexportfile ]; then
echo "readexportfile does not exist"
exit 0
fi
The above is part of my script. When the current shell is /bin/csh my script fails with the following error:
If: Expression Syntax
Then: Command not found
If I run bash and then run my script, it runs fine(as expected).
So the question is: If there is any way that myscript can change the current shell and then interpretate rest of the code.
PS: If i keep bash in my script, it changes the current shell and rest of the code in script doesn't get executed.
The other replies are correct, however, to answer your question, this should do the trick:
[[ $(basename $SHELL) = 'bash' ]] || exec /bin/bash
The exec builtin replaces the current shell with the given command (in this case, /bin/bash).
You can use SHEBANG(#!) to overcome your issue.
In your code you are already using she-bang but make sure it is first and foremost line.
$ cat test.sh
#!/bin/bash
if [ ! -f readexportfile ]; then
echo "readexportfile does not exist"
exit 0
else
echo "No File"
fi
$ ./test.sh
readexportfile does not exist
$ echo $SHELL
/bin/tcsh
In the above code even though I am using CSH that code executed as we mentioned shebang in the code. In case if there is no shebang then it will take the help of shell in which you are already logged in.
In you case you also check the location of bash interpreter using
$ which bash
or
$ cat /etc/shells |grep bash
When a run a for statement in debian bash command line, it works fine.
But when I run it in a sh script or run it with bash command, it's keeping report "error near unexpected token `do'"
Where is the difference?
[leon#www] ~/tmp $ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[leon#www] ~/tmp $ bash for i in {1..10}; do echo $i; done
-bash: syntax error near unexpected token `do'
BTW, all works fine in centos enviorment.
Use the -c option so that bash reads the commands from the string you pass in. Also, use single quotes around the command.
bash -c 'for i in {1..10}; do echo $i; done'
your bash command line ends with the first ;
so it gets executed separately as:
bash for i in {1..10};
do echo $i;
done
and man bash says command argument should be a file to load: bash [options] [file]
You can wrap all your script inside inverted commas or in a file. Because here, you're doing bash for i in {1..10} then do echo $i and so on. You should use -c option if you don't put it in a file.