Trying to read from file within existing for loop

52 views Asked by At

Hello I am having a hard time figuring out how to complete the existing script I have written. In a nutsheel I am trying to set disk quotas based on an existing user for any user that doesn't have a quota set to 100MB or 102400 as below in the script. The logic seems to work currently but I have ran out of ideas for how to populate the $USER variable. Any help would be appreciated.

AWK=$(awk '{ print $4 }' test.txt)

USER=$(awk '{ print $1 }' test.txt)

for quota in $AWK;
do
    if [ "$quota" = 102400 ];
    then
        echo "Quota already set to 100MB for user: "$USER""
    else
        echo "Setting quota from template for user: $USER "
        edquota -p username "$USER"
    fi
done

The test.txt file is below:

user1   --  245 0   0
user2   --  245 102400  102400
user3   --  234 102400  102400
user4   --  234 1   0
1

There are 1 answers

0
Barmar On

Use a single loop that reads both variables from the file:

while read -r user a b quota c; do
    if [ "$quota" = 102400 ];
    then
        echo "Quota already set to 100MB for user: "$user""
    else
        echo "Setting quota from template for user: $user "
        edquota -p username "$user"
    fi
done < test.txt

The a, b, and c variables are just used to skip over those fields in the input file.