0

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

wjandrea
  • 28,235
  • 9
  • 60
  • 81
decipher
  • 1
  • 3
  • 1
    You're using the variable `c` in the `read` but `char` in the body of `while` loop. Aside from this, there are many problems with this code. Your method is debatable too: Reading a file character by character is rarely useful and tremendously slow in `bash`. – M. Nejat Aydin Aug 26 '22 at 18:21
  • `[[ $char == \" || $char == *\" ]]`. Also I agree. Your method is questionable. You might want to use a better parser, or a real one, and that isn't bash. – konsolebox Aug 26 '22 at 18:21
  • `[[ $variable = *'"' ]]` – Charles Duffy Aug 26 '22 at 18:43
  • Note that `*` is only a wildcard for string comparison purposes (as opposed to filesystem globbing purposes) in `[[`, not `[`. – Charles Duffy Aug 26 '22 at 18:48
  • What do you mean by "string"? Do you mean an individual character (like you're trying to do)? In that case, why do you say "ends with" instead of "is"? They're identical for a one-character string. Or do you mean the line of the file? In that case, why aren't you using `grep`? Or do you mean the syntactic string `"Hello World!"`? In that case, what syntax is the file written in? You can [edit] to clarify. For more tips, see [ask]. – wjandrea Aug 26 '22 at 18:51
  • I meant as a Syntactical String – decipher Aug 26 '22 at 18:58
  • As wjandrea said, if you want to talk about things in the context of how any specific language would parse a string as syntax, your question needs to discuss that language. But bash is an exceptionally poorly-chosen language to implement a parser in, particularly when you're on UNIXy platforms that have offered tokenization and parser-generation tools (`lex`, `bison`, `yacc`, etc etc) for decades. But if all you care about is looking for `"` characters, parsing syntax should be unnecessary overkill. – Charles Duffy Aug 26 '22 at 19:34
  • Look, I know, Im thirteen, its quite hard I'm trying my best – decipher Aug 27 '22 at 18:21

0 Answers0