I am new to shell scripting and stuck in some syntax error in a simple program. I read an integer and compare it with some value to display the result
Please tell me how to rectify it.
#! /bin/bash
read n
if [ "$n" -le 12 ]
then
echo "a kid"
elif[ "$n" -lt 18 ]
then
echo "a teen"
else
echo "an adult"
fi
The error was:
./hello.sh: line 8: syntax error near unexpected token `then'
./hello.sh: line 8: `then'
You're missing a space between elif and [, which causes a parsing error later on.
For future reference, the shellcheck tool is a good way to diagnose errors like this one.
Related
So I have 1 bash scripts,
findFungible.sh
#!/bin/bash
for file in $*;
for word in $(cat $file);
if [ $word == Fungible ];
echo Fungible found
fi
done
done
Which should be checking files if they contain the word fungible.
It's pretty much verbatim out of my lecture example.
So if I run it with bash findFungible.sh
I get:
findFungible.sh: line 2: syntax error near unexpected token "$\r''
'indFungible.sh: line 2: 'for file in $*;
So I think it has something to do with windows putting in extra line \r characters or something. As there is a \r after $.
Then if I run it with sh findFungible.sh
I get:
findFungible.sh: 2: findFungible.sh Syntax error: word unexpected (expecting "do")
Any help would be great thanks.
As someone mentioned in a reply, you have syntax errors in this, meaning, you are missing "do's" and "then's".
#!/bin/bash
for file in $*; do
for word in $(cat $file); do
if [ $word == Fungible ]; then
echo Fungible found
fi
done
done
And yes, like mentioned in the reply, bash is very sensitive to white spaces, new lines and quotes, I'm not getting into too much detail there.
I am a beginner at Bash scripting and I am getting an error saying this when I run my code:
main.sh: line 7: ((: -w /etc/shadow : division by 0 (error token is "etc/shadow ")
The following is the code I wrote in main.sh:
#!/usr/bin/env bash
if [ -e /etc/shadow ]
then
echo "Shadow passwords are enabled."
if (( -w /etc/shadow ))
then
echo "You have permissions to edit /etc/shadow"
else
echo "You do NOT have permissions to edit /etc/shadow"
fi
else
echo "Shadow passwords are not enabled."
fi
The result after running the code also gave:
Shadow passwords are enabled.
You do NOT have permissions to edit /etc/shadow
This was given before the error message. Does anyone have any suggestions for how to fix this problem and what the error message means? Thanks!
You should use [[ ... ]] (preferred, bash-only) or [ ... ] (can cause problems, POSIX compliant) instead of (( ... )): they are adequate for comparing text, while (( ... )) is an arithmetic context and only accepts mathematical operations. The error occurs because it tries to use the /s in the path for division.
That error counts as a false for the if, making you run the else block.
if [[ -w /etc/shadow ]]
then
# ...
A good reference for using if in bash is the Bash Beginner Guide.
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.
This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 7 years ago.
I'm at a loss as to why this is giving a syntax error. Any thoughts?
#!/bin/bash
if [ `date +%H` -lt 11 ] ; then exit 0;
fi
if [ `date +%H` -gt 14 ] ; then
if[ `date +%H` -lt 20 ] ; then # <--- this line is the culprit, it seems
exit 0;
fi
fi
When run, I get:
./get.sh: line 7: syntax error near unexpected token `then'
./get.sh: line 7: ` if[ `date +%H` -lt 20 ] ; then '
The reason that this is a syntax error is that [ isn't part of the shell syntax; it's actually a command. Originally it was just a symlink to the test command. It still is, but it's also a built-in command in bash and other Bourne-derived shells.
if is a shell keyword, but the shell sees if[, not if. Because it didn't see an if, it doesn't know what to do when it sees then. (Actually, it knows exactly what to do: print a syntax error message.)
...
A bit of experimentation shows that it's not quite as simple as I thought it was. I tried creating a command called if[ and putting it in a directory in my $PATH. When I type just if[ at the prompt, the shell asks for more input. I actually don't know what it's looking for, but apparently the [ character is specially treated by the shell. The shell just doesn't split if[ into the if keyword and the [ command (as you might reasonably expect based on how other languages work). (If I really wanted to execute that command, I could type \if[ or "if[" -- or give it a sane name in the first place.
In any case, that last part probably doesn't matter; adding a space character will fix the problem.
Add space before [
if [ `date +%H` -lt 20 ]
if[ `date +%H` -lt 20 ] ;
you need to place a space after if
if [ `date +%H` -lt 20 ] ;
I have the following bash code, which is copied and pasted from "bash cookbook" (1st edition):
#!/bin/bash
VERBOSE=0;
if [[ $1 =-v ]]
then
VERBOSE=1;
shift;
fi
When I run this (bash 4.0.33), I get the following syntax error:
./test.sh: line 4: conditional binary operator expected
./test.sh: line 4: syntax error near `=-v'
./test.sh: line 4: `if [[ $1 =-v ]]'
Is this as simple as a misprint in the bash cookbook, or is there a version incompatibility or something else here? What would the most obvious fix be? I've tried various combinations of changing the operator, but I'm not really familiar with bash scripting.
Bash uses spaces to tokenise scripts. The line:
if [[ $1 =-v ]]
should be:
if [[ $1 = -v ]]