Search code examples
bashumask

how to set umask for directory and exit places


I am writing a script which is creating directories to be used using the following command mktemp -d

I've to add a umask to the directories being created in the above way. And I've to add it to the exit conditions of the code.

Below is the sample code:

DIR1=$(mktemp -d)

wget_output=$(wget -q -P "$DIR1" "$CERT1")
    if [ $? -ne 0 ]; then
        echo "Certificates NOT Found OR Saving the certificates in directory failed."
        exit
    fi

How to do it?


Solution

  • I've found the solution.

    In the begining of the file record original umask

    umask=$(umask)

    then set the value before the directory creation.

    umask 077
    DIR1=$(mktemp -d)
    
    wget_output=$(wget -q -P "$DIR1" "$CERT1")
        if [ $? -ne 0 ]; then
            echo "Certificates NOT Found OR Saving the certificates in directory failed."
            exit
        fi
    

    then in the end restore umask using

    umask ${umask}

    So, the updated code becomes:

    umask=$(umask) #record umask
    .
    .
    .
    umask 077 # set umask value
    DIR1=$(mktemp -d)
    
    wget_output=$(wget -q -P "$DIR1" "$CERT1")
        if [ $? -ne 0 ]; then
            echo "Certificates NOT Found OR Saving the certificates in directory failed."
            exit
        fi
    
    umask ${umask} # restore umask