내가 main.sh에 있다고 가정 해보십시오.
$NAME="a string"
if [ -f $HOME/install.sh ]
. $HOME/install.sh $NAME
fi
그리고 install.sh에서 :
echo $1
이것은 "a string"
, 그러나 아무것도 울리지 않습니다. 왜?
세 가지 오류가 있습니다.
할당 라인이 잘못되었습니다 :
$NAME="a string"
변수에 할당 할 때는 $
를 포함하지 않습니다. 그것은해야한다:
NAME="a string"
then
;이 없습니다. 조건부 행은 다음과 같아야합니다.
if [ -f $HOME/install.sh ]; then
공백이 있어도 $NAME
을 (를) 인용하지 않습니다. 소스 라인은 다음과 같아야합니다.
. $HOME/install.sh "$NAME"
스크립트를 소싱하기 전에 매개 변수를 설정하십시오!
#!/bin/bash
NAME=${*:-"a string"}
if [[ -f install.sh ]];
then
set -- $NAME ;
. install.sh ;
fi
exit;
#!/bin/bash
echo " i am sourced by [ ${0##*/} ]";
echo " with [ [email protected] ] as parametr(s) ";
exit;
[email protected]$ ./main.sh some args
i am sourced by [ main.sh ]
with [ some args ] as parametr(s)
[email protected]$