일반적으로 우리는
sh script.sh 1>t.log 2>t.err
로그를 리디렉션합니다.
Variant를 사용하여 기록하려면 어떻게해야합니까?
string="1>t.log 2>t.err"
sh script.sh $string
이 목적을 위해 내장 된 'eval'쉘을 사용해야합니다. bash
명령의 맨 페이지에 따라 :
eval [arg ...] The args are read and concatenated together into a single command. This command is then read and exe‐ cuted by the shell, and its exit status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0.
다음과 같이 명령을 실행하십시오.
eval sh script.sh $string
그러나 정말로 명령을 script.sh
통해 실행해야 sh
합니까? 대신 스크립트 자체 에 sh
인터프리터 줄 ( #!/bin/sh
셸 스크립트의 첫 번째 줄로 사용 )을 넣고 실행 권한을 부여하면 ls
명령의 반환 코드에 액세스 할 수 있습니다 . 다음은를 사용 sh
하고 사용하지 않는 예입니다 sh
. 종료 코드의 차이를 확인하십시오.
참고 : try.sh
현재 디렉토리에 파일 이 하나뿐입니다 . 따라서 ls
명령은 반환 코드 2와 함께 종료되도록 바인딩되었습니다.
$ ls try1.sh try1.sh.backup 1>out.txt 2>err.txt
$ echo $?
2
$ eval sh ls try1.sh try1.sh.backup 1>out.txt 2>err.txt
$ echo $?
127
두 번째 경우 종료 코드는 sh
쉘입니다. 첫 번째 경우 종료 코드는 ls
명령입니다. 필요에 따라 신중한 선택을해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다