나는 간단한 bash 스크립트를 작성했고 그 끝에 $ 0, $ 1, ...과 같은 위치 인수를 테스트하려고했습니다.
echo please enter your name
read name
if [ -z "$name" ]
then
echo please enter your name
fi
if [ -n "$name" ]
then
echo Thank you so much
fi
echo $0
echo $1
echo $2
echo $3
그것을 실행 한 후 출력은 다음과 같습니다.
please enter your name
j
Thank you so much
/bin/reza.sh
왜 $ 0에는 산출물이 있고 다른 것은 아무것도 없었습니까?
스크립트에 입력하는 인수는 $ 1, $ 2, $ 3 순서로 지정됩니다.
이것에서 testscript
:
#!/bin/bash
echo $0 #Gives you the command/script name itself
echo $1 #Gives you the first argument
echo $2 #Gives you the second argument
echo $3 #Gives you the third argument
echo $@ #Gives you all arguments
echo $# #Gives you the total number of arguments excluding the script name
그래서 결과
$./testscript a b c
이다
./testscript
a
b
c
a b c
3
인수가 지정되지 않은 경우 값이 널이거나 아무것도 인쇄되지 않습니다.
$ printf "%sThere is nothing before this.\n" $1
제공합니다 :
There is nothing before this.
참고 :echo
인수를 테스트 하는 데 사용하지 마십시오 . echo는 bash에서와 같이 끝에 자동으로 줄 바꿈을 추가합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다