BASH for loop exiting after calling function

EmptyArsenal

I'm working on a bash scripts that will essentially shift the entire contents of a directory, where the filenames all start with a number (0001, 0002, etc). I'm iterating in reverse order through those files to shift the last one down first.

I have one function that iterates through the files and calls another function to actually shift them. When I call the second function, it short-circuits the for loop and exits. I can't figure out why.

validation_dir="scripts/validation"
content_directories=($validation_dir "tutorials")

function shift_contents() {
    local start_index=$1
    local positions=$2

    local files=(${validation_dir}/*)
    for ((i=${#files[@]}-1; i>=0; i--)); do
        # Only gets called once
        echo $i
        local f="${files[$i]}"
        local old_index=$(echo $f | tr -dc '0-9' | sed -e 's/^0*//')
        if [ "$old_index" -lt "$start_index" ]
        then
            # Only start when we're at the start index
            continue
        fi
        local new_index=$((old_index + positions))
        # When this is called, it exits the loop
        move_exercise $old_index $new_index
    done
}

Here's the function that does the shifting:

function move_exercise() {
    local start_index=$1
    local end_index=$2
    local start_prefix=$(echo_filename_prefix $start_index)
    local end_prefix=$(echo_filename_prefix $end_index)

    for i in ${content_directories[@]}; do
        start_file_glob="${i}/${start_prefix}*"

        for f in $start_file_glob; do
            if [ -e $f ]
            then
                start_filename=$f
            else
                log_error "No content found with index ${start_index}"
                exit 1
            fi
            break
        done

        end_file_glob="${i}/${end_prefix}*"

        if [ -e $end_file_glob ]
        then
            log_error "Content was already found for end index ${end_index}: ${end_file_glob}"
            exit 1
        fi

        # Generate the new file name
        new_filename="${start_filename/$start_prefix/$end_prefix}"
        # We get down here once
        echo $start_filename $new_filename
    done

    return
}

It looks like the second function is causing the first to exit early, but I don't see how that could happen. See what's going on?

EDIT:

When I run with bash -x script.sh, it ends with the following output when it should have one more iteration:

+ echo path/to/second-to-last-file new-path
+ return
+ (( i-- ))
+ (( i>=0 ))

Does that imply that it's failing the for loop condition check? If so, how could move_exercise impact that? If I comment out that line, it works as expected.

Charles Duffy

If you want i to be local, you need to declare it as such:

shift_contents() {
  local i
  # ...etc...
}

move_exercise() {
  local i
  # ...etc...
}

Otherwise there's only one variable named i shared between both functions. When that variable contains a filename, its numeric value is 0 (unless that filename is also the name of a shell variable with a nonzero numeric value assigned) -- so when you decrement it after assigning a filename, it becomes negative.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to activate a function within an loop to continue after exiting loop

From Dev

bash while loop not exiting and not echoing

From Dev

Additional increment after exiting for loop

From Dev

JQuery Loop stop after calling external function

From Dev

while loop ending after calling a function

From Dev

Calling a function after a specific time, inside a loop

From Dev

For loop stops after calling another function

From Dev

For loop exiting after one loop - MPI

From Dev

Break a loop without exiting script (bash)

From Dev

for loop in bash exiting to early without errors

From Dev

Bash Script Loop Exiting Prematurely on If Statement

From Dev

Exiting the recv loop after a fixed timeout

From Dev

Exiting out of loop after counter ends

From Dev

'While' loop is not exiting after set number of seconds

From Dev

last input not working after exiting the while loop

From Dev

Ctrl D not exiting bash after sourcing bashrc

From Dev

Exiting A While Loop Using A Function Call

From Dev

Why do I get stuck in an infinite loop after calling a function?

From Dev

Calling string from array using loop after onclick function [javascript]

From Dev

Jquery. Calling a function after .each loop with ajax calls

From Dev

loop running infinite time after calling display function

From Dev

Calling a function through a for loop

From Dev

javascript function calling in loop

From Dev

Calling function directly in for loop

From Dev

Calling function in loop header

From Dev

Calling a function to a loop

From Dev

Calling a Function Inside a Loop

From Dev

Calling while loop function in main function, and won't move past after completing loop

From Dev

Python google sheets, for loop exiting after one loop

Related Related

  1. 1

    How to activate a function within an loop to continue after exiting loop

  2. 2

    bash while loop not exiting and not echoing

  3. 3

    Additional increment after exiting for loop

  4. 4

    JQuery Loop stop after calling external function

  5. 5

    while loop ending after calling a function

  6. 6

    Calling a function after a specific time, inside a loop

  7. 7

    For loop stops after calling another function

  8. 8

    For loop exiting after one loop - MPI

  9. 9

    Break a loop without exiting script (bash)

  10. 10

    for loop in bash exiting to early without errors

  11. 11

    Bash Script Loop Exiting Prematurely on If Statement

  12. 12

    Exiting the recv loop after a fixed timeout

  13. 13

    Exiting out of loop after counter ends

  14. 14

    'While' loop is not exiting after set number of seconds

  15. 15

    last input not working after exiting the while loop

  16. 16

    Ctrl D not exiting bash after sourcing bashrc

  17. 17

    Exiting A While Loop Using A Function Call

  18. 18

    Why do I get stuck in an infinite loop after calling a function?

  19. 19

    Calling string from array using loop after onclick function [javascript]

  20. 20

    Jquery. Calling a function after .each loop with ajax calls

  21. 21

    loop running infinite time after calling display function

  22. 22

    Calling a function through a for loop

  23. 23

    javascript function calling in loop

  24. 24

    Calling function directly in for loop

  25. 25

    Calling function in loop header

  26. 26

    Calling a function to a loop

  27. 27

    Calling a Function Inside a Loop

  28. 28

    Calling while loop function in main function, and won't move past after completing loop

  29. 29

    Python google sheets, for loop exiting after one loop

HotTag

Archive