将两个特定的Linux命令合并为一个

时间:2018-05-15 09:34:52

标签: linux shell

我想结合使用2个Linux命令:

第一个命令:

cut -f 1 -d: /etc/passwd | xargs -n 1 -I {} bash -c "echo -e \"\n{}\"; chage -l {}" >> users-list.log

第二个命令:

cat /etc/passwd | cut -d: -f7

第一个命令获取用户和每个用户的所有详细信息,第二个命令获取登录shell。

目前,第一个命令具有以下输出:

dante
Last password change                    : Aug 18, 2017
Password expires                    : never
Password inactive                   : never
Account expires                     : never
Minimum number of days between password change      : 0
Maximum number of days between password change      : 99999
Number of days of warning before password expires   : 7

marion
Last password change                    : Aug 28, 2017
Password expires                    : never
Password inactive                   : never
Account expires                     : never
Minimum number of days between password change      : 0
Maximum number of days between password change      : 99999
Number of days of warning before password expires   : 7

我希望我的users-list.log文件中的新输出看起来像这样(" Linux Sheel的间距"并不重要):

dante
Last password change                    : Aug 18, 2017
Password expires                    : never
Password inactive                   : never
Account expires                     : never
Minimum number of days between password change      : 0
Maximum number of days between password change      : 99999
Number of days of warning before password expires   : 7
Linux shell                                         : /bin/bash

marion
Last password change                    : Aug 28, 2017
Password expires                    : never
Password inactive                   : never
Account expires                     : never
Minimum number of days between password change      : 0
Maximum number of days between password change      : 99999
Number of days of warning before password expires   : 7
Linux shell                                         : /bin/bash

用一个命令可以实现这个目的吗?

祝你好运, 罗曼

1 个答案:

答案 0 :(得分:1)

使用以下命令

cat /etc/passwd | xargs -n1 -I{} bash -c 'a=`echo "{}" | cut -f1 -d:`; echo -e "\n$a"; chage -l $a; echo -e "Linux shell\t: " `echo "{}" | cut -f7 -d:`' >> users-list.log

为了便于阅读,将其细分如下

cat /etc/passwd | xargs -n1 -I{} bash -c '
  a=`echo "{}" | cut -f1 -d:`;
  echo -e "\n$a";
  chage -l $a;
  echo -e "Linux shell\t: " `echo "{}" | cut -f7 -d:`
' >> users-list.log

反引号中的命令由shell评估。 参考:https://unix.stackexchange.com/questions/48392/understanding-backtick