Syntax error when running bash script in Homestead Vagrant - bash

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.

Related

Errors in bash scripts

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.

Shell script continue after failure

How do I write a shell script that continues execution even if a specific command failed, however I want to output as error later, I tried this:
#!/bin/bash
./node_modules/.bin/wdio wdio.conf.js --spec ./test/specs/login.test.js
rc=$?
echo "print here"
chown -R gitlab-runner /gpc_testes/
chown -R gitlab-runner /gpc_fontes/
exit $rc
However the script stops when the node modules command fails.
You could use
command_that_would_fail || command_failed=1
# More code and even more
.
.
if [ ${command_failed:-0} -eq 1 ]
then
echo "command_that_would_fail failed"
fi
Suppose name of the script is test.sh.
While executing the scripting, execute it with below command
./test.sh 2>>error.log
Error due bad commands won't appear on terminal but will be stored in file error.log which can be referred afterwards.

Execute a statement after logging in to SSH

I am writing a script in which i have to execute a set of statments after logging in via SSH. So i am using this command
ssh -t user#adf-svc-1001.do.e1.bboo.com $COMMAND "&& exec bash -l"
Now this works if i give COMMAND as any single line statement such as ls or mkdir, but its not working if i put the following in COMMAND
COMMAND= "if [ $B==$A ]
then
echo 'ERROR'
fi "
I tried putting \ at the end of each line,changed the indentation,put it into a single line but its throwing error
fi : command not found
syntax error near unexpected token `&&'
The first problem is the ssh statement itself. ssh command should be run like:
ssh -t user#adf-svc-1001.do.e1.bboo.com $COMMAND && exec bash -l
Second one is that COMMAND variable should be assigned as one line like:
COMMAND="if [ $B==$A ]; then echo 'ERROR'; fi;"
I hope this helps.

file basename error ONLY with nohup

I get an error when running the script (test.sh) as nohup. Interestingly, it works fine when ran as $./test.sh
Btw, the OS is X86 Solaris 10
$nohup ./test.sh &
./test.sh: syntax error at line 7: `file_name=$' unexpected
Here is the script test.sh
#!/usr/local/bin/bash
for dt in 20150806
do
stat_files="/storage2/Production_Stats/*"$dt"*"
for file_path in $stat_files
do
file_name=$(basename $file_path)
echo $file_name
done
done
Apparently, I had to use the deprecated back ticks `` and not the $() to make it work on Solaris 10. Thanks

bash for loop work in command line, but failed in script

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.

Resources