0

This works:

echo "Type the desired option
 Option 1: See textfile
 Option 2: Edit textfile 
 Option 3: Add line to the textfile"
read option
if [$option == 1]
then
 cat database.txt
else
 vim database.txt
fi

but this doesn't

file="database.txt"

echo "Type the desired option
 Option 1: See textfile
 Option 2: Edit textfile 
 Option 3: Add line to the textfile  "
read option
if [$option == 1]
then
 cat $file
elif [$option == 2]
then
 vim $file
elif [$option == 3]
then
 echo "Type your line:"
 read newline
 echo $newline >> $file
else
 echo "None of the options were chosen"
fi

and this is the result whenever I choose one of the options:

stock1.sh: line 8: [1: command not found stock1.sh: line 11: [1: command not found stock1.sh: line 14: [1: command not found

I've have already tried removing the file variable and leaving it as database.txt, to no success

fathawk
  • 13
  • 3
  • 1
    Put a valid shebang and paste your script at https://shellcheck.net for validation/recommendation. – Jetchisel Nov 06 '22 at 12:45
  • 1
    Sigh. 10,000 duplicates. `[` is a command. `[1` is not a command, as shown in the error message. If `$option` expands to `1`, then `[$option` expands to `[1`, and the shell is trying to find that command. `if cmd; then ...`. In this context, the command is supposed to be `[`. Someone long ago thought it would be a good idea to introduce the command `[` so that people would be fooled into thinking it is part of the grammar. it is not. – William Pursell Nov 06 '22 at 12:48
  • Add spaces after the "[" and before the "]", and be sure to follow Jetchisel suggestion to check with shellcheck. – Lars Fischer Nov 06 '22 at 12:59

0 Answers0