Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

shell - 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"
question from:https://stackoverflow.com/questions/65836206/looping-through-awk-command-using-bash-in-rhel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...