Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...