2014-07-04 2 views
1

누구든지 아래 명령 출력을 구문 분석하고 특정 값을 변수에 저장할 수있는 방법을 제안 해 주실 수 있습니까? 변수에 :쉘 스크립트에서 명령 출력을 구문 분석하는 방법

sestatus는

이 그 명령 저는 여기에 현재 모드의 "시행"을 저장할

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

의 출력입니다.

아무도 나에게 제안 해 줄 수 있습니까?

감사의 말 전진.

+0

덕분에 모든 ... 모든 대답은 유용했다. – Honey

답변

2

당신은 sed를 사용할 수 있습니다

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

$(cmd) 구문은, command substituion 호출되는 문 cmd의 출력에 의해 확장되는 것입니다.

2

잘라내거나 나오지도, 누구의 구현이 사용하기 충분 사용할 수

[[email protected] ~]# sestatus 
SELinux status:     enabled 
SELinuxfs mount:    /selinux 
Current mode:     enforcing 
Mode from config file:   enforcing 
Policy version:     24 
Policy from config file:  targeted 
[[email protected] ~]# variable=`sestatus | grep 'Current mode'|cut -f2 -d ":"` 
[[email protected] ~]# echo $variable 
enforcing 
[[email protected] ~]# 

이 위보다 쓰기 간단합니다.

2

당신은 AWK를 사용할 수 있습니다

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