0

I currently have a simple bash script that work fine when I use user input, however I would like to use a file as the input instead. This is the code I have right now

#!/bin/bash
go=0
while [ $go ]; do
  echo -e "Enter one of the following actions or press CTRL-D to exit.\n\
  C - create a customer file\n\
  P - accept customer file\n\
  F - find customer by apartment number\n\"
  if ! read option ; then
     break
  fi
  case "$option" in
     C) . ./create.bash
        continue
        ;;
     P) . ./payment.bash
        continue
        ;;
     F) . ./find.bash
        continue
        ;;
     *) echo -e "Error: invalid action value\n"
   esac
done

This is a small file I have created that I want to be used as the input. The file will be extended later on with more cases following this one. input.txt

C
bboard@gmail.com
Bill Board
APT-09
120
2017-09-08

Where C is read for the case C) and the following text is used by create.bash as if it was normal user input. I'm not sure how to go about this. I've tried ./menu < input.txt or done < input.txt so far but those aren't working properly.

below_avg_st
  • 187
  • 15
  • 2
    Your quotes are wrong here. In the code as posted, `read` is never run, because it's part of the arguments passed to `echo`. – Charles Duffy Sep 15 '17 at 19:30
  • 1
    (and by the way -- in general, `echo -e` should be avoided; honoring it in a manner other than printing the string `-e` on output is one of the few places where bash *breaks* the POSIX specification rather than extending it. Consider `cat < – Charles Duffy Sep 15 '17 at 19:32
  • (See the POSIX spec for `echo` at http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html; note in particular the APPLICATION USAGE and RATIONALE sections; also, note that bash complies with this specification when appropriately configured -- ie. when both posix and xpg_echo flags are set, which *can* be made to be default at compile time). – Charles Duffy Sep 15 '17 at 19:33
  • Back towards topic -- can you build a [mcve] for your problem? Part of "verifiable" is that (1) it should produce the problem if directly copied-and-pasted from the question without changes -- meaning no dependency on `create.bash` or `payment.bash` or the like -- and "minimal" means that anything not necessary to demonstrate your bug should likewise be stripped. – Charles Duffy Sep 15 '17 at 19:34
  • 2
    (Also consider making a habit of running code through http://shellcheck.net/ before asking questions here). – Charles Duffy Sep 15 '17 at 19:46

0 Answers0