You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何合并两个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/passwd correctly: The while IFS=: read -r user _ _ _ _ _ shell loop splits each line of /etc/passwd using colons as delimiters. We capture the first field (username) as user and the seventh field (login shell) as shell, 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.log appends all generated content to your users-list.log file, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:57:14