1

this is my code inside a function

function safeChmod_ProcessOctalModes() {
    local i=0
    local targetpermission=""
    local chmod_param=$1
    # local folder_chmod_param=`echo $chmod_param | fold -w1`
    # echo xxxxxxxxxxxx=$folder_chmod_param >/dev/tty
    echo -n "$chmod_param" | \
    while read -n 1 ch;
    do
        if [ $i -eq 0 ]
        then
            i=$(( i+1 ))
            continue
        fi
        i=$(( i+1 ))

        if [ $ch -eq 0 ]
        then
            targetpermission=($targetpermission"---")
            echo tp=$targetpermission >/dev/tty
        elif [ $ch -eq 1 ]
        then
            targetpermission=($targetpermission"--x")
            echo tp=$targetpermission >/dev/tty
        elif [ $ch -eq 2 ]
        then
            targetpermission=($targetpermission"-w-")
            echo tp=$targetpermission >/dev/tty
        elif [ $ch -eq 3 ]
        then
            targetpermission=($targetpermission"-wx")
            echo tp=$targetpermission >/dev/tty
        elif [ $ch -eq 4 ]
        then
            targetpermission=($targetpermission"r--")
            echo tp=$targetpermission >/dev/tty
        elif [ $ch -eq 5 ]
        then
            targetpermission=($targetpermission"r-x")
            echo tp=$targetpermission >/dev/tty
        elif [ $ch -eq 6 ]
        then
            targetpermission=($targetpermission"rw-")
            echo tp=$targetpermission >/dev/tty
        elif [ $ch -eq 7 ]
        then
            targetpermission=($targetpermission"rwx")
            echo tp=$targetpermission >/dev/tty
        fi
    done 
    echo tp_in_func=$targetpermission >/dev/tty
    echo $targetpermission
    return 0;
}

Till I am within the while loop, targetpermission variable is populated correctly. But, as soon as the loop is done, the targetpermission is none.

This is the output.

chmod_param=0700
tp=rwx
tp=rwx---
tp=rwx------
tp_in_func=

Why is this happening ? How to I retain thetargetpermission variable's assigned value outside the while loop ?

codeforester
  • 39,467
  • 16
  • 112
  • 140
anu
  • 1,017
  • 1
  • 19
  • 36
  • 2
    If you pipe into a while loop, the while loop will be in a subshell. – Petr Skocik Oct 15 '15 at 01:41
  • 2
    The pipe `|` causes the while loop to be executed as a parallel process, hence losing the variable when while loop ends. You can change it to `while (condition); do ... done <<< "$chmod_param"` – alvits Oct 15 '15 at 01:41

2 Answers2

2

Change:

echo -n "$chmod_param" | \
while read -n 1 ch; do 
  #...
done

into

while read -n 1 ch; do 
  #...
done <<<"$chmod_param"

or more generally:

while read -n 1 ch; do 
   #...
done < <( echo -n "$chmod_param" )

to prevent the subshell creation that piping into a while loop results into.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
1

I've seen behavior like this before and it was due to certain constructs in bash creating a new context. Basically anything that creates another shell, like the backtick operator or the $() operator have their own context. In your case I think it might be the while loop being fed by echo.

codesniffer
  • 1,033
  • 9
  • 22