我正在嘗試編寫一個採用參數basename的腳本,然後檢查該參數是否有擴展名。如果有,它會打印擴展名。Bash basename語法
這裏是我的代碼:
我呼應最後$基名$ 1檢查基本名稱的輸出是什麼。
一些測試表明:
testfile.sh one.two
./testfile: line 2: one.two: command not found
one.two
testfile.sh ../tester
./testfile: line 2: ../tester: No such file or directory
../tester
所以既不1 $ $基名正在努力。我知道這是一個語法錯誤,所以有人可以解釋我做錯了什麼?
編輯:
我現在已經解決了我的問題:
file=$(basename "$1")
stub=${file%.*}
echo ${file#"$stub"}
從而降低了我的論點爲基本名,謝謝大家。
正確的語法是file = $(basename $ 1)。我建議你使用file = $ {1 ## * /},它將刪除每個'* /'序列。它實際上比basename命令快得多,特別是在循環中處理文件時。 –