如何合并两个Linux命令,使输出包含用户密码信息与登录Shell?
Absolutely! You can combine both tasks into a single, efficient command that produces exactly the output format you want:
while IFS=: read -r user _ _ _ _ _ shell; do echo -e "\n$user" chage -l "$user" echo "Linux shell : $shell" done < /etc/passwd >> users-list.log
Breakdown of how this works:
- Parsing
/etc/passwdcorrectly: Thewhile IFS=: read -r user _ _ _ _ _ shellloop splits each line of/etc/passwdusing colons as delimiters. We capture the first field (username) asuserand the seventh field (login shell) asshell, using underscores to ignore the intermediate fields we don't need. - Matching your original output structure:
echo -e "\n$user"adds a newline before each username, mirroring the formatting of your initial command's output. - Retrieving password details:
chage -l "$user"pulls up the password expiration and account settings for each user, just like your first command did. - Adding the shell info:
echo "Linux shell : $shell"appends the login shell line exactly as you specified in your desired output. - Saving to the log file:
>> users-list.logappends all generated content to yourusers-list.logfile, preserving any existing entries (consistent with your original command's behavior).
This approach is more efficient than your original setup because it avoids spawning multiple bash processes via xargs—all operations run within a single shell loop.
内容的提问来源于stack exchange,提问作者Romain
相关产品推荐
相关产品推荐

