${variable[token][pattern]}
[token]
從頭開始找:
## 取代掉最多符合的
# 取代掉最少符合的
從尾開始找:
%% 取代掉最多符合的
% 取代掉最少符合的
[範例]
/t/t_var.sh
#!/bin/sh
var=$1
echo \${var##/*/}
echo ${var##/*/}
tail=${var%%*/}/a.txt
echo "process with $tail"
echo \${tail##$var/}
echo ${tail##$var/}
[結果]
root@7[mktmf]# /c/t_var.sh /path/path/fname.txt
${var##/*/}
fname.txt <===從一開始的/到最後的/都被取代掉
process with /path/path/fname.txt/a.txt
${tail##/path/path/fname.txt/}
a.txt
參考資料: http://linux.vbird.org/linux_basic/0320bash.php
【關於判定檔案或目錄】
-d file
True if file exists and is a directory. 確認是否有此目錄,
-f file
True if file exists and is a regular file. 確認是否有此檔案
!!! 但如果丟空字串, 會傳回 true !!!
【關於判定字串】
-z string
True if the length of string is zero. 是否為空字串
string
-n string
True if the length of string is non-zero. 是否不是空字串
* 要表示 否定, 就 在前面加上 !
[範例程式] 可以避開空字串, 正確判定是目錄或檔案
#!/bin/sh
SDIR=$1
if [ $SDIR ]; then
if [ -d $SDIR ] ;then
echo "is a directory"
elif [ -f $SDIR ] ;then
echo "is a file"
else
echo "not file or directory"
fi
else
echo "null"
fi
參考: man sh
沒有留言:
張貼留言