Register
It is currently Sun Sep 21, 2014 12:03 am

need help...about shell scripting..


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Mon Apr 20, 2009 4:18 am   

Joined: Mon Apr 20, 2009 4:03 am
Posts: 1
hi friends..
i have written one script..actually what the script do is that...when i ran the script it will display questions one by one with some options..when user has entered any one choice form among available choices..it will display next questions...and finally it will display how many you have answered correctly and wrongly..
but my code is not taking user input..here is the code

Code:
while read  line
do
echo $line | cut -d ':' -f1
sleep 2
ans=`echo $line | cut -d ':' -f2`
read -p  "Enter Your Answer:" USERANS
correctans=0
wrongans=0
if [ "$ans" = "$USERANS" ]
then
correctans=$(( $correctans + 1 ))
fi
done < questions.txt
echo you answered $correctans correctly

here questions.txt
    capital of Hungery? a)budapest b)paris c)pitsburgh d)i dont no :a

    The longest river in the world? a)ganga b)nile c)bramhaputra d)dont no :b

    Niagara Falls was discovered by? a)jhonny b)Louis Hennepi c)kempson d)adrew heko :b

when i ran the script i am getting below output with 3 sec delay(i have given sleep 3)
    srinivas@opensolaris:~$ ./tryingscript.sh
    capital of Hungery? a)budapest b)paris c)pitsburgh d)i dont no
    The longest river in the world? a)ganga b)nile c)bramhaputra d)dont no
    Niagara Falls was discovered by? a)jhonny b)Louis Hennepi c)kempson d)adrew heko
    you answered 0 correctly
but the problem is that it is not asking for user input..can somebody help me


Top
 Profile  
 PostPosted: Mon Apr 20, 2009 12:29 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
Often times I've noticed on a loop... the read method is kind of... skipped. I'm not sure of the reason for this, or if it's just user error. But I have a work around that I tend to use. I load things into an array. Now I'm sure it has something to do with the text being 'loaded' using the for loop that makes read ignore what it's supposed to do, but this method below works.

[quote=questions.txt]
capital of Hungery? a)budapest b)paris c)pitsburgh d)i dont no :a
The longest river in the world? a)ganga b)nile c)bramhaputra d)dont no :b
Niagara Falls was discovered by? a)jhonny b)Louis Hennepi c)kempson d)adrew heko :b
[/quote]
Code:
#!/bin/bash
correctans=0
wrongans=0
qarray=-1
declare -a questions
until ! read line
do
        qarray=$((qarray + 1))
        questions[$qarray]=`echo $line`
#       qarray=$((qarray + 1))
done < questions.txt

for i in `seq 0 $qarray`
do
        echo $i
        ans=`echo ${questions[$i]} | cut -d ':' -f2`
        echo ${questions[$i]} | cut -d ':' -f1
        read -p "Enter Your Answer:" USERANS
#       echo $USERANS
        sleep 2
        if [ "$ans" == "$USERANS" ]
        then
                correctans=$(( $correctans + 1 ))
        fi
#       sleep 2
done
echo you answered $correctans correctly


hope that helps


Top
 Profile  
 PostPosted: Mon Apr 20, 2009 4:29 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
Hi,

AFAIK, loops open another subshell thus you need to redirect standard input to read from it.
Code:
#!/bin/bash

myTTY=$(tty)

correctans=0
# wrongans=0 #is it usefull?

while read  line
  do
     ans="${line##*:}"
     echo "${line%%:*}"
#     sleep 2 #is it usefull?
     read -p  "Enter Your Answer:" USERANS <$myTTY
     if [ "$ans" = "$USERANS" ]
       then
          ((correctans ++))
     fi
done <questions.txt

echo "you answered $correctans correctly"
You could make use of select to have cleaner output
...
?


Top
 Profile  
 PostPosted: Mon Apr 20, 2009 7:41 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
...
Code:
#!/bin/bash

Questions="'capital of Hungery?' 'budapest' 'paris' 'pitsburgh' 'I dont know' 'budapest'
'The longest river in the world?' 'ganga' 'nile' 'bramhaputra' 'I dont know' 'nile'
'Niagara Falls was discovered by?' 'jhonny' 'Louis Hennepi' 'kempson' 'adrew heko' 'Louis Hennepi'"

myTTY=$(tty)
correctans=0

while read line
  do
     clear
     eval array=( "$line" )
     echo "${array[0]}"
     PS3="Enter your answer : "
     select QnA in "${array[@]:1:4}"
       do
          case $REPLY in
             1) [ "${array[1]}" = "${array[5]}" ] && ((correctans ++)); break ;;
             2) [ "${array[2]}" = "${array[5]}" ] && ((correctans ++)); break ;;
             3) [ "${array[3]}" = "${array[5]}" ] && ((correctans ++)); break ;;
             4) [ "${array[4]}" = "${array[5]}" ] && ((correctans ++)); break ;;
          esac
     done < "$myTTY"
done <<< "$Questions"
clear
printf '\n%s\n\n' "you answered $correctans correctly"
read -p "       Press \"Enter\" to quit"
clear


Top
 Profile  
 PostPosted: Tue Aug 18, 2009 2:29 pm   

Joined: Tue Aug 18, 2009 1:59 pm
Posts: 1
Watael wrote:
AFAIK, loops open another subshell thus you need to redirect standard input to read from it.
Code:
#!/bin/bash
#...
myTTY=$(tty)
#...
     read -p  "Enter Your Answer:" USERANS <$myTTY
#....


Thanks, I've been looking for this too long... :-/


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP