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 ]]
Related
I try to built a Programm where I can create Text-Files and folders. The user can type in i.a. the directory. To check if the given path exits I created a while-loop but it doesn’t work.
My question is where are my errors and how can I fix them.
while [[ test -e $path == false]]
do echo “type in a valid path”
read -r path
done
Error:
./Create: line 28: conditional binary operator expected
./Create: line 28: syntax error near `e'
./Create: line 28: `while [[ test -e $path == false ]]'
I found a couple of issues:
You should use [[ or test, but not both
false is not a constant in bash
The fix:
while [[ ! -e $path ]]
do
....
done
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 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.
I just started doing shell scripts and getting unknown operand error while using regex in if statement. i searched google but did not get anything
IP="172.21.1.1"
if [[ "$IP" =~ /d ]] ; then
echo "qqq"
fi
Getting error as
sh: =~: unknown operand
Bash version is : BusyBox v1.19.3 (2012-01-31 08:57:52 PST) built-in shell (ash)
This is happening because the operator =~ doesn't existe for bash.
As see you are trying to use a Regex to compare your variables. I recommend to use the expr command. Here is an example:
IP="172.21.1.1"
if [[ $(expr match "$IP" 'my_regex') != 0 ]]; then echo "qqq"; fi;
I have a script to match a pattern, and if it matches, I used the match to append to a variable.
My script works on Bash v3.2.57 and fails on v4.3.30.
Could someone tell me what is wrong with the second ifcondition that matches a pattern here?
#!/bin/sh
if [ -f .file-to-read ]; then
while read p; do
echo "yes"
if [[ $p =~ "#user/"(.+)"#"[0-9]+"."[0-9]+"."[0-9]+ ]]
then
var="$var<#user/${BASH_REMATCH[1]}|$p>\\n"
fi
done < .file-to-read
fi
The error message is
Syntax error: "(" unexpected (expecting "then")
I figured out the issue. The problem was the file was being executed in #!/bin/sh when it should have been #!/bin/bash