Cant get "=~" to work. Keep getting "[: =~: binary operator expected" [closed] - bash

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
When I try and use if [ "$1" =~ "$regex" ]; then
I keep on getting back the same error: [: =~: binary operator expected
Here is an example function where I get the error:
char_check() {
regex='^[]0-9a-zA-Z,!^`#{}=().;/~_|[-]*$'
if [ "$1" =~ "$regex" ]; then
echo "No illegal characters."
else
echo "Illegal characters."
fi
}
Any response would be a great help.

Regular expressions require [[ expression ]] as shown in the man page of bash.
Also you must not quote the regex itself, e.g. you should prefer $regex over "$regex"
char_check() {
regex='^[]0-9a-zA-Z,!^`#{}=().;/~_|[-]*$'
if [[ "$1" =~ $regex ]]; then
echo "No illegal characters."
else
echo "Illegal characters."
fi
}
If you quote the regex then you are trying to match the string, not the pattern. In other words it will match the weird string '^[]0-9a-zA-Z,!^`#{}=().;/~_|[-]*$'

Related

Bash script to find factorial [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The program takes input but show the following errors, how to resolve those errors ?
[ is not mere syntax, it's a command, therefore it needs space to separate it from its arguments.
while [ $counter -le $number ]
# .....^....................^
Make sure you validate that number is actually only digits.
Some other comments:
Use $(...) instead of `...` -- see
https://github.com/koalaman/shellcheck/wiki/SC2006
for more details.
bash can do arithmetic, you don't need to call out to expr. See
Arithmetic Expansion
and Shell Arithmetic
in the manual.
There is also an arithmetic conditional construct (analogous to the
string-oriented [[...]] conditional construct) -- see
Conditional Constructs
and scroll down to ((...)) (there's no direct link).
I think you need to add a space after [ and before ]:
#!/bin/bash
echo "Enter the number to find it's factorial"
read number
total=1
counter=1
while [ $counter -le $number ];
do
total=` expr $counter \* $total`
counter=` expr $counter + 1`
done
echo $total
Working here on Ubuntu:
$ ./factorial.sh
Enter the number to find it's factorial
5
120
Actually, on line 7, you forgot the completing backtick.

need help understanding bash syntax [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to understand bash syntax a bit better, and I need some help with a while loop:
The following script works:
#!/bin/bash
#
counter=2
mystring=testdir
while [ $counter -le 5 ]; do
echo Making dir $mystring$counter
mkdir $mystring$counter
ls *.slurm > $mystring$counter/testfile.$counter.slurm
counter=$((counter+1))
done
Question 1: what is -le ? Google didn't seem to help, showing me any page with the word 'linux' in it
I literally copied the following form the while manual, and it does not work:
#!/bin/bash
set x 0
while {$x<10} {
puts "x is $x"
incr x
}
#test3.sh: line 6: syntax error near unexpected token `}'
#test3.sh: line 6: `}'
Question 2: What am I doing wrong? Thank you for your attention :)
The [ is a synonym for test builtin function and -le is one of the possible tests, lower or equal. In bash try this for more details:
help [
help test
Re. your second question, it doesn't look like bash syntax at all.
what is -le ?
it means "less than or equal".
What am I doing wrong?
Almost everything. Your script should look like this in bash
x=0
while [ $x -lt 10 ]; do
echo "x is $x"
((x++))
done
I think you really should read the bash manual.

My if statement executes both if's rather than just 1 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I'm learning Bash and I have no idea why my if statement isn't working (working in putty if it makes any difference). I've tried looking online for how an if statement is supposed to be built and followed it making sure I got all the details correct. When I run it both of the if's come out positive and both of them get executed rather than just 1.
echo -n "Enter file name: "
read x
echo "file $x with numbers (Y/N)?"
read y
if [ "$y"="n" ];
then
cat $x
fi
if [ "$y"="y" ];
then
cat -n $x
fi
exit
can anyone help please?
Remember there needs to be a whitespace in your if condition else it becomes an assignment. https://uvesway.wordpress.com/2013/03/11/some-whitespace-pitfalls-in-bash-programming/
"$y"="n" is neither a conditional test nor an assignment. It's just a single string e.g, if $y is 'y', then "$y"="n" is the same as the literal 'y=n' and this literal string is being treated as a single parameter to the test ([) command.
A condition like below is always evaluated to true:
if [ abcd ]; then echo true; fi
while an empty string makes it false:
if [ '' ]; then echo true; else echo false; fi
White spaces aren't optional (everywhere) in bash. Even though the [ abcd thingy looks like a syntax, it's not; [ is a command (test) and abcd is its first positional parameter.

Getting "syntax error near unexpected token `else’" in shell script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I’m using bash shell on Amazon Linux. When I run the below block of code
if [ $rc -eq 0 ] then
passed=`tr ' ' '\n' < $TFILE | grep -c PASSED`
error=`tr ' ' '\n' < $TFILE | grep -c ERROR`
warning=`tr ' ' '\n' < $TFILE | grep -c WARNING`
subject="Automated Results - $passed passed, $error errors, $warning warnings."
else
subject="Failed to run any tests."
fi
I get the error, “syntax error near unexpected token `else’”. What do I need to do to write this if-then-else block correctly?
To quote the syntax definition from help if in bash (which is quite close to the relevant POSIX spec):
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
There can be multiple commands used in the conditional part of an if statement, and a command separator (represented as a semicolon here) is mandatory between the last of them and the list of commands to given should that conditional part return a successful status.
Comparing the code given against that syntax definition, it's missing such a separator:
if [ $rc -eq 0 ]; then
# ^
# \- this semicolon, or a newline, is mandatory before "then"
As it is, then is being passed as an argument to the [ command, not parsed as syntax.
(Since you're tagged bash, consider also using native math syntax: if (( rc == 0 )); then is both more readable and less buggy).

How can I test if a variable contains a string in a bash script without getting the "command not found" error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I keep getting a "command not found" error after checking a variable for the substring .txt.
Here's a simple version of my script.
myscript.sh
#!/bin/sh
if [["$1" == *.txt]]
then
echo $1
fi
Result:
> ./myscript.sh argument.txt
./myscript.sh: line 2: [[argument.txt: command not found]]
The error is because of a space needed around the brackets [[ and ]]:
#!/bin/sh
if [[ "$1" == *.txt ]]
then
echo $1
fi
That is, instead of:
if [["$1" == *.txt]]
use
if [[ "$1" == *.txt ]]

Resources