I have the following shell script. The purpose is to loop thru each line of the target file (whose path is the input parameter to the script) and do work against each line. Now, it seems only work with the very first line in the target file and stops after that line got processed. Is there anything wrong with my script?
#!/bin/bash
# SCRIPT: do.sh
# PURPOSE: loop thru the targets
FILENAME=$1
count=0
echo "proceed with $FILENAME"
while read LINE; do
let count++
echo "$count $LINE"
sh ./do_work.sh $LINE
done < $FILENAME
echo "\ntotal $count targets"
In do_work.sh, I run a couple of ssh commands.
The problem is that
do_work.shrunssshcommands and by defaultsshreads from stdin which is your input file. As a result, you only see the first line processed, because the command consumes the rest of the file and your while loop terminates.This happens not just for
ssh, but for any command that reads stdin, includingmplayer,ffmpeg,HandBrakeCLI,httpie,brew install, and more.To prevent this, pass the
-noption to yoursshcommand to make it read from/dev/nullinstead of stdin. Other commands have similar flags, or you can universally use< /dev/null.