Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a script that asks some questions to the user and repeats the question until the user has answered something appropiate. This is done with an infinite loop that is break'd when an acceptable answer is read:

while true
do
    read -p "Which helper do you prefer (cache, store)? " HELPER
    if [ "$HELPER" = "store" -o "$HELPER" = "cache" ]
    then
        break
    else
        error "Invalid option. Choose again"
    fi
done

This is working fine when called independently. The problem is when I execute this script inside a | while read ... loop:

# find scripts that should be run as non-root user, and run them all sequentially
grep -l '^\s*require_non_root' [0-9]* | while read execScript
do
  echo "=== EXECUTING $execScript ==="
  "./$execScript"
done

The ouptut of the grep command alone is what should be in my case:

15-gitcredentials
20-workspace
40-download_latest_dev_vapp
99-change_username_and_password

Problem:

the 15-gitcredentials script (the first snip I posted is part of this script) is reading the same STDIN that the | while read execScript part is expected to read, i.e. the output of the grep command. How could I made the 15-gitcredentials script to read not from STDIN but from other descriptor?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You are approaching the problem with the wrong solution.

for p in $(grep -l regex files); do
    echo -n "Executing $p ... "
    ./$p
    echo "[DONE]"
done
share|improve this answer
add comment

You can use input redirection. For example:

$ cat file
hello
world
$ exec 3< file
$ cat <&3
hello
world
$
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.