私の bash スクリプトはファイル名 (または相対パス) を文字列として受け取りますが、そのファイルから読み取る必要があります。スクリプトで直接リテラルとして宣言した場合にのみファイル名から読み取ることができます(引用符なし)...最初は暗黙的に文字列であるため、引数には不可能です。観察:
a="~/test.txt"
#Look for it
if [[ -a $a ]] ; then
echo "A Found it"
else
echo "A Error"
fi
#Try to use it
while read line; do
echo $line
done < $a
b='~/test.txt'
#Look for it
if [[ -a $b ]] ; then
echo "B Found it"
else
echo "B Error"
fi
#Try to use it
while read line; do
echo $line
done < $b
c=~/test.txt
#Look for it
if [[ -a $c ]] ; then
echo "C Found it"
else
echo "C Error"
fi
#Try to use it
while read line; do
echo $line
done < $c
収量:
A Error
./test.sh: line 10: ~/test.txt: No such file or directory
B Error
./test: line 12: ~/test.txt: No such file or directory
C Found it
Hello
上で述べたように、上記のルーチンにコマンド ライン引数を渡すことはできません。