[英]How to parse a command output in shell script
任何人都可以建議我如何解析以下命令輸出並將特定值存儲在變量中。
狀態
這是該命令的輸出
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 24
Policy from config file: targeted
在這里,我想將當前模式的“強制”存儲在一個變量中。
任何人都可以請建議我。
提前致謝。
您可以使用 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 ~]#
這比上面寫起來簡單。
您可以使用sed
:
variable=$(the_command | sed -n 's/Current mode: \(.*\)/\1/p')
$(cmd)
語法稱為command substituion ,該語句將被cmd
的輸出擴展。
您可以使用 awk:
variable=`sestatus | awk '/Current mode:/ {print $3}'`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.