Sponsored Content
Full Discussion: Help with if-else construct
Top Forums Shell Programming and Scripting Help with if-else construct Post 302480180 by Chubler_XL on Tuesday 14th of December 2010 07:32:34 AM
Old 12-14-2010
In ask you want to display question 8 of 8, then read their answer and then show your new message so the updated ask function would be:

Code:
ask()
{
    clear
    echo "Question $((QNUM+1)) of ${#QUESTIONS[@]}"
    show_time
    echo
    echo -e "${QUESTIONS[$QNUM]}\n"
    echo -e "Your answer: ${ANSWER[$QNUM]:- }$BACK\c"
    read new_ans
    new_ans=${new_ans:-${ANSWER[$QNUM]}}
    case $new_ans in
        a|b|c|d) ANSWER[$QNUM]=$new_ans ;;
        *) echo "Illegal answer - please try again" ;
           sleep 1 ;
           return 2 ;;
    esac
    while true
    do
        if [ "$((QNUM+1))" -eq ${#QUESTIONS[@]} ];
        then
            echo "end of questions. enter q to quit"
        else
            echo -e "Type n for next, p for previous, q for quit: \c"
        fi
        show_time
        read nav
        case $nav in
            n|N) [ "$((QNUM+1))" -ne ${#QUESTIONS[@]} ] && return 0 ;;
            p|P) [ "$((QNUM+1))" -ne ${#QUESTIONS[@]} ] && return 1 ;;
            q|Q) return 3 ;;
        esac
    done
}

Down side of this is they don't get to go back and review/change an answer before finishing.

The version below allows them to supply the required number of answers and then asks "All done, type question (1-n) to review your answer or Q to quit:"

Code:
ask()
{
    ANSWERED=$(echo -e ${ANSWER[@]}"\c" | sed 's/ //g' | wc -c)
    if [ $ANSWERED -ge ${#QUESTIONS[@]} ]
    then
        while true
        do
            echo -e "All done, type question (1-${#QUESTIONS[@]}) to review your answer or Q to quit: \c"
            read QNUM
            [ $QNUM = "q" ] && return 3
            let QNUM=QNUM-1
            [ $QNUM -ge 0 -a $QNUM -lt ${#QUESTIONS[@]} ] && break
            echo "Illegal response"
        done
    fi
    clear
    echo "Question $((QNUM+1)) of ${#QUESTIONS[@]}"
    show_time
    echo
    echo -e "${QUESTIONS[$QNUM]}\n"
    echo -e "Your answer: ${ANSWER[$QNUM]:- }$BACK\c"
    read new_ans
    new_ans=${new_ans:-${ANSWER[$QNUM]}}
    case $new_ans in
        a|b|c|d) ANSWER[$QNUM]=$new_ans ;;
        *) echo "Illegal answer - please try again" ;
           sleep 1 ;
           return 2 ;;
    esac
    ANSWERED=$(echo -e ${ANSWER[@]}"\c" | sed 's/ //g' | wc -c)
    [ $ANSWERED -ge ${#QUESTIONS[@]} ] && return 4
    while true
    do
        echo -e "Type n for next, p for previous, q for quit: \c"
        show_time
        read nav
        case $nav in
            n|N) return 0 ;;
            p|P) return 1 ;;
            q|Q) return 3 ;;
        esac
    done
}


Last edited by Chubler_XL; 12-14-2010 at 09:28 AM..
This User Gave Thanks to Chubler_XL For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with looping construct

Hi all I have tried to search for this, but keep getting a MySQL db connect error, so am posing the question here, and taking a risk of incurring the wrath of the mods with my first post... I have the following test script: #!/bin/bash HTTPD=`/bin/ps -axcu | /usr/bin/grep httpd... (6 Replies)
Discussion started by: mikie
6 Replies

2. Shell Programming and Scripting

ksh construct

Hi Guys, could someone tell me what this ksh construct does typeset -r PROG_PWD=${0%/*} does I understand the -r for readonly but I would very much appreciate a definitive account of what this will set $PROG_PWD to. If I run this at the cmd line it it gets set to /usr/bin but I would... (2 Replies)
Discussion started by: ajcannon
2 Replies

3. Shell Programming and Scripting

if-else construct not working

Hi all, Sorry to ask this easy question but I am stuck. In a scenario i am executing one shell script which contains a if - else construct : if ; then echo $line $line >> successful_build.txt else $line >> failed_services.txt fi explaination : if the... (5 Replies)
Discussion started by: bhaskar_m
5 Replies

4. Shell Programming and Scripting

syntax error in the if construct

Hi can anyone tell me why is this code giving error mode=$1 if ] || ] then echo "MODES:" exit 1 fi Thanks (5 Replies)
Discussion started by: Anteus
5 Replies

5. Shell Programming and Scripting

construct a string with X number of spaces

I'd like to create a variable with the value of X number of space( no Perl please), printf seems to work, but , in following example,10 spaces becomes 1 space when assinged to a variable, Why? other solutions are welcome. $printf "=%10s=\n" = = $var=$(printf "=%10s=\n") echo... (4 Replies)
Discussion started by: honglus
4 Replies

6. UNIX for Dummies Questions & Answers

awk construct unfamiliar to me

Please help me out: I've seen this construct awk '{...}1'several times, like in scrutinizer's today's post awk '{for(i=2;i<=NF;i++)if($i==$1)$i=RS $i}1' infilebut I can't find (manuals, man pages, internet FAQs,...) an explanation of what it does resp. stands for. Any hint is appreciated! (5 Replies)
Discussion started by: RudiC
5 Replies

7. Shell Programming and Scripting

Construct path

Hi, I need to construct the below path from the two available directory path, O/P /home/data/test/run/ht/WEB/HTML /home/data/test/run/ht/WEB/JSP /home/data/test/run/ht/WEB/CSS Path:1 ------ /home/data/test/run/ Path:2 ------ /home/data/share/app/01/lang/ht/WEB/HTML... (5 Replies)
Discussion started by: vel4ever
5 Replies

8. Shell Programming and Scripting

For loop; how to construct a array with variables

Hi everybody!! Here is the thing; I have a trouble in this simple situation, I'm trying to write an array with all the arguments of a command. I mean, if I have: ./mycommand.sh aa bb cc dd I need to take an array like this: myarray=(aa bb cc dd) So I use a simple for loop like this: for... (4 Replies)
Discussion started by: andresgom
4 Replies

9. Shell Programming and Scripting

Special IF construct syntax in UNIX

Hi, I don't understand && and || in this context. I thought && is for logical 'AND' and || is for logical 'OR'. && echo "Not empty" || echo "Empty" Please help Thank You (5 Replies)
Discussion started by: TomG
5 Replies

10. Shell Programming and Scripting

How would I construct a (possibly simple) IF statement?

Hi all, I thought this would be simple, but I've been having a lot of trouble trying to write this IF statement, if I may ask for help pls: In BASH, how would I construct the if statement: Should ONLY be true if USEROPTscript=="yes"]] AND $mode=="INSTALL" /or/ $mode=="CHANGE" ]]... (3 Replies)
Discussion started by: jmccoughlin
3 Replies
All times are GMT -4. The time now is 11:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy