0

I have this bash function:

botch_get_prev_cd(){
    local dr=$(declare -f cd);
    local dr_trimmed="$(tr -d "$dr")"

    if [[ "$dr_trimmed" != *"run_botch"* ]]; then
        definition_prev_cd="prev_$dr"
        echo "$definition_prev_cd";
        eval "${definition_prev_cd}"
        unset definition_prev_cd  # clean up, no reason to leave this string around
        export -f prev_cd
    fi
}

when I call it, my routine freezes, appears like it's just going into an infinite loop.

When I change my code to this, it works:

botch_get_prev_cd(){
    local dr=$(declare -f cd);

    if [[ "$dr" != *"run_botch"* ]]; then
        definition_prev_cd="prev_$dr"
        echo "$definition_prev_cd";
        eval "${definition_prev_cd}"
        unset definition_prev_cd  # clean up, no reason to leave this string around
        export -f prev_cd
    fi
}

so maybe I don't need to trim the whitespace to check for matching characters? I hope so, because the first function doesn't seem to work, not sure why it seems to go into an infinite loop..

4

2 Answers 2

3

This isn’t an infinite loop, it’s

tr -d "$dr"

waiting for input.

I’m not sure exactly what you’re trying to do there; if you want to use tr to delete characters from a value stored in a variable, you need to give it the value:

tr -d " " <<<"$dr"

would delete all spaces from the value stored in dr.

1

One problem with your code is this line

local dr_trimmed="$(tr -d "$dr")"

I assume that $dr is a range of characters for tr to delete from stdin, but you have forgotten to provide tr with any data to process. So it's sitting there waiting for some.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .