Syntax error near unexpected token 'else'
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I'm trying to create a script that will remove certain parts of a txt file full of status info and other various text. I can't end the main if statement. Also I'm trying to get it to delete the oldest status info by putting the date command into the script that generates the output.txt. Is there a way to make it delete the oldest info. I'm mainly trying to get the if statement to end. Thanks. (Also, I originally had the else if as elif's, but I got the same results.)
./count.sh: line 30: syntax error near unexpected token else'
else'
./count.sh: line 30:
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
else
echo "You're good, homie. It's under 500"
fi
linux bash syntax
add a comment |Â
up vote
2
down vote
favorite
I'm trying to create a script that will remove certain parts of a txt file full of status info and other various text. I can't end the main if statement. Also I'm trying to get it to delete the oldest status info by putting the date command into the script that generates the output.txt. Is there a way to make it delete the oldest info. I'm mainly trying to get the if statement to end. Thanks. (Also, I originally had the else if as elif's, but I got the same results.)
./count.sh: line 30: syntax error near unexpected token else'
else'
./count.sh: line 30:
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
else
echo "You're good, homie. It's under 500"
fi
linux bash syntax
1
If $1 (the first commandline argument) is set and is a filename, yourwc|awk
pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection:WCOUNT=$(wc -l <output.txt)
. Also yourif [[ "grep something output.txt" ]]
tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.
â dave_thompson_085
Feb 16 at 5:02
" I originally had the else if as elif's, but I got the same results" -- changing them toelif
, as in Matt's answer should prevent theunexpected token else
error. If you're still seeing that message, can you show us theelif
version of your script that produces it?
â JigglyNaga
Feb 16 at 11:23
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to create a script that will remove certain parts of a txt file full of status info and other various text. I can't end the main if statement. Also I'm trying to get it to delete the oldest status info by putting the date command into the script that generates the output.txt. Is there a way to make it delete the oldest info. I'm mainly trying to get the if statement to end. Thanks. (Also, I originally had the else if as elif's, but I got the same results.)
./count.sh: line 30: syntax error near unexpected token else'
else'
./count.sh: line 30:
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
else
echo "You're good, homie. It's under 500"
fi
linux bash syntax
I'm trying to create a script that will remove certain parts of a txt file full of status info and other various text. I can't end the main if statement. Also I'm trying to get it to delete the oldest status info by putting the date command into the script that generates the output.txt. Is there a way to make it delete the oldest info. I'm mainly trying to get the if statement to end. Thanks. (Also, I originally had the else if as elif's, but I got the same results.)
./count.sh: line 30: syntax error near unexpected token else'
else'
./count.sh: line 30:
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
else if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
else
echo "You're good, homie. It's under 500"
fi
linux bash syntax
edited Feb 16 at 8:27
muru
33.4k577142
33.4k577142
asked Feb 16 at 0:00
Desert
111
111
1
If $1 (the first commandline argument) is set and is a filename, yourwc|awk
pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection:WCOUNT=$(wc -l <output.txt)
. Also yourif [[ "grep something output.txt" ]]
tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.
â dave_thompson_085
Feb 16 at 5:02
" I originally had the else if as elif's, but I got the same results" -- changing them toelif
, as in Matt's answer should prevent theunexpected token else
error. If you're still seeing that message, can you show us theelif
version of your script that produces it?
â JigglyNaga
Feb 16 at 11:23
add a comment |Â
1
If $1 (the first commandline argument) is set and is a filename, yourwc|awk
pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection:WCOUNT=$(wc -l <output.txt)
. Also yourif [[ "grep something output.txt" ]]
tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.
â dave_thompson_085
Feb 16 at 5:02
" I originally had the else if as elif's, but I got the same results" -- changing them toelif
, as in Matt's answer should prevent theunexpected token else
error. If you're still seeing that message, can you show us theelif
version of your script that produces it?
â JigglyNaga
Feb 16 at 11:23
1
1
If $1 (the first commandline argument) is set and is a filename, your
wc|awk
pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection: WCOUNT=$(wc -l <output.txt)
. Also your if [[ "grep something output.txt" ]]
tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.â dave_thompson_085
Feb 16 at 5:02
If $1 (the first commandline argument) is set and is a filename, your
wc|awk
pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection: WCOUNT=$(wc -l <output.txt)
. Also your if [[ "grep something output.txt" ]]
tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.â dave_thompson_085
Feb 16 at 5:02
" I originally had the else if as elif's, but I got the same results" -- changing them to
elif
, as in Matt's answer should prevent the unexpected token else
error. If you're still seeing that message, can you show us the elif
version of your script that produces it?â JigglyNaga
Feb 16 at 11:23
" I originally had the else if as elif's, but I got the same results" -- changing them to
elif
, as in Matt's answer should prevent the unexpected token else
error. If you're still seeing that message, can you show us the elif
version of your script that produces it?â JigglyNaga
Feb 16 at 11:23
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
7
down vote
The issue here is that by using else if
rather than elif
, you are adding nesting to your program, and will need to add more fi
statements to terminate the additional conditional statements.
Here is an edited version of your program with slightly different indentation to demonstrate what I mean
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
fi
fi
fi
else
echo "You're good, homie. It's under 500"
fi
Alternatively, you could just use elif
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
else
echo "You're good, homie. It's under 500"
fi
(Also, slight nitpick, you're checking if there are more than 50 lines, but your output message indicates that you are checking for 500. While technically, there are under 500 lines, it is a little misleading :p)
EDIT: As others have pointed out, this will fix your syntax error, but there are still other issues with the script. I would recommend reading their comments/answers too.
The answer does not address the fact that-n "grep ..."
is an odd thing to test (always true as the stringgrep ...
is non-empty).
â Kusalananda
Feb 16 at 8:24
Want to pick up on[[ "grep CLIENTSTART output.txt" != null ]]
and its equally broken counterparts, too?
â roaima
Feb 16 at 8:25
add a comment |Â
up vote
1
down vote
The reason you're getting a syntax error is that your else if
statements should be elif
.
If you give wc -l
two files, it will produce three lines of output:
$ wc -l .profile .profile
30 .profile
30 .profile
60 total
This affects the very first if
test where $WCOUNT
is unquoted, and its expansion introduces another syntax error.
You probably want
WCOUNT=$( wc -l <output.txt )
Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt"
. These tests will always be true.
To test whether a string is present in a file, use
if grep -Fq 'fixed string' filename; then
e.g.
if grep -Fq 'CLIENTSTART' output.txt; then
# do something
elif grep -Fq 'DHCPSTART' output.txt; then
# do something
else
# do something
fi
The -q
stops grep
from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if
statement work (the flag also makes grep
stop reading the file at the first match). The -F
flag makes grep
treat the given pattern as a fixed string rather than as a regular expression.
Suggestion for your script's internals:
tmpfile=$(mktemp)
for string in CLIENT DHCP DNS WEB; do
if grep -Fq "$stringSTART" output.txt; then
sed "/***$stringSTART/,/$stringEND***/d"
output.txt >"$tmpfile"
break
fi
done
if [ -s "$tmpfile" ]; then
mv "$tmpfile" output.txt
else
echo 'Can not help you'
rm -f "$tmpfile"
fi
The [ -s "$tmpfile" ]
test will succeed if the file $tmpfile
has size greater than zero (which it will have if sed
was invoked).
The for
loop with the break
imitates your if ... then ... elif
logic but in a more compact way.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
The issue here is that by using else if
rather than elif
, you are adding nesting to your program, and will need to add more fi
statements to terminate the additional conditional statements.
Here is an edited version of your program with slightly different indentation to demonstrate what I mean
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
fi
fi
fi
else
echo "You're good, homie. It's under 500"
fi
Alternatively, you could just use elif
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
else
echo "You're good, homie. It's under 500"
fi
(Also, slight nitpick, you're checking if there are more than 50 lines, but your output message indicates that you are checking for 500. While technically, there are under 500 lines, it is a little misleading :p)
EDIT: As others have pointed out, this will fix your syntax error, but there are still other issues with the script. I would recommend reading their comments/answers too.
The answer does not address the fact that-n "grep ..."
is an odd thing to test (always true as the stringgrep ...
is non-empty).
â Kusalananda
Feb 16 at 8:24
Want to pick up on[[ "grep CLIENTSTART output.txt" != null ]]
and its equally broken counterparts, too?
â roaima
Feb 16 at 8:25
add a comment |Â
up vote
7
down vote
The issue here is that by using else if
rather than elif
, you are adding nesting to your program, and will need to add more fi
statements to terminate the additional conditional statements.
Here is an edited version of your program with slightly different indentation to demonstrate what I mean
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
fi
fi
fi
else
echo "You're good, homie. It's under 500"
fi
Alternatively, you could just use elif
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
else
echo "You're good, homie. It's under 500"
fi
(Also, slight nitpick, you're checking if there are more than 50 lines, but your output message indicates that you are checking for 500. While technically, there are under 500 lines, it is a little misleading :p)
EDIT: As others have pointed out, this will fix your syntax error, but there are still other issues with the script. I would recommend reading their comments/answers too.
The answer does not address the fact that-n "grep ..."
is an odd thing to test (always true as the stringgrep ...
is non-empty).
â Kusalananda
Feb 16 at 8:24
Want to pick up on[[ "grep CLIENTSTART output.txt" != null ]]
and its equally broken counterparts, too?
â roaima
Feb 16 at 8:25
add a comment |Â
up vote
7
down vote
up vote
7
down vote
The issue here is that by using else if
rather than elif
, you are adding nesting to your program, and will need to add more fi
statements to terminate the additional conditional statements.
Here is an edited version of your program with slightly different indentation to demonstrate what I mean
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
fi
fi
fi
else
echo "You're good, homie. It's under 500"
fi
Alternatively, you could just use elif
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
else
echo "You're good, homie. It's under 500"
fi
(Also, slight nitpick, you're checking if there are more than 50 lines, but your output message indicates that you are checking for 500. While technically, there are under 500 lines, it is a little misleading :p)
EDIT: As others have pointed out, this will fix your syntax error, but there are still other issues with the script. I would recommend reading their comments/answers too.
The issue here is that by using else if
rather than elif
, you are adding nesting to your program, and will need to add more fi
statements to terminate the additional conditional statements.
Here is an edited version of your program with slightly different indentation to demonstrate what I mean
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
fi
fi
fi
else
echo "You're good, homie. It's under 500"
fi
Alternatively, you could just use elif
WCOUNT=$(wc -l output.txt $1 | awk 'print $1')
if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
elif [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
else
echo "You're good, homie. It's under 500"
fi
(Also, slight nitpick, you're checking if there are more than 50 lines, but your output message indicates that you are checking for 500. While technically, there are under 500 lines, it is a little misleading :p)
EDIT: As others have pointed out, this will fix your syntax error, but there are still other issues with the script. I would recommend reading their comments/answers too.
edited Feb 17 at 0:03
answered Feb 16 at 0:25
Matt
22718
22718
The answer does not address the fact that-n "grep ..."
is an odd thing to test (always true as the stringgrep ...
is non-empty).
â Kusalananda
Feb 16 at 8:24
Want to pick up on[[ "grep CLIENTSTART output.txt" != null ]]
and its equally broken counterparts, too?
â roaima
Feb 16 at 8:25
add a comment |Â
The answer does not address the fact that-n "grep ..."
is an odd thing to test (always true as the stringgrep ...
is non-empty).
â Kusalananda
Feb 16 at 8:24
Want to pick up on[[ "grep CLIENTSTART output.txt" != null ]]
and its equally broken counterparts, too?
â roaima
Feb 16 at 8:25
The answer does not address the fact that
-n "grep ..."
is an odd thing to test (always true as the string grep ...
is non-empty).â Kusalananda
Feb 16 at 8:24
The answer does not address the fact that
-n "grep ..."
is an odd thing to test (always true as the string grep ...
is non-empty).â Kusalananda
Feb 16 at 8:24
Want to pick up on
[[ "grep CLIENTSTART output.txt" != null ]]
and its equally broken counterparts, too?â roaima
Feb 16 at 8:25
Want to pick up on
[[ "grep CLIENTSTART output.txt" != null ]]
and its equally broken counterparts, too?â roaima
Feb 16 at 8:25
add a comment |Â
up vote
1
down vote
The reason you're getting a syntax error is that your else if
statements should be elif
.
If you give wc -l
two files, it will produce three lines of output:
$ wc -l .profile .profile
30 .profile
30 .profile
60 total
This affects the very first if
test where $WCOUNT
is unquoted, and its expansion introduces another syntax error.
You probably want
WCOUNT=$( wc -l <output.txt )
Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt"
. These tests will always be true.
To test whether a string is present in a file, use
if grep -Fq 'fixed string' filename; then
e.g.
if grep -Fq 'CLIENTSTART' output.txt; then
# do something
elif grep -Fq 'DHCPSTART' output.txt; then
# do something
else
# do something
fi
The -q
stops grep
from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if
statement work (the flag also makes grep
stop reading the file at the first match). The -F
flag makes grep
treat the given pattern as a fixed string rather than as a regular expression.
Suggestion for your script's internals:
tmpfile=$(mktemp)
for string in CLIENT DHCP DNS WEB; do
if grep -Fq "$stringSTART" output.txt; then
sed "/***$stringSTART/,/$stringEND***/d"
output.txt >"$tmpfile"
break
fi
done
if [ -s "$tmpfile" ]; then
mv "$tmpfile" output.txt
else
echo 'Can not help you'
rm -f "$tmpfile"
fi
The [ -s "$tmpfile" ]
test will succeed if the file $tmpfile
has size greater than zero (which it will have if sed
was invoked).
The for
loop with the break
imitates your if ... then ... elif
logic but in a more compact way.
add a comment |Â
up vote
1
down vote
The reason you're getting a syntax error is that your else if
statements should be elif
.
If you give wc -l
two files, it will produce three lines of output:
$ wc -l .profile .profile
30 .profile
30 .profile
60 total
This affects the very first if
test where $WCOUNT
is unquoted, and its expansion introduces another syntax error.
You probably want
WCOUNT=$( wc -l <output.txt )
Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt"
. These tests will always be true.
To test whether a string is present in a file, use
if grep -Fq 'fixed string' filename; then
e.g.
if grep -Fq 'CLIENTSTART' output.txt; then
# do something
elif grep -Fq 'DHCPSTART' output.txt; then
# do something
else
# do something
fi
The -q
stops grep
from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if
statement work (the flag also makes grep
stop reading the file at the first match). The -F
flag makes grep
treat the given pattern as a fixed string rather than as a regular expression.
Suggestion for your script's internals:
tmpfile=$(mktemp)
for string in CLIENT DHCP DNS WEB; do
if grep -Fq "$stringSTART" output.txt; then
sed "/***$stringSTART/,/$stringEND***/d"
output.txt >"$tmpfile"
break
fi
done
if [ -s "$tmpfile" ]; then
mv "$tmpfile" output.txt
else
echo 'Can not help you'
rm -f "$tmpfile"
fi
The [ -s "$tmpfile" ]
test will succeed if the file $tmpfile
has size greater than zero (which it will have if sed
was invoked).
The for
loop with the break
imitates your if ... then ... elif
logic but in a more compact way.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The reason you're getting a syntax error is that your else if
statements should be elif
.
If you give wc -l
two files, it will produce three lines of output:
$ wc -l .profile .profile
30 .profile
30 .profile
60 total
This affects the very first if
test where $WCOUNT
is unquoted, and its expansion introduces another syntax error.
You probably want
WCOUNT=$( wc -l <output.txt )
Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt"
. These tests will always be true.
To test whether a string is present in a file, use
if grep -Fq 'fixed string' filename; then
e.g.
if grep -Fq 'CLIENTSTART' output.txt; then
# do something
elif grep -Fq 'DHCPSTART' output.txt; then
# do something
else
# do something
fi
The -q
stops grep
from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if
statement work (the flag also makes grep
stop reading the file at the first match). The -F
flag makes grep
treat the given pattern as a fixed string rather than as a regular expression.
Suggestion for your script's internals:
tmpfile=$(mktemp)
for string in CLIENT DHCP DNS WEB; do
if grep -Fq "$stringSTART" output.txt; then
sed "/***$stringSTART/,/$stringEND***/d"
output.txt >"$tmpfile"
break
fi
done
if [ -s "$tmpfile" ]; then
mv "$tmpfile" output.txt
else
echo 'Can not help you'
rm -f "$tmpfile"
fi
The [ -s "$tmpfile" ]
test will succeed if the file $tmpfile
has size greater than zero (which it will have if sed
was invoked).
The for
loop with the break
imitates your if ... then ... elif
logic but in a more compact way.
The reason you're getting a syntax error is that your else if
statements should be elif
.
If you give wc -l
two files, it will produce three lines of output:
$ wc -l .profile .profile
30 .profile
30 .profile
60 total
This affects the very first if
test where $WCOUNT
is unquoted, and its expansion introduces another syntax error.
You probably want
WCOUNT=$( wc -l <output.txt )
Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt"
. These tests will always be true.
To test whether a string is present in a file, use
if grep -Fq 'fixed string' filename; then
e.g.
if grep -Fq 'CLIENTSTART' output.txt; then
# do something
elif grep -Fq 'DHCPSTART' output.txt; then
# do something
else
# do something
fi
The -q
stops grep
from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if
statement work (the flag also makes grep
stop reading the file at the first match). The -F
flag makes grep
treat the given pattern as a fixed string rather than as a regular expression.
Suggestion for your script's internals:
tmpfile=$(mktemp)
for string in CLIENT DHCP DNS WEB; do
if grep -Fq "$stringSTART" output.txt; then
sed "/***$stringSTART/,/$stringEND***/d"
output.txt >"$tmpfile"
break
fi
done
if [ -s "$tmpfile" ]; then
mv "$tmpfile" output.txt
else
echo 'Can not help you'
rm -f "$tmpfile"
fi
The [ -s "$tmpfile" ]
test will succeed if the file $tmpfile
has size greater than zero (which it will have if sed
was invoked).
The for
loop with the break
imitates your if ... then ... elif
logic but in a more compact way.
edited Feb 16 at 13:41
answered Feb 16 at 8:55
Kusalananda
103k13202318
103k13202318
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424502%2fsyntax-error-near-unexpected-token-else%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
If $1 (the first commandline argument) is set and is a filename, your
wc|awk
pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection:WCOUNT=$(wc -l <output.txt)
. Also yourif [[ "grep something output.txt" ]]
tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.â dave_thompson_085
Feb 16 at 5:02
" I originally had the else if as elif's, but I got the same results" -- changing them to
elif
, as in Matt's answer should prevent theunexpected token else
error. If you're still seeing that message, can you show us theelif
version of your script that produces it?â JigglyNaga
Feb 16 at 11:23