76289

Variable in while loop being read from unexpected source

Question:

I am trying to compare the content of file2 with that of file1 and based on that I need to take some action.

But when I try to take input from user (in variable answer) whether to start or not, the program does not wait for user input and takes value assigned to variable line automatically.

#!/bin/bash while read line; do var=`grep $line file1.txt` if [ -z "$var"] then echo "$line is not running" echo "Do you want to start? (Y/N)" read answer if [ "$answer" = 'Y' ] || [ "$answer" = 'N' ] then if [ "$answer" = 'Y' ] then (some action) else (action) fi else (action) fi fi done < file2

Answer1:

You redirect stdin for the while loop to file2. So inside the loop, stdin is redirected and a read will read from the file, not from the terminal.

With bash, you can easily fix that by using a different file descriptor:

while read -r -u3 line; do echo "$line" read -p "Continue? " yesno if [[ $yesno != [Yy]* ]]; then break; fi done 3<file2

The -u3 command-line flag to read causes it to read from fd 3, while the 3<file2 redirection redirects fd 3 to file (opening file for reading).

Answer2:

Another approach to the excellent answer offered by @rici, this time not requiring bash:

while read -r line <&3; do echo "$line" printf "Continue? " >&2 read yesno case $yesno in [Yy]*) : ;; *) break ;; esac done 3<file2

Using read <&3 reads from FD 3, just as the bash extension read -u 3 would do.

Recommend

  • case-esac; syntax error: newline unexpected (expecting “)”)
  • Getting an external table's location and file name?
  • Spring singleton bean lifecycle
  • Easy Way to Get Averages Based on Names in List
  • Read stdin in chunks in Bash pipe
  • System call time out?
  • How do we generate stack trace in TOMCAT?
  • How can I count unique terms in a plaintext file case-insensitively?
  • Unicorn and Rails eat up 2x MySQL connections
  • How to save dynamically created textboxes and their values
  • How to enable large page memory for the JVM?
  • Interpreting STRACE output - pipes and forks
  • pip in virtualenv gets ConnectTimeoutError
  • nodemcu custom firmware build problems
  • Android Studio Can't Find tools.jar
  • Convert SQLite database to XML
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Eclipse CDT error: Unable to compile
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Installed module is empty
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • d3 v4 drag and drop with TypeScript
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • ActionScript 2 vs ActionScript 3 performance
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How can I estimate amount of memory left with calling System.gc()?
  • Apache 2.4 - remove | delete | uninstall
  • How to delete a row from a dynamic generate table using jquery?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • using HTMLImports.whenReady not working in chrome
  • LevelDB C iterator
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Can't mass-assign protected attributes when import data from csv file
  • Reading document lines to the user (python)