在終端中執行rm -rf !(*.txt),成功洗掉.txt以外的檔案,但是把這句放在腳本中,會報錯。
LOCATION=/export/home/byang/Test
if [ -d $Location ]; then
cd $Location
LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH
ls *.zip > a.asc
if test -s a.asc; then
cat a.asc | while read line
do
echo $line
cd $LOCATION
7za e $line -y
done
fi
cd $LOCATION
pwd
sleep 10
rm -rf !(*.txt)
rm *Summary.txt
fi
報錯資訊為:
$ ./test.sh
./test.sh: line 19: syntax error near unexpected token `!(*'
./test.sh: line 19: `rm -rf !(*.txt)'
不懂為什么會報錯?要怎么修改?
uj5u.com熱心網友回復:
試試第一行加上 #!/bin/bash 或者 #!/bin/sh 等。uj5u.com熱心網友回復:
第一行是有#!/bin/bash的,我只是沒有寫到這里。uj5u.com熱心網友回復:
rm -rf !(*.txt)中的括號是個特殊符號,因此會報錯。替代陳述句是rm -rf `find . ! -name "*.txt"`.感謝大神。uj5u.com熱心網友回復:
登錄給老鐵一個贊
uj5u.com熱心網友回復:
rm -rf !(*.txt) 在互動式shell下面 ! 表示歷史記錄擴展功能,語法沒有錯。在腳本默認關閉了歷史記錄擴展功能,所以語法報錯。如果懷疑我說的!的功能,請 man bash 看一下就知道了。
uj5u.com熱心網友回復:
https://linux.die.net/man/1/bash
History Expansion 部分
The shell supports a history expansion feature that is similar to the history expansion in csh. This section describes what syntax features are available. This feature is enabled by default for interactive shells, and can be disabled using the +H option to the set builtin command (see SHELL BUILTIN COMMANDS below). Non-interactive shells do not perform history expansion by default.
內容太多,后面省略,請查看鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/82262.html
標籤:Solaris
上一篇:求助!ubuntu無線連接,連校園網的時候一直反復讓我輸入校園網的用戶名和密碼,怎么解決啊?
下一篇:solaris防火墻問題
