how to detect if a string ends with " in bash [duplicate] - bash

This question already has answers here:
Difference between single and double square brackets in Bash
(7 answers)
How can I escape a double quote inside double quotes?
(9 answers)
Closed 6 months ago.
I have a problem with bash, I'm trying to detect strings in a file with bash with the file looking like
cnlog "Hello World!"
When i try the [ $variable = *" ] in bash it doesn't work and throws out an error
basilc.sh: line 13: unexpected EOF while looking for matching `"'
basilc.sh: line 16: syntax error: unexpected end of file
the code of the bash file is
char=""
while IFS='' read -n1 c; do
if [ $char = '"' ] || [ $char = *" ]
then
echo "STRING FOUND"
char=""
pwd
fi
echo "$char"
done < $1
Please help

Related

EOF error in bash script [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 4 years ago.
I seem to be having some problems executing this script? not sure what is causing the problem?
touch file
cat file | while read line;
do
output=$(awk '/path/ {print NR, $0}' "$line");
if [$(#output) -ne 0];
then
echo "File: " "$line";
echo "----------------------------------------------------";
echo "$output"
else
echo "No keyword appeared";
fi
done
Based on shellcheck.net
Line 5:
if [$(#output) -ne 0];
^-- SC1035: You need a space after the [ and before the ].
^-- SC1009: The mentioned syntax error was in this test expression.
^-- SC1073: Couldn't parse this command expansion. Fix to allow more checks.
Line 6:
then
^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.

"[0: command not found" in Bash [duplicate]

This question already has answers here:
How to use double or single brackets, parentheses, curly braces
(9 answers)
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
I am trying to get the array in the while-loop and need to update the value in array too.
Below is my code what I have tried. I get this error [0: command not found
#!/bin/bash
i=0
while [$i -le "{#myarray[#]}" ]
do
echo "Welcome $i times"
i= $(($i+1)))
done
How do I fix this?
Need a space after [ and no space before or after = in the assignment. $(($i+1))) would try to execute the output of the ((...)) expression and I am sure that's not what you want. Also, you are missing a $ before the array name.
With these things corrected, your while loop would be:
#!/bin/bash
i=0
while [ "$i" -le "${#myarray[#]}" ]
do
echo "Welcome $i times"
i=$((i + 1))
done
i=$((i + 1)) can also be written as ((i++))
it is always better to enclose variables in double quotes inside [ ... ]
check your script through shellcheck - you can catch most basic issues there
See also:
Why should there be a space after '[' and before ']' in Bash?
How to use double or single brackets, parentheses, curly braces
Command not found error in Bash variable assignment
Using [ ] vs [[ ]] in a Bash if statement

-bash: [: =: unary operator expected. when no parameter given [duplicate]

This question already has answers here:
How to use double or single brackets, parentheses, curly braces
(9 answers)
Bash script error [: !=: unary operator expected
(2 answers)
Closed 5 years ago.
I have my shell script, myscript.sh below
#!/bin/sh
if [ $1 = "-r" ]; then
echo "I am here"
fi
If I run with . myscript.sh -r, it works well with message I am here.
But if I just run with . myscript.sh, it complaints
-bash: [: =: unary operator expected
What's missing in my script?
You would need to add quotes around $1.
if [ "$1" = "-r" ]; then
echo "I am here"
fi
When $1 is empty you are getting if [ = "-r"] which is a syntax error.
You have missed the quotes:
if [ "$1" = "-r" ]; then

Shell scripting while loop error [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
#!/bin/bash
a=0
while ["$a" -lt 10]
do
a=`expr "$a" + 1`
echo $a
done
This is the error:
a.sh: 3: a.sh: [0: not found
you need to keep spaces around your brackets:
while [ "$a" -lt 10 ]
since bash interpretes strings; it uses spaces to separate words. When you write ["$a"; then bash reads: [0 (after $a is replaced with 0)

bash script if and while conditions [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
I am having trouble with executing the following bash script:
#!/bin/bash
response=" "
while ["$response" != "q"]; do
echo -n "Please enter a response"; read response
done
ALSO
!/bin/bash
response="x"
if ["$response" = "x"]
then
echo "the value is x"
fi
What could the possible errors be?
Your while and if statements are spaced wrong.
while [ "$response" != "q" ]; do etc
You need a space between the bracket and the double quote.

Resources