简体   繁体   中英

Looping through awk command using bash in RHEL

I am trying to loop through the output of an awk command in bash. I saved the output to a variable and when I loop through, all of the output is saved as one long string instead of each one their own individual index. How would I be able to use each individual string? Any other suggestions? Thanks!

variable=`awk -F '$3 > 1000 { print $1 }' < /etc/password | grep -v 'person1\|person2'`

for x in "{$variable[@]}";do
chage -M 60 "$x"

On surface, the goal is to run chage... on any user with uid > 1000, excluding two predefined users (person1, person2). Minor fix to the loop should address the problem.

Two minor issues are fixed: the awk is using ':' as field separator, and the bash loop iterate over a simple variable (instead of array). Solution integrate the user filter into awk scripts, eliminating the separate grep :

user_list=`awk -F: '$3 > 1000 && $1 !~ "$(person1|person2)$" { print $1 }' < /etc/password`

for x in $user_list ; do
    chage -M 60 "$x"
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM