Hon*_*ney 5 linux shell parsing
任何人都可以建议我如何解析以下命令输出并将特定值存储在变量中。
塞斯塔图斯
这是该命令的输出
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 24
Policy from config file: targeted
Run Code Online (Sandbox Code Playgroud)
在这里,我想将“当前模式”的“强制”存储在一个变量中。
谁能建议我。
提前致谢。
您可以使用 cut 或 sed,任何实现都可以使用,
[root@giam20 ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 24
Policy from config file: targeted
[root@giam20 ~]# variable=`sestatus | grep 'Current mode'|cut -f2 -d ":"`
[root@giam20 ~]# echo $variable
enforcing
[root@giam20 ~]#
Run Code Online (Sandbox Code Playgroud)
这比上面写起来简单。
您可以使用sed:
variable=$(the_command | sed -n 's/Current mode: \(.*\)/\1/p')
Run Code Online (Sandbox Code Playgroud)
该$(cmd)语法称为命令substituion,该语句将被输出扩展cmd。
小智 6
您可以使用 awk:
variable=`sestatus | awk '/Current mode:/ {print $3}'`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12956 次 |
| 最近记录: |