-1

I have a script that is misbehaving and I am at a loss as to why.

#!/bin/bash
while read -r;do
read -rsn1 inp
if ["$inp" = "a"]; then
echo "$REPLY"
fi
done

This returns:

./some.sh: line 4: [a: command not found

The goal of this script is to return a line from the read in the beginning of the while loop whenever any key is pressed once. Obviously it does not currently do that. I know understand the errors I made in my if statement and am seeking help with accepting a single user keypress and then echoing a line from "$REPLY". Any help is appreciated.

torchhound
  • 510
  • 6
  • 15

1 Answers1

0

The syntax of if in bash is:

if command line
then
  commands
fi

Because you didn't put in space inside the brackets, in case your $inp is "a", the command if executes is [a, which is not a known builtin, nor an executable file you have on your system. When you write if [ "$inp" = "a" ], the command line executed is [ a = a; the command [, which is same as test, returns a 0 (true, non-error) if the test in its arguments evaluates to true. See man [; and the bash internal [ behaves the same as /bin/[.

That covers the error itself; but the logic is still wrong to reach your stated goal (which could use some clarification if you want that answered as well: how do you differentiate a key meant to progress from the line you want to display? if I understood even that correctly.)

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • How would I check to see if `inp` was filled at all? Could I do this instead: `read -psn1 "$REPLY"`? – torchhound Apr 30 '16 at 04:19
  • `-p` needs an argument, so you'll have a prompt of `sn1` pop up to user... not what you want, I believe. `inp` will definitely be filled because it will wait for a character (unless EOF happens). Again, you need to clarify what you want if you want more than "why does my `if` fail". – Amadan Apr 30 '16 at 04:23
  • The first part of my script reads from `stdin` and returns it line by line already works fine. Now I want a line to appear every time a key is pressed. This led me to put in a second `read` to catch a single user keypress and then `echo` a line from `"$REPLY"` – torchhound Apr 30 '16 at 04:26
  • But where are you reading your lines from? STDIN? What do you mean "a line from `"$REPLY"`? What is the content of `"$REPLY"`? Currently your two reads are reading from the same source, so your `$inp` will be the first character of the next line read by the outer `read`. – Amadan Apr 30 '16 at 04:28
  • I was doing this: `cat a_file | ./this_script.sh`. This worked fine when my script was `#!/bin/bash while read -r;do echo "$REPLY" done` – torchhound Apr 30 '16 at 04:31
  • You're still reading in both places from the same source, in this case the output of `cat a_file`. There is no keypress there. Either you need to modify your script so it accepts file name (so it can decide what read reads from what), or you want to input directly from keyboard in your inner read; the two approaches are illustrated in Gordon's and dank's answer to [this question](http://stackoverflow.com/questions/6883363/read-input-in-bash-inside-a-while-loop), respectively. – Amadan Apr 30 '16 at 08:33
  • This looks very promising, thank you for your patient assistance. – torchhound Apr 30 '16 at 20:57