20

Can anyone please suggest me that how can I parse the below command output and store particular value in a variable.

sestatus

This is the output of that command

SELinux status:                 enabled  
SELinuxfs mount:                /selinux  
Current mode:                   enforcing  
Mode from config file:          enforcing  
Policy version:                 24  
Policy from config file:        targeted

Here I want to store "enforcing" of Current mode: in a variable.

Can anyone please suggest me.

Thanks In Advance.

1
  • Thanks All...all answers were useful. Commented Jul 4, 2014 at 11:41

3 Answers 3

13

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

[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 ~]#

this is simple to write than above.

Sign up to request clarification or add additional context in comments.

Comments

8

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.

Comments

8

You can use awk:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.