close
bash-3.2$ FILENAME=somefile.tar.gz //指定黨名 bash-3.2$ echo ${FILENAME%%.*} //設定抓取檔名(%%. 指:用.為分段來截斷最長的字串) somefile bash-3.2$ echo ${FILENAME%.*} /
/設定抓取副檔名(%.
指:用.為分段來截斷最短的字串)
somefile.tar
#!/bin/bash
FULL_PATH=/d/d1/F1/sh/runDIR.239.sh.ppw
echo "FULL_PATH=$FULL_PATH "
echo "%%.*=${FULL_PATH%%.*} "
echo "%.*FULL_PATH=${FULL_PATH%.*} "
echo " "
echo "##*.=${FULL_PATH##*.} "
echo "#*.=${FULL_PATH#*.} "
echo " "
echo "##*/=${FULL_PATH##*/} "
echo "#*/=${FULL_PATH#*/} "
[root@fopa01 dir]# ./dir.sh
FULL_PATH=/d/d1/F1/sh/runDIR.239.sh.ppw
%%.*=/d/d1/F1/sh/runDIR
%.*FULL_PATH=/d/d1/F1/sh/runDIR.239.sh
##*.=ppw
#*.=239.sh.ppw
##*/=runDIR.239.sh.ppw
#*/=d/d1/F1/sh/runDIR.239.sh.ppw
This site explains more.
${variable%pattern*}
Trim the shortest match from the end (從結尾去除文字,直到遇見第一個相同的特徵)
${variable##pattern*}
Trim the longest match from the beginning(從開頭去除文字,直到遇見最後相同的特徵)
${variable%%pattern*}
Trim the longest match from the end(從結尾去除文字,直到遇見最後相同的特徵)
${variable#pattern*}
Trim the shortest match from the beginning(從開頭去除文字,直到遇見第一個相同的特徵)
[root@fopa01 sh]# basename /d/d1/123.txt //去除資料夾路徑
123.txt
[root@fopa01 sh]# basename /d/d1/123.txt .txt //去除資料夾路徑+去除指定副檔名
123
全站熱搜