셸 의 ` $() ${} expr $() ()

5691 단어
` `    :
    .`command`      (`)[   :         ,           ESC   ,   1  , TAB       ,     ]     (command)            。        (backquotes)     (backticks).
  :
A=`ls -l`
 ls -l     A    ls -l        

모든 UNIX 명령 은 결 과 를 가 져 오 거나 출력 하려 면 $() 또는 반 따옴표 를 사용 해 야 합 니 다.
tt=` file test.sh ` echo $tt
#sh test.sh test.sh: ASCII text
tar -zcvf lastmod.tar.gz `find . -mtime -1 -type f -print`
지난 24 시간 (- mtime – 2 는 지난 48 시간) 동안 수 정 된 파일 tar 를 함께 합 니 다.
 
 
 
 
반 따옴표  `$() 결함 도 같 습 니 다. 차 를 갈 아타 지 않 으 면 여러 줄 을 합병 하기 쉽 습 니 다.
[mac@machome ~]$ vi test.sh echo `ps -ef | grep syslog` 
[mac@machome ~]$ sh test.sh root 1363 1 0 12:15 ? 00:00:00 syslogd -m 0 mac 15032 15030 0 18:13 pts/2 00:00:00 grep syslog
[macg@machome ~]$ vi test.sh var=$(ls -l) echo $var
[macg@machome ~]$ sh test.sh total 44 -rw-rw-r-- 1 macg macg 126 Jun 8 23:40 1 -rw-rw-r-- 1 macg macg 0 Jun 8 17:53 100 -rw-rw-r-- 1 macg macg 16 Jun 9 19:00 111.txt -rw-rw-r-- 1 macg macg 15 Jun 9 19:00 22.txt -rw-rw-r-- 1 macg macg 15 Jun 9 19:00 33.txt -rw-rw-r-- 1 macg macg 23 Jun 12 19:20 test.sh
 
 
 
 
wc - l 은 항상 $() 와 함께 출력 을 취하 고 줄 수 를 계산 하여 순환 횟수 를 확인한다.
count=$(more iftmp|wc -l) while [ $i -le $count ]
 
 
 
 
echo +$(  ) ――echo 에서 도 명령 을 실행 할 수 있 으 며, $() 를 통 해 이 명령 의 출력 을 가 져 올 수 있 습 니 다.
더욱 나타 낸다
흔 한 사용: echo 내, awk 와 sed 로 일부 명령 을 수정 한 효과
[macg@machome ~]$ vi test #choose a iinterface to config/sbin/ifconfig -a | awk '$2=="Link"{print($1,$3)}' >iftmp echo "choose one of interface to config"i=1 count=$(more iftmp|wc -l) while [ $i -le $count ] do echo "$i)-------- $(sed -n "${i}p"iftmp)"                  sed 로 출력 ii = $($i + 1) done   
[macg@machome ~]$ sh test choose one of interface to config 1)-------- eth0 encap:Ethernet 2)-------- lo encap:Local 3)-------- sit0 encap:IPv6-in-IPv4 give the interface a host name[such as:xxx-hme0]:
 
   ${} 과 $() 의 차이 점: ${}  무엇 에 쓰 입 니까?echo 에서 변수 가 연결 되 지 않도록 합 니 다.
 
 
 
 ${i} 은 다음 문자열 을 구분 하여 '연독' 을 피 합 니 다.
$ vi ttt.sh GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"echo $GONE
$ sh ttt.sh (ServerType|BindAddress|Port|AddModule|ClearModuleList|AgentLog|RefererLog|RefererIgnore|FancyIndexing  
 
 
 
 
${i} 일반 변 수 를 대체 할 수 있 습 니 다 $i
ttt=${REMOTEHOST} echo ${ttt}
ttt=$REMOTEHOST echo $ttt
[macg@localhost ~]$ sh ttt.sh 192.168.1.11    
[macg@localhost ~]$ sh ttt.sh 192.168.1.11
 
 
 
 
expr   수학 식
셸 언어 에는 연산 공식 이 없다.
, expr 문 구 를 통 해 이 루어 져 야 합 니 다.
expr 변수 ‘+-x/’ 변량
expr $a '+' $b
expr 상수 ‘+-x/’ 상수
[mac@machome ~]$ expr 3 '+' 2 5
연산 기 호 는 모두 따옴표 를 붙 여야 한다.
작은 따옴표
출력 은 결과
 
 
 
 
왜 if expr 3 '+' 2 는 5 인 데 if 는 여전히 then? 0 이 아니면 else 로 가 야 지.
... 때문에
expr 표현 식 은 '반환 값' 을 되 돌려 줍 니 다. '결과' 가 아 닙 니 다.
[mac@machome ~]$ vi test.sh expr 3 '+' 2 echo $?
[mac@machome ~]$ sh test.sh 5                   출력                   되돌아가다       덧셈 만 성공 하면 반환 치 는 0, 0 이 진실 이다.
 
 
 
 
 
 
 
expr 표현 식 계산 이 끝 난 후에 어떻게 결 과 를 얻 습 니까? -
$(expr) 로  … ) 따옴표  …`
사실은 출력 을 취 하 는 것 입 니 다. expr 의 출력 은 결과 입 니 다.
var = expr $a '+' $b echo "final is $var"
var=$(expr $a '+' $b) echo "final is $var"
test.sh: line 7: var: command not found   덧셈 완료 후 결과 할당 오류 final is 
final is 7
 
 
 
 
 
 
 
expr 는 if 구문 에서 [] 를 사용 할 수 없습니다. 여기 서 command 로 생각 하기 때 문 입 니 다.
[mac@machome ~]$ vi test.sh if [ expr 3 '+' 2 ] ; then echo no5 else echo is5 fi
[mac@machome ~]$ sh test.sh test.sh: line 1: [: too many arguments is5   
... 로 바꾸다
[mac@machome ~]$ vi test.sh if  expr 3 '+' 2  ; then               expr 3 '+' 2 는 command echo no5 else echo is5 fi 에 해당 합 니 다.
[mac@machome ~]$ sh test.sh 5 no5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
하지만 결과 판정 을 하려 면 $() 를 추가 하려 면 [] 를 사용 해 야 합 니 다.
표현 식 으로 생각 했 으 니까.
[macg@machome ~]$ vi test.sh if [ $(expr 3 '+' 2) != 5 ];then           그리고 [$와 중간 에 빈 칸 이 있어 야 합 니 다 echo no 5 else echo is 5 fi
[macg@machome ~]$ sh test.sh is5
 
 
 
 
다른 연산 형식 i++ 는 어떻게 실현 합 니까?
)
while [ $i -le $count ] do command i=$(($i+1)) done
 
 
(() 와 [] 작용 이 완전히 같다.
echo input: read i i=$(($i+1)) echo $i    
echo input: read i i=$[$i+1] echo $i
[macg@localhost ~]$ sh ttt.sh input: 6 7   
[macg@localhost ~]$ sh ttt.sh input: 6 7
 
 
 
 
반면 증명 () 과 [] 가 완전히 같다 --- if ()
if (( $# != 3 )); then  echo "usage: $0 host user passwd"fi
if [ $# != 3 ]; then  echo "usage: $0 host user passwd"fi
[macg@localhost ~]$ sh ttt.sh 1 2 usage: ttt.sh host user passwd
[macg@localhost ~]$ sh ttt.sh 1 2 usage: ttt.sh host user passwd

좋은 웹페이지 즐겨찾기