Bash syntax error near unexpected token `done' - bash

This file is written in bash. When I run it I get this error:
./q: line 7: syntax error near unexpected token `done'
./q: line 7: `done '**strong text**
The code is:
nohup echo ELMAYET > /dev/null 2> /dev/null &&
if curl -m5 -s --insecure "$1/test/final" | grep "phpshell" > /dev/null;
then
echo "$1/test/final.php" | tee -a final.txt;
fi &
done

Assuming there's nothing else of significance in your script, you simply need to delete the done.
done is a shell keyword. It's used only to mark the end of a while, for, or until loop. If it doesn't mark the end of a loop, it's a syntax error.
Out of curiosity, what did you think the done was for, and why did you add it to the script? If you assumed you need a done to mark the end of the script, that's your mistake. The end of a script doesn't need to be marked in any special way; it's just the end of the file.

Related

Using while loop without process substitution, while allowing global variable value modification inside the loop

I have a script that when I run from a file manager (Filza), it return an error saying
command substitution: syntax error near unexpected token `('.
line 56: `paste -d'\n' <(echo "$var1") <(echo "$var2"))'
But when I run it from a terminal (./myscript.sh), it ran with no error. Here's the code that gave the error:
#!/bin/bash
var1="A
B
C"
var2="1
2
3"
globalvar=0
while read v1 && read v2; do
globalvar=$(echo $v1 $v2)
done<<<$(paste -d'\n' <(echo "$var1") <(echo "$var2"))
As commented below, it's probably some shell doesn't allow process substitution, thus why it failed. This command is running on iOS environment (jailbroken). Is there alternative way to implement this? Thanks in advance!
Try using 'here document' (<<) instead of 'here string' (<<<). It is supported by most shells.
while read v1 && read v2; do
globalvar=$(echo $v1 $v2)
done <<__END__
$(paste -d'\n' <(echo "$var1") <(echo "$var2"))
__END__
The other option is create a shell wrapper that will force bash (from the question, looks like bash is installed and working). Rename original script to script-run, and modify the shell script to call the script-run
#! /bin/sh
exec /bin/bash ${0%/*}/script-run "$#"
Or other equivalent.

Bash script using COMM and SORT issues syntax error near unexpected token

Linux, CentOS - to compare 2 files I use command
comm --check-order -1 --output-delimiter=----- <sort(file1.txt) <sort (file3.txt) > result.txt ;
and it works on shell, but when I try create a bash file - I have
syntax error near unexpected token `('
The script is simplest
#!/bin/bash
cd /var/www/html/compare/ ;
comm --check-order -1 --output-delimiter=----- <sort(file1.txt) <sort (file3.txt) > result.txt ;
exit ;
sh
I already tried variations with escaping of round brackets like
sort\(file1.txt\)
or
sort'(file1.txt)'
but this case shell says
sort(file1.txt)...: No such file or directory
I tried with absolute path like
<sort\(var/www/html/compare/file1.txt\)
same result "No such file"
and I already tried run the script with variations like
sh /a/compare.sh
bash /a/compare.sh
chmod +x /a/compare.sh; ./a/compare.sh
Still same problem.
So I have OR "No such file..." with escaping of brackets - OR "unexpected token"in other cases.
Thanks in advance for any ideas to try, may be should be a right "mix" of syntax ?
After many combinations I found the solution where we need to force BASH with a little specific escaping.
This script works
#!/bin/bash
cd /var/www/html/compare/ ;
`bash -c "comm --check-order -1 --output-delimiter=----- <(sort file1.txt) <(sort file2.txt) > result.txt" ` ;
exit ;
sh
Hopefully will help somebody to save the time.
!/usr/bin/env bash
to solve it, instead of label the command, label the file
Process substitution puts the entire command in parens.
... <(sort ...) ...

script error - cygwin -- pinging to host and checking is it alive or not?

I'm new to scripting.
I downloaded cygwin and Notepad++(I'm using unix option-for writing and saving the ".sh files")
I have below script
Below code is from command $ cat -v pinging.sh
#!/bin/bash
target=$1
# email report when
SUBJECT="Ping failed"
EMAILID="someemailid#gmail.com"
count=$( $SYSTEMROOT/system32/ping -n -c 1 $target | grep 'received')
if [ $count == 0 ];
then
echo "Host : $target is not Alive!! Try again later.. at $(date)" | mail -s "$SUBJECT" $EMAILID
else
echo "Yes! Host is Alive!"
fi
done
But my script is giving error -
$ ./pinging.sh www.google.com
./pinging.sh: line 9: [: ==: unary operator expected
Yes! Host is Alive!
./pinging.sh: line 17: syntax error near unexpected token `done'
./pinging.sh: line 17: `done'
I'm not sure what I am doing wrong here.
The script is giving error
I'm getting- "host is alive" message always even in case of destination unreachable messages too. If I'm using ping www.somesite.com and if I'm getting destination unreachable through cygwin or cmd, this code is giving host is alive.
I also tried if [ $count -et 0 ]; in above code
Please help me!
Best Regards,
The value of the $count variable is not a number. It is a full line of text.
When you expand it in the [ test (without quotes) it gets word-split by the shell and the contents of the [ test become invalid (too many words) and you get your error.
If you quote "$count" you will avoid the error (but still not get the results you want).
You need to filter out only the number from the ping output and then use that in your [ test.
Add set -x to the top of your script to see the commands that are actually being run and you'll see the problem.

syntax error near unexpected token `(' in below code

I have very small shell script.When I am running it run flow. it is giving "syntax error near unexpected token `(". Very basic question but sorry not able to figure out.
foreach i ( `cat list407`)
mkdir cells/${i}
cp /<path>/$i/${i}.gds cells/${i}/${i}.gds
end
Error:
flow: line 1: syntax error near unexpected token `('
flow: line 1: `foreach i ( `cat list407`)'
You've used csh syntax for execution using bash which is causing the error.
Either use csh to execute your script, or using bash say:
while read -r i; do
mkdir "cells/${i}"
cp "/<path>/${i}/${i}.gds" "cells/${i}/${i}.gds"
done < list407
for i in $(cat list407); do
mkdir cells/${i};
cp /<path>/$i/${i}.gds cells/${i}/${i}.gds;
done

Syntax Error in bash while loop

What is the error in that?
j=0
filenames=("")
filedates=("")
while read line
do
filenames[$j]="$line"
filedates[$j]=$(stat -c %y ${filenames[$j]} | cut -d ' ' -f1)
(( j++ ))
done < <(ls -t *.gz)
Out:
script.sh: line 9: syntax error near unexpected token `<'
script.sh: line 9: `done < <(ls -t *.gz)'
Really i don't know the error in that while loop, i tested it on several machine but same problem
The problem is that you're using bash specific <(process substition), but running your script with /bin/sh.
Use bash script.sh instead of sh script.sh, and make sure the shebang uses bash and not sh.
use a for loop
for file in *t.gz
do
...
done

Resources