How to parse a command output in shell script How to parse a command output in shell script linux linux

How to parse a command output in shell script


you can use cut or sed, anyone implementation is good enough to use,

[root@giam20 ~]# sestatusSELinux status:                 enabledSELinuxfs mount:                /selinuxCurrent mode:                   enforcingMode from config file:          enforcingPolicy version:                 24Policy from config file:        targeted[root@giam20 ~]# variable=`sestatus | grep 'Current mode'|cut -f2 -d ":"`[root@giam20 ~]# echo $variableenforcing[root@giam20 ~]#

this is simple to write than above.


You can use sed:

variable=$(the_command | sed -n 's/Current mode: \(.*\)/\1/p')

The $(cmd) syntax is called command substituion, the statement will being expanded by the output of cmd.


You can use awk:

variable=`sestatus | awk '/Current mode:/ {print $3}'`