Operating System - HP-UX
1834628 Members
3035 Online
110069 Solutions
New Discussion

Re: shell script - read doesn't work

 
SOLVED
Go to solution
andi_1
Frequent Advisor

shell script - read doesn't work

Hi guys,

For some reason, this script doesn't stop at read to read user answer (Y/N) and for somehow N is always assumed!

sed "s/,/ /g" $1|while read line
do

LVSTRING="${LVCREATE} -i ${LVSTRIP}"

echo "\n Striping will be used"
echo " Number of spindles to be used is "
echo "\nWould you like to change the number of spindles (y/n)[n]: \c"
read ans
ans=$(echo $ans |tr "[a-z]" "[A-Z]")
if [ "$ans" = "Y" ]; then
echo "YES"
else
echo "NO"
fi
fi
done

Thank you.
11 REPLIES 11
Bill McNAMARA_1
Honored Contributor

Re: shell script - read doesn't work

insert the line

#!/usr/bin/ksh

at the top of the script..

Later,
Bill
It works for me (tm)
Bill McNAMARA_1
Honored Contributor

Re: shell script - read doesn't work

kibo:root> /tmp/scr

Striping will be used
Number of spindles to be used is

Would you like to change the number of spindles (y/n)[n]: n
NO

Works for me:

kibo:root> cat /tmp/scr
echo "\n Striping will be used"
echo " Number of spindles to be used is "
echo "\nWould you like to change the number of spindles (y/n)[n]: \c"
read ans
ans=$(echo $ans |tr "[a-z]" "[A-Z]")
if [ "$ans" = "Y" ]; then
echo "YES"
else
echo "NO"
fi

Later,
Bill
It works for me (tm)
andi_1
Frequent Advisor

Re: shell script - read doesn't work

I do have #!/bin/ksh
andi_1
Frequent Advisor

Re: shell script - read doesn't work

Bill,

If I remove while loop it works, but I need to have while loops since its in the while loop I read file and determine striping

I hope read can work in the while loop
Bill McNAMARA_1
Honored Contributor

Re: shell script - read doesn't work

Watch out for your nested if
and multiple use of the same variable.

other than that, term emulation or env should be looked at.

Dis you try to add a set -x in the script.

Later,
Bill

It works for me (tm)
Santosh Nair_1
Honored Contributor
Solution

Re: shell script - read doesn't work

It looks like the line "read ans" is reading from your input file, i.e. from $1. Try redirecting stdin into the read, i.e:

the line:

read ans

become:

read ans <&1

Hope this helps.

-Santosh

Life is what's happening while you're busy making other plans
Rodney Hills
Honored Contributor

Re: shell script - read doesn't work

It looks like your sed is piping its output to the while loop, so the "read ans" is picking up its data from whatever you pass as a file to sed.

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: shell script - read doesn't work

If you use

read -u3 ans

Then it will do it's input from STDERR and do a real input from the terminal, assuming you haven't redirected STDERR.

-- Rod Hills
There be dragons...
Bill McNAMARA_1
Honored Contributor

Re: shell script - read doesn't work


Would you like to change the number of spindles (y/n)[n]: n
NO
hello1111

Striping will be used
Number of spindles to be used is

Would you like to change the number of spindles (y/n)[n]: y
YES
hello111

Striping will be used
Number of spindles to be used is

Would you like to change the number of spindles (y/n)[n]: y
YES
hello11





where the script is:
for i in $(cat /tmp/file1)
do

echo "\n Striping will be used"
echo " Number of spindles to be used is "
echo "\nWould you like to change the number of spindles (y/n)[n]: \c"
read ans
ans=$(echo $ans |tr "[a-z]" "[A-Z]")
if [ "$ans" = "Y" ]; then
echo "YES"
else
echo "NO"
fi

echo $i

done


and the file1 is:

hello111111
hello11111
hello1111
hello111
hello11
~
It works for me (tm)
Rodney Hills
Honored Contributor

Re: shell script - read doesn't work

oops, i meant

read -u2 ans

-- Rod Hills
There be dragons...
Ian Dennison_1
Honored Contributor

Re: shell script - read doesn't work

Try sticking the portion of code inside the loop in a function of its own, then calling the function from inside the loop. Be sure to export any variables that are passed between.

function prompt_user
{
print "Are you really really sure?"
read ans

export ans2=$ans
}

sed .... |while read line
do

prompt_user

/* Actions here */
done

Share and Enjoy! Ian
Building a dumber user