Help on case to call recursively in UNIX Shell Script

Hi,

I am New to Unix Shell Scripting basically, i need some help in achieving a case statement in Shell script to call recursively That is if case having like 1 2 3 4 options , if user inputs 1 and gets executed case should ask for options again but user should not input the same input value 1, whether this type of validation can be performed. I hope i have explained. Please excuse if this kind of post has already available and please let me know the post thread.

Thanks,
Regards,
karthikram

What have you tried so far?

Hi Vidyadhar,

Thanks for the interest shown to my query, have searched Forum and got this below script, previously i was trying to implement a case in a function try to call it again but function return once input is accomplished, I am currently using Linux ksh and will try this and let you know if there is any issue.

while true
do
    echo "Would you like to:"
    echo "1) See your name"
    echo "2) See your current directory"
    echo "3) See your home directory"
    echo "4) Quit"
    echo -n ">> "
    read case

    case "$case" in
        1) echo "username";;
        2) echo "$PWD";;
        3) echo "$HOME";;
        4) break;;
    esac
done

echo "Quit"

Thanks,
Regards,
karthikram

It is maybe easier to have 4 state variables, and keep it iteratively,
querying the state variable for each item.
With a recursive function, you have to pass the outstanding items as arguments,
initially

myfunc 1 2 3 4

and, in the function, loop through them. If the users enters 3, call another function with the remaining items

myfunc 1 2 4

etc.

Hi Vidyadhar/Sir, Thank very much ,The case inside the while loop is working fine, Please excuse if possible could you please guide me how to script for the sample code using Function which have posted, i need the script like need to ask for option and user shouldn't enter the same input which they have given earlier during execution.

Would you like to:
1) See your name
2) See your current directory
3) See your home directory
4) Quit
>> 1

if user input 1 , once it does its operation, if user gives the same the input 1 operation should not get executed. I am unable to figure out using function, It might be simple but Please advise.

Thanks,
Regards,
karthikram

Please Advise, have tried like this :

#! /usr/bin/ksh
set -a arr
set -a sort_case
while true
do
    echo "SELECT THE OPTION:"
    echo "1) CASE1"
    echo "2) CASE2"
    echo "3) CASE3"
    echo "4) Quit"
    echo -n ">> "
read  case
typeset -i case
arr[${i}]=$case
echo -e "Array All - ${arr
[*]}\n"
case "$case" in
        1) echo "CASE_1";;
        2) echo "CASE_2";;
        3) echo "CASE_3";;
        *) break;;
    esac
i=`expr $i + 1`
done
echo "Quit"

how to get the unique values from a dynamic array variable like this and Please advise how to make the script not execute the user input if user inputs the same input which have already executed.

You could try something like:

#!/bin/ksh
# Initialize variables
d1=0
d2=0
d3=0
err=0
while [ 1 ]
do      if [ $d3 = 0 ]
        then    list="3, 4"
        else    list="4"
        fi
        if [ $d2 = 0 ]
        then    list="2, $list"
        fi
        if [ $d1 = 0 ]
        then    list="1, $list"
        fi
        printf "Choose an unused selection (%s): " "$list"
        read choice
        case "$choice" in
        (1)     if [ $d1 = 1 ]
                then    printf "Choice 1 is invalid.\n" >&2
                        err=$((err + 1))
                        continue
                fi
                d1=1
                echo 'Choice 1 has been processed.';;
        (2)     if [ $d2 = 1 ]
                then    printf "Choice 2 is invalid.\n" >&2
                        err=$((err + 1))
                        continue
                fi
                d2=1
                echo 'Choice 2 has been processed.';;
        (3)     if [ $d3 = 1 ]
                then    printf "Choice 3 is invalid.\n" >&2
                        err=$((err + 1))
                        continue
                fi
                d3=1
                echo 'Choice 3 has been processed.';;
        (4)     echo 'Choice 4 has been processed.'
                break;;
        esac
done
printf "%d error(s) reported.\n" $err
if [ $d1 -eq 0 ]
then    echo '#1 was not selected'
fi
if [ $d2 -eq 0 ]
then    echo '#2 was not selected'
fi
if [ $d3 -eq 0 ]
then    echo '#3 was not selected'
fi

Or use something similar to what you have been using when creating the menu, but only echo the items in the menu that haven't been chosen using something like the if statements close to the start of this script.

1 Like

Hi Don Cragun,

Thank you very much ... this is works perfectly as expected, I didnt think in this logic and once again Thanks for spending your valuable time on my request to come with this solution.

Thanks,
Regards,
Karthikram