rm -rf !(*.txt) 终端不报错为什么在脚本中会报错?

weixin_39441020 2017-07-15 02:55:32
在终端中执行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)'
不懂为什么会报错?要怎么修改?
...全文
2611 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaowei190684 2019-10-23
  • 打赏
  • 举报
回复
引用 5 楼 gaowei190684的回复:
rm -rf !(*.txt) 在交互式shell下面 ! 表示历史记录扩展功能,语法没有错。在脚本默认关闭了历史记录扩展功能,所以语法报错。 如果怀疑我说的!的功能,请 man bash 看一下就知道了。
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. 内容太多,后面省略,请查看链接
gaowei190684 2019-10-23
  • 打赏
  • 举报
回复
rm -rf !(*.txt) 在交互式shell下面 ! 表示历史记录扩展功能,语法没有错。在脚本默认关闭了历史记录扩展功能,所以语法报错。 如果怀疑我说的!的功能,请 man bash 看一下就知道了。
weixin_39064137 2019-05-15
  • 打赏
  • 举报
回复
登录给老铁一个赞
weixin_39441020 2017-07-18
  • 打赏
  • 举报
回复
rm -rf !(*.txt)中的括号是个特殊符号,因此会报错。替代语句是rm -rf `find . ! -name "*.txt"`.感谢大神。
weixin_39441020 2017-07-18
  • 打赏
  • 举报
回复
第一行是有#!/bin/bash的,我只是没有写到这里。
pyqix 2017-07-15
  • 打赏
  • 举报
回复
试试第一行加上 #!/bin/bash 或者 #!/bin/sh 等。

1,543

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 Solaris
社区管理员
  • Solaris
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧