0

I'm facing a weird error,

I have a file which has some inputs and based on these inputs I ask user confirmation and proceed with the actions ( as shown below ),

#!/bin/bash

func() {
cat testing | while read line
do
  if [ $line = "hi" ]
  then
    while true
    do
      read -p "[y/n] : " c
      case $c in
        [nN] ) return 1;;
        [yY] ) return 0;;
        * ) echo "Enter y/Y or n/N";;
      esac
    done
  fi
  echo "HELLO !!"
done
}

func

Because of the while read line in the function, the actual read -p "[y/n] : " is picking the input from the above testing file and not from STDIN.

The program works fine if remove the cat testing | while read line loop (which is obviously not what I want).

Did anyone face this issue before or Can someone help me solve this ?

Thanks in advance !

ArigatoManga
  • 594
  • 5
  • 23
  • 2
    Please check your code with www.shellcheck.net, there are several issues with it and the website provides useful information. – Patrick Trentin Feb 03 '17 at 16:03
  • 1
    Note the comment in the accepted answer of the linked duplicate, which points to a superior answer. – chepner Feb 03 '17 at 16:11
  • As an aside, `if [ $line = "hi" ]` is quite buggy. It's the *expansion*, not the constant string, that needs to be quoted to ensure consistent behavior; thus: `if [ "$line" = hi ]` – Charles Duffy Feb 03 '17 at 16:18
  • @PatrickTrentin I checked the code..all the changes are minor ones..mostly of the "$line" which is not a problem.. – ArigatoManga Feb 03 '17 at 16:19
  • 1
    @ArigatoManga, "not a problem" only if you know what your inputs are. If your input file has a `*`, for instance, then you'll get a list of files in your current directory passed as individual arguments to `test`. – Charles Duffy Feb 03 '17 at 16:19
  • 1
    @ArigatoManga, ...similarly, if your line of input is `hello world`, then the effective test command will be exactly equivalent to `[ "hello" "world" = "hi" ]`, which isn't valid syntax (only exactly one argument is allowed on the left-hand side of `=`). – Charles Duffy Feb 03 '17 at 16:20
  • @chepner those answer did not work for this..all of them failed – ArigatoManga Feb 03 '17 at 16:20
  • @ArigatoManga, Chepner's answer here, and the second answer on the linked question, are entirely correct. If you're having trouble using them, you'd need to show exactly how they fail to allow that to be addressed. – Charles Duffy Feb 03 '17 at 16:21
  • @CharlesDuffy all the inputs are numbers, so no wild characters, but thanks for pointing out.. – ArigatoManga Feb 03 '17 at 16:21
  • @CharlesDuffy I tried all the answers specified in the duplicate question but the first answer will not work (as specified in comments) the second one is giving the error, ` syntax error near unexpected token }' done; } 3<&0' and the rest i cant use because the lines in the input file have spaces so the best way is to use while loop – ArigatoManga Feb 03 '17 at 16:22
  • 1
    Please show the exact code you're using so we can reproduce that syntax error. – Charles Duffy Feb 03 '17 at 16:23
  • See https://gist.github.com/charles-dyfis-net/e798f23410379ee5a19e442ec12a51d5 for an example incorporating Chepner's answer with correct syntax. – Charles Duffy Feb 03 '17 at 16:27

1 Answers1

2

Use a different file descriptor (and not a pipe) for testing.

while IFS= read -r line <&3; do
    ...
done 3< testing
chepner
  • 497,756
  • 71
  • 530
  • 681
  • @CharlesDuffy I tried all the answers specified in the duplicate question but the first answer will not work (as specified in comments) the second one is giving the error, ` syntax error near unexpected token `}' done; } 3<&0' ` and the rest i cant use because the lines in the input file have spaces so the best way is to use while loop – ArigatoManga Feb 03 '17 at 16:16
  • below is the code that i tried (for ease i removed everything else in the file), `#!/bin/bash while IFS= read DC_ip <3; do read -p "y/n" c echo c done <3 testing` It is giving me this error `syntax error near unexpected token 'testing' done <3 clust_config.ini` – ArigatoManga Feb 03 '17 at 16:26
  • 1
    You are missing the `&` from the redirection on the `read` command, and using `<3` instead of `3<` on the while loop (that was my error in the original, since fixed). – chepner Feb 03 '17 at 16:27
  • thanks it is working.. – ArigatoManga Feb 03 '17 at 16:36