I have a variable like below.
variable = This script is not found
if [[ "$variable" = ~ "not found" ]];
then
echo "Not Found"
else
echo "Its there"
if
while executing im getting below err,
line 4: syntax error in conditional expression
./test.sh: line 4: syntax error near `found"'
./test.sh: line 4: `if [[ "$variable" = ~ "not found" ]]; '
could anyone point me, What im missing here?
LIST="some string with a substring you want to match"
SOURCE="substring"
if echo "$LIST" | grep -q "$SOURCE"; then
echo "matched";
else
echo "no match";
fi
Good Luck ;)
Compare this with your version at the indicated points:
variable="This script is not found" # <--
if [[ "$variable" =~ "not found" ]] # <--
then
echo "Not Found"
else
echo "Its there"
fi # <--
You can't put spaces around = in an assignment, and you need to quote a string literal that has spaces. You don't need a trailing ; if you're going to put then on its own line. And an if-then ends with "fi" not "if".
here is a correct construction of your if statement
if [[ "$variable" =~ "not found" ]]; then
echo "Not Found";
else
echo "Its there";
fi
Input:
line="There\'s a substring to be found!"
if [[ "$line" =~ *"substring"* ]]; then
echo "Not Found";
else
echo "Found!"
fi
Output:
Found!
I have tried below codes which always return same result either in true or false
if [[ "$variable" =~ "not found" ]]; then
echo "Not Found";
else
echo "Its there";
fi
Related
read answer #this will read what the user is typing in and makes a string for it
if [[ -z {answer} ]]
then
echo "Sorry that is not a valid choice, please select from the above list provided" #this will help pick up if the user just enters a blank option
if [[ $1{answer} ]]
then
echo "this has worked" #no clue if this will work going to try this and see if it works
if [[ $2{answer} ]]
then
echo "this is answer 2"
if [[ $3{answer} ]]
then
echo "this is answer 3"
if [[ $4{answer} ]]
then
echo "This is answer 4"
if [[ $Q{answer} ]]
then
echo "this is the quit button"
else
echo "no valid choice has been made"
fi
fi
fi
fi
fi
fi
so i am trying to run this code, i have closing arguments for each if statement, but every time i try and run it it shows "line 51: syntax error: unexpected end of file" the code goes up to line 50, i tried putting in ;; but it ran a syntax error on them, i tried converting with the dos2unix command but nothing, is there something i am missing?
Add the $ to this line, like so
if [[ -z ${answer} ]]
I am using shell to simple String regex match. Here is my shell
#!/bin/sh
MSG="ANK"
PATTERN="([A-Z]{3,5}[-][0-9]{2,5})"
if [ "$MSG" =~ "$PATTERN" ]; then
echo "MATCHED";
else
echo "not";
fi
It is giving error
abc.sh: 6: [: ANK: unexpected operator
How should I fix this?
Making the changes proposed by several contributors in the comments yields:
#!/bin/bash
MSG="ANK"
PATTERN="([A-Z]{3,5}[-][0-9]{2,5})"
if [[ "$MSG" =~ $PATTERN ]]; then
echo "MATCHED";
else
echo "not";
fi
Note the change to bash, the change to [[ and removal of the quotation marks around $PATTERN.
Can someone explain why this simple bash script:
#!/bin/bash
myvar="Hello"
if [[ -z "$myvar" ]]; then
# echo "It's an unfilled string"
else
echo "It's a filled string!"
fi
gives me the error
./testscript: line 7: syntax error near unexpected token `else'
./testscript: line 7: `else'
However, if I remove the comment on the echo line, the script runs fine. Obviously, there is an issue with having commented lines within empty if statements. This this in mind, how do I fix it so I can have an empty if statement with comments?
There are no statements between then and else, so this is a syntax error. If you really want to do nothing in the if branch, then you can use a : (or true) as a placeholder:
#!/bin/bash
myvar="Hello"
if [[ -z "$myvar" ]]; then
# echo "It's an unfilled string"
:
else
echo "It's a filled string!"
fi
Better yet, reverse your logic:
#!/bin/bash
myvar="Hello"
if [[ -n "$myvar" ]]; then
echo "It's a filled string!"
fi
This is the way not to use if else statement.
#!/bin/bash
myvar="Hello"
[[ -n "$myvar" ]] && echo "It's a filled string!"
Also you can use this.
#!/bin/bash
myvar="Hello"
[[ -z "$myvar" ]] || echo "It's a filled string!"
I want an if/then statement in Bash and I can't seem to get it to work. I would like to say "If the line begins with > character, then do this, else do something else".
I have:
while IFS= read -r line
do
if [[$line == ">"*]]
then
echo $line'first'
else
echo $line'second'
fi
done
But it isn't working.
I also tried to escape the ">" by saying:
if [[$line == ^\>*]]
Which didn't work either.
Both ways I am getting this error:
line 27: [[>blah: command not found
Suggestions?
Spaces are needed inside [[ and ]] as follows:
if [[ "$line" == ">"* ]]; then
echo "found"
else
echo "not found"
fi
This attempt attempt uses a regex:
line="> line"
if [[ $line =~ ^\> ]] ; then
echo "found"
else
echo "not found"
fi
This one uses a glob pattern:
line="> line"
if [[ $line == \>* ]] ; then
echo "found"
else
echo "not found"
fi
Spacing is important.
$ [[ ">test" == ">"* ]]; echo $?
0
$ [[ "test" == ">"* ]]; echo $?
1
if grep -q '>' <<<$line; then
..
else
..
fi
using grep is much better :)
I want to validate a file name in bash to make sure that I don't have this '[]' character in it
I have this :
if ! [[ $filename=~ ^[a-zA-Z]+$ ]]; then
echo 'Wrong filename input' >&2
exit 1
fi
but I want explicitly avoid [] and allow other special characters.
any advice?
Thanks.
Use spaces around =~ operator:
[[ ! "$filename" =~ ^[a-zA-Z]+$ ]] && echo "bad filename" || echo "its good"
OR your own script:
if [[ ! "$filename" =~ ^[a-zA-Z]+$ ]]; then
echo 'Wrong filename input' >&2
exit 1
fi
Update:
If you want to explicitly avoid only [ and ] then following check is better:
if [[ "$filename" == *[]\[]* ]]; then
echo 'Wrong filename input' >&2
exit 1
fi