attempting to break within function


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers attempting to break within function
# 1  
Old 03-22-2005
attempting to break within function

Attempting to break from a case/esac paragraph while inside of a function. When executing the code below, entering the letter 'a', will prove that the directory exists, but the break command works for the if/fi, but not for the case/esac. So, in my example below, if an A is entered, the function testf validates the directory A exists, but then it doesn't. Maybe there is a far simpler way to accomplish what I want to do, limit to a set # of predefined entries, then validate those entries are available.

testf ()
{
dir=$reply
if [ -x /main/$dir ]; then
echo dir $dir exists
break /* want this command to break out of case/esac */
else
echo no dir $dir exists, try again
fi
}

while (true)
do
echo " a b "
echo " Enter one of the directories from above: \c"
read reply
typeset -u reply
case $reply in
A) testf ;;
B) testf ;;
*) echo $reply is not valid dir to choose - try again
esac
done


Appreciate the assistance!
# 2  
Old 03-22-2005
First. Please dont open a new thread when you have already opened one concerning this topic.

Code:
testf () {
dir=$reply

if [ -x /main/$dir ]
 then
    echo dir $dir exists
    #useless use of the command break here #
 else
    echo "no dir $dir exists, try again"
    bflag=n
fi
}

typeset -u reply
while (true)
do
echo " a b "
echo " Enter one of the directories from above: \c"
read reply

case $reply in
A) testf 
    break
    ;;
B) testf
    break
    ;;
*) echo $reply is not valid dir to choose - try again
esac
done


Last edited by google; 03-22-2005 at 02:29 PM..
# 3  
Old 03-22-2005
that logic forces a break regardless if dir exists or not

First, my apology for starting a new thread. What I am attempting is if I choose a, and it exists, then I'm out of the case/esac loop. If I choose, b, which does not exist, then I want to stay in the case/esac loop and not break until I select a good entry. Want to basically stay in this loop until I have selected a valid directory.
# 4  
Old 03-22-2005
You can build a valid list programmatically of direcoties to display rather than have the user input a directory. Alternatively, you can build the list of directories to display and allow the user to enter a directory. If you programmatically build the list then all you have to worry about is validating input from your user. Can you elaborate more on what it is you are trying to build?

Code:
testf () {
dir=$reply

if [ -x /main/$dir ]
 then
    echo dir $dir exists
    #useless use of the command break here #
    FLAG=1
 else
    echo "no dir $dir exists, try again"
    bflag=n
    FLAG=0
fi
}
FLAG=0
typeset -u reply
while [ $FLAG -eq 0 ]
do
echo " a b "
echo " Enter one of the directories from above: \c"
read reply

case $reply in
A) testf 
    break
    ;;
B) testf
    break
    ;;
*) echo $reply is not valid dir to choose - try again
esac
done


Last edited by google; 03-22-2005 at 02:43 PM..
# 5  
Old 03-22-2005
your logic makes sense

Okay, the logic you show makes sense and should work for me.

I have a root directory called root. Within it there are 10 or so subdirectories each of varying size (in terms of name). Of those 10 directories there are around 3 or so that I want the user to access. There are around 3 more that I will want the user to access if they are available. So, basically I want to list all 6 directories, the user selects the one they want and the program says, yes it exists, or "no, at this time it doesn't exist select another". So, I know all six directories that I will have a few months from now, but I want to limit their access to those that exist.

If I could programatically build the list that would be great, though I'm uncertain on how to do that. If I could filter the list of files/directories that exist under main down to only those that are uppercase, then that might work.
# 6  
Old 03-22-2005
I can do this that will produce a list of directories from my main directory

list=`ls ?? ???|grep -E "P|T"`
echo $list
TRN UT:

I don't know why it shows the colon following the UT directory.

Then, in my logic I guess I could just bypass validation and it would work as long as the user doesn't enter some other bogus directory.

Is this what you were thinking?

Last edited by tumblez; 03-22-2005 at 03:20 PM.. Reason: typo
# 7  
Old 03-22-2005
I was actually thinking of building a list of directories using the find command. But this may not be the best idea. If you have at most 6 directories that could be accessed, why not hard code them into the script, but only display those that are available. You could do this by loading an array of only those directories (of the six) that are currently available.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Error attempting to run alsamixer

I am working on an embbeded system. On the system in /usr/bin there are a host of programs one of which is called alsamixer. I want to use this utility but whenever I try to run any alsamixer commands I get and error message: Error opening terminal: vt100 Anyone ever seen this error... (10 Replies)
Discussion started by: Circuits
10 Replies

2. Homework & Coursework Questions

C++ Attempting to modify this function to read from a (;) semi-colon-separated file

After some thought. I am uncomfortable issuing my professors name where, there may be unintended side effects from any negative responses/feedback. Willing to re post if I can omit school / professor publicly, but can message moderator for validation? I am here for knowledge and understanding,... (1 Reply)
Discussion started by: briandanielz
1 Replies

3. SuSE

Attempting to use AutoYaST with SLES11SP2

We currently have a setup with SLES11SP1 where we have an AutoYaST ISO set up for automated network installs. I'm attempting to port this to SP2 to make new installs current, but I'm running into a few problems. The process seems the same, and after running mkisofs to build a new SP2 ISO the... (0 Replies)
Discussion started by: Magus Zeal
0 Replies

4. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

5. Debian

attempting to boot from cd rom

i just installed Debian Lenny on HP DL380. After the server rebooted, it still asks for me to insert a cd rom. where can I exactly check the settings for the boot menu? Should it use the hard disk since I didnt insert any cd? pls advise (1 Reply)
Discussion started by: lhareigh890
1 Replies

6. Solaris

Forcing OpenBoot: H/W Solution to no <BREAK> function on USB Serial

Please note: This solution does require some soldering ability. If, like me today, you really really need to get into OpenBoot on a non booting Solaris machine (in my case an Ultra 5) but your USB dongle won't send <BREAK> then here is a -really- simple hardware hack that works a treat: ... (0 Replies)
Discussion started by: HideawayStudio
0 Replies

7. Red Hat

attempting to install chkconfig 1.3.5

Hi there everyone, I'm using redhat 7.3 at the moment and am currently trying to install chkconfig-1.3.5-3.i386.rpm , but when I type this command: rpm -Uvh chkconfig-1.3.5-3.i386.rpm I get the following error: error: failed dependencies: chkconfig = 1.2.24 is needed by... (3 Replies)
Discussion started by: redhat_newb101
3 Replies

8. IP Networking

Error with ifconfig when attempting to switch to 10baseT

In Darwin, when typing "ifconfig en0 media 10baseT/UTP mediaopt half-duplex" I recieve the error message "ifconfig: SIOCSIFMEDIA: Operation not permitted". The same thing occurs when I sudo the command. Any suggestions? Thanks... (1 Reply)
Discussion started by: tkarrde
1 Replies
Login or Register to Ask a Question