Help on case to call recursively in UNIX Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on case to call recursively in UNIX Shell Script
# 1  
Old 05-10-2013
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
# 2  
Old 05-10-2013
What have you tried so far?
# 3  
Old 05-10-2013
Help on case to call recursively in UNIX Shell Script

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.

Code:
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
# 4  
Old 05-10-2013
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
Code:
myfunc 1 2 3 4

and, in the function, loop through them. If the users enters 3, call another function with the remaining items
Code:
myfunc 1 2 4

etc.
# 5  
Old 05-11-2013
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.

Code:
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
# 6  
Old 05-22-2013
Please Advise, have tried like this :

Code:
#! /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.
# 7  
Old 05-22-2013
You could try something like:
Code:
#!/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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

3. Shell Programming and Scripting

Case statement in UNIX shell script

have written the below code to check whether the string received from user is a file name or dir using case statement, but its going into default case*). #!/bin/sh #Get a string from user and check whether its a existing filename or not rm str2 rm str3 echo "enter a file \c" read fil... (8 Replies)
Discussion started by: Mohan0509
8 Replies

4. Shell Programming and Scripting

Call sql script from UNIX shell script

I know this question is out there in many forums, but I tried all the combinations in vain. I'm basically trying to call a sql script from a shell script. Below is my sql script (plsql.sql) DELCARE v_empno NUMBER := '&empno'; BEGIN select ename,sal from emp where empno = v_empno;... (3 Replies)
Discussion started by: FName_LName
3 Replies

5. Shell Programming and Scripting

Shell script to copy particular file from directories recursively

I have directory path in which there are several sub directories. In all these sub dir there will be one env.cnf file. I want to copy this env.cnf file from each sub dir's and place them in destination path by creating same filename as sub dir_env.cnf. After copying env.cnf files from source... (4 Replies)
Discussion started by: Optimus81
4 Replies

6. Shell Programming and Scripting

Need to find recursively all shell script in the /xyz directory

Pls. advise how to find or used grep recursively all shell script files. Some files doesnt have a .sh or .ksh extension name. find / -name "*" |xargs grep bin |grep sh ?? TIA (1 Reply)
Discussion started by: budz26
1 Replies

7. Shell Programming and Scripting

Shell Script - find, recursively, all files that are duplicated

Hi. I have a problem that i can't seem to resolve. I need to create a script that list all the files, that are found recursively, with the same name. For example if a file exists in more than one directory with the same name it list all the files that he founds with all the info. Could someone... (5 Replies)
Discussion started by: KitFisto
5 Replies

8. Programming

How to call the C Programme in UNIX shell scripting...?

Hi All, How to call the C programme in UNIX shell scripting? please send me some sample script for the above scenario. Regards, MPS:b: (1 Reply)
Discussion started by: psiva_arul
1 Replies

9. UNIX for Dummies Questions & Answers

Call a UNIX shell with parameters from C

Hello...I hava quite a problem, couldn't find a solution anywhere :(. I have a C program, and from that C program I have to call a shell script. This is not difficult, I can do it using the "system" command from C. But the ugly part is how can I send as parameters some variables? For example...i... (1 Reply)
Discussion started by: dustman
1 Replies

10. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question