Operating System - HP-UX
1834226 Members
2536 Online
110066 Solutions
New Discussion

Re: shell script question

 
Ray Pemberton
Occasional Advisor

shell script question

I am writing a script which reads the contents of a file and if the 2nd field on eah line = hpux003 then it will write to a tape.

When I ask the user for a response with the 2nd read statement, this statement is ignored?

Can someone help please?

#!/usr/bin/ksh
clear
cat /ignite_images/clients.txt | while read a b c
do
if [ $b = "hpux003" ]
then
i=50
while (( i < 100 ))
do
tput cup 6 5 ; echo "Load $a DAT tape in drive, enter [Yy] to Continue [Qq] to Quit : " ; tput cup 6 83
read resp
case $resp in
[Yy]) tput cup 7 5 ; echo "Copying lif_image to tape"
mt -t /dev/rmt/8mn rew
dd if=/ignite_images/net_recoveries/$1/lif_image of=/dev/rmt/8mn obs=2k > /tmp/dd.out 2>&1
dd if=/ignite_images/net_recoveries/$1/$d of=/dev/rmt/8mn obs=10k >> /tmp/dd.out 2>&1
mt -t /dev/rmt/8mn rew
i=200
;;
[Qq]) tput cup 7 5 ; echo "Not creating tape archive"
exit
;;
*) tput cup 6 83 ; echo " "
tput cup 7 0 ; echo " "
tput cup 7 5 ; echo "${blon}****** Invalid Option - Valid options are \"YyNn\" - Please try again.${blof}"
sleep 5
tput cup 7 0 ; echo " "
;;
esac
done

exit
else
:
fi
done
6 REPLIES 6
RAC_1
Honored Contributor

Re: shell script question

sh -vx and execute your command.
There is no substitute to HARDWORK
Ray Pemberton
Occasional Advisor

Re: shell script question

I may not have explained the probem correctly.
The statement "read resp" does not wait for a response, it just drops through, I presume it has something to do with the initial read. How can I get the "read resp" to actually read a response.
I tried replacing the outer loop with
for i in `cat /ignite_images/clients.txt`
but each field on each line becomes a line in its own right?
James R. Ferguson
Acclaimed Contributor

Re: shell script question

Hi Ray:

You are reading from the same descriptor. Duplicate a file descriptor (FD) for reading (.e.g. 3) and read from *that* FD for your second read. Consider:

#!/usr/bin/sh
exec 3<&0
cat /etc/hosts | while read LINE
do
echo "${LINE}"
read -u3 X
echo "You said ${X}"
done

By the way, using 'cat' to feed a 'read' generates a superflous process. It is much more economical to do:

while read LINE
do
...
done < filename

Regards!

...JRF...
Ray Pemberton
Occasional Advisor

Re: shell script question

Thanks James. That works OK.
Matti_Kurkela
Honored Contributor

Re: shell script question

You start your "while" loop with:

cat /ignite_images/clients.txt | while read a b c

Now everything inside this loop has its standard input directed from that pipeline.
Your "read resp" is reading from your /ignite_images/clients.txt file, not from the user.

You can force your "read resp" to read from the user by changing that line to:

read resp < /dev/tty

/dev/tty is a pseudo-device that always refers to "the standard input/output the session would have if no redirection were used".
MK
Alan Meyer_4
Respected Contributor

Re: shell script question

Hey Ray,

You've acknowledged that you've gotten the answer you were needing, why not award points to the people whove spent their time to help you?

Every response to your question is eligible to earn between 1-10 points. No need to worry about running out of points - when a truly awesome reply rolls in that deserves a 10, you will be able to assign it a 10! However, be careful to assign points based on the value that a reply truly provides. Use the following scale as a guideline:

o N/A: The answer was simply a point of clarification to my original question

o 1-3: The answer didn't really help answer my question, but thanks for your assistance!

o 4- 7: The answer helped with a portion of my question, but I still need some additional help!

o 8-10: The answer has solved my problem completely! Now I'm a happy camper!


Although assigning points is not mandatory, it is a key component of a strong, interactive community, and it is STRONGLY ENCOURAGED. Others have taken time to help you, so please take a moment to give them credit for their assistance!
" I may not be certified, but I am certifiable... "