Does processing "fall through" functions? Plus...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Does processing "fall through" functions? Plus...
# 1  
Old 08-28-2015
Does processing "fall through" functions? Plus...

Hi,

I'm new here, and hope that someone can help explain how a script that I'm working with works?

The code looks like this:

Code:
#!/bin/bash


export PATH=$PATH:.:/usr/sbin:/sbin



function validate_xxx()
{
echo "validating xxx parameters"

if [ -d "$PLATFORMDIR" ]; then
.
.
.
else
.
.
.
fi
}

function cleanup_disks ()
{
    DRIVES=
    for DEV in /sys/block/*; do
.
.
.
    done
    echo "Confirm drives to cleanup:"
    DRIVES2CLEANUP=
    select DRIVE in ${DRIVES}; do
        DRIVES2CLEANUP="${DRIVES2CLEANUP} ${DRIVE}"
    done
    if [ "${DRIVES2CLEANUP}" = "" ]; then
        echo "No drives to cleanup";
    else
        echo "Cleaning up drives: $DRIVES2CLEANUP"
        for DRIVE in ${DRIVES2CLEANUP}; do
.
.
.
        done
    fi
}

if [ "$(whoami)" != "root" ]; then
  echo "You must be root to run $0"
  exit 1
fi

echo ""
echo ""
echo ""
echo "Cleanup Script"
echo ""
validate_xxx

When I run this script it outputs:

Code:
Cleanup Script
validating xxx
Confirm drives to cleanup:
1)sda
2)sdb
#?

I can see that it is calling the "validate_xxx" function, and executing that, but it also appears to then be processing the "cleanup_disks" function, but I don't see anywhere that the "cleanup_disks" function is being called?

It almost seems like processing is "falling through" the "cleanup_disks" function which is just right "under" the validate_xxx function?


The other question that I have is that it is prompting to enter the drives to be cleaned up, and I can enter data, e.g., "sda", but then it keeps re-prompting for more choices. Pressing just Enter doesn't seem to make it start processing the "for DRIVE in ${DRIVES2CLEANUP}; do" loop to actually do the cleanup.

Why is that part not working? It seems like when the Enter key is pressed, the "if [ "${DRIVES2CLEANUP}" = "" ]; then" should detect that, and start the cleanup code?

Thanks!!

Jim
Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for code segments.

Last edited by Don Cragun; 08-28-2015 at 06:18 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-28-2015
Trying to guess at what:
Code:
.
.
.

in your code is doing is hard, and my crystal ball is cloudy today. Please show us the rest of your script (in CODE tags).
# 3  
Old 08-28-2015
Unfortunately, you put the ellipses in lieu of the most interesting stuff. I can't find anything responsible for the described behaviour in what you posted; the reasons must be in one of the ellipses.

Regarding the other question: you must explicitly leave a select statement, like e.g. [ "$DRIVE" == "EXIT" ] && break.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-28-2015
Hi,

Thanks for the responses.

There isn't a call to the function "cleanup_disks" anywhere in the code (including none being in the "..." parts).

THAT is what is puzzling me. If there is no call to the "cleanup_disks" function, what is causing it to be executed? That was why I had the Subject that I had... I was wondering if there might be something "special" about being the last function or something like that.

Otherwise, if the "cleanup_disks" function isn't explicitly called, then why is it being executed?

Thanks,
Jim
# 5  
Old 08-28-2015
Quote:
Originally Posted by ohaya
Hi,

Thanks for the responses.

There isn't a call to the function "cleanup_disks" anywhere in the code (including none being in the "..." parts).

THAT is what is puzzling me. If there is no call to the "cleanup_disks" function, what is causing it to be executed? That was why I had the Subject that I had... I was wondering if there might be something "special" about being the last function or something like that.

Otherwise, if the "cleanup_disks" function isn't explicitly called, then why is it being executed?

Thanks,
Jim
We never said there was a call to the cleanup_disks function in your script. We said that if we can't see your script, we can't tell you what is going wrong.

If you show us your code, we may be able to help you. If you don't show us your code, there is an extremely small chance of us being able to guess what your code is doing. The choice is yours...
# 6  
Old 08-29-2015
Hi, ohaya

If you decide not to post the relevant code as suggested, you could try to demystify what's puzzling you by seeing how the script is executing each line.
Try addingset -x at the top of the script or run it as bash -x scriptname
This User Gave Thanks to Aia For This Post:
# 7  
Old 08-29-2015
Aia,

Thanks! That was helpful because when I ran bash -x, it showed that erase_disk was actually being called, which at first puzzled me. I then made a copy of the .sh file and started editing it to try to get a working example that I could post fully here. When I did that, and got to the end of the file, guess what?

There was another line at the end that had a call to the cleanup_disks function Smilie!!!

I don't know how I missed that before, but at least that solves that mystery!

Thanks!

Jim

---------- Post updated at 06:44 AM ---------- Previous update was at 06:42 AM ----------

Ok, I think I see how I missed that line. It looks like it doesn't have a newline at the end, so when I do "cat xxx.sh", that last line was kind of blending in with the prompt Smilie...

Jim

---------- Post updated at 07:01 AM ---------- Previous update was at 06:44 AM ----------

Quote:
Originally Posted by RudiC
Unfortunately, you put the ellipses in lieu of the most interesting stuff. I can't find anything responsible for the described behaviour in what you posted; the reasons must be in one of the ellipses.

Regarding the other question: you must explicitly leave a select statement, like e.g. [ "$DRIVE" == "EXIT" ] && break.

I myself haven't used that "select" construct before, but it seems like whoever wrote this code also misunderstood how it works...

I think that the intention was to have it list out the set of drives, and then let the user enter/choose which one to clean. I'll have to re-configure the code so that the processing of the choice gets done between the "do" and the "done", I think, right?

Jim
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Programming

How can I not fall in "not responding " while doing long loop?

Hi .. I'm a student that I helped by you when I was learning c for unix.. I want to ask you about different thing this time and I know you can help me be cause I like your style helping people.. I want to ask about .net c# ... I did a simple program that make me control my windows system... (3 Replies)
Discussion started by: fwrlfo
3 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

user-defined functions, "$@", $jobs and $ps wwaxu

Imagine a user-defined function. func() { /usr/pkg/bin/program long-string-of-switches-and-configs "$@" ;} I execute it once. Then background it. I execute another instance. Then bg it. func unique-user-input ^Z func unique-user-input ^Z First I view with ps ps wwaux ... (0 Replies)
Discussion started by: uiop44
0 Replies

7. Shell Programming and Scripting

Strange behaviour with "set -e" and functions

Hi, This seems to be a recurrent problem on mailing lists and bug reports, but I've been unable to find a solution. Let's imagine we have this bash script: #!/bin/bash set -e fun() { echo "fun_start" test 1 = 2 echo "fun_end" } echo "main_start" fun echo "main_end"This... (1 Reply)
Discussion started by: tokland
1 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question