[问题] 直接调用通过,放到脚本里就报错的两个命令[已解决]

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
5451vs5451
帖子: 345
注册时间: 2006-07-14 18:56
来自: Apple Valley, Planet Tux, Linux System
送出感谢: 0
接收感谢: 0

[问题] 直接调用通过,放到脚本里就报错的两个命令[已解决]

#1

帖子 5451vs5451 » 2007-03-04 23:48

下面两条命令在终端下运行一点问题没有,一放到脚本里就出错。

命令一:

代码: 全选

test $PWD = @(/home/ubuntu/documents|$my_doc_base) || exit 1
错误信息:

代码: 全选

/home/ubuntu/.gnome2/nautilus-scripts/InstallDocument: line 55: syntax error near unexpected token `('
/home/ubuntu/.gnome2/nautilus-scripts/InstallDocument: line 55: `test ! $PWD = (/home/jianghua/documents|$my_doc_base) || exit 1'
命令二:

代码: 全选

temp=$(ls -d [Hh][Tt][Mm]?(L|l)/* * 2>/dev/null | 
    grep -i -m 1 '^\(.*/\)\?index\.html\?\(\.gz\)\?$') ||
temp=$(ls -d [Hh][Tt][Mm]?(L|l)/* * 2>/dev/null | 
    grep -i -m 1 '\.html\?\(\.gz\)\?$')
错误信息:

代码: 全选

/home/ubuntu/.gnome2/nautilus-scripts/InstallDocument: command substitution: line 34: syntax error near unexpected token `('
/home/ubuntu/.gnome2/nautilus-scripts/InstallDocument: command substitution: line 34: `ls -d [Hh][Tt][Mm]?(L|l)/* * 2>/dev/null | '
/home/ubuntu/.gnome2/nautilus-scripts/InstallDocument: command substitution: line 36: syntax error near unexpected token `('
/home/ubuntu/.gnome2/nautilus-scripts/InstallDocument: command substitution: line 36: `ls -d [Hh][Tt][Mm]?(L|l)/* * 2>/dev/null | '
把命令中的?(L|l)去掉后可以运行,但命令功能就变了。
上次由 5451vs5451 在 2007-03-05 1:14,总共编辑 3 次。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙
送出感谢: 4 次
接收感谢: 256 次

#2

帖子 eexpress » 2007-03-05 0:00

命令输出加``包括。
● 鸣学
头像
5451vs5451
帖子: 345
注册时间: 2006-07-14 18:56
来自: Apple Valley, Planet Tux, Linux System
送出感谢: 0
接收感谢: 0

#3

帖子 5451vs5451 » 2007-03-05 1:01

原来是extglob选项没打开,先用shopt -s extglob开启,然后在运行就能通过了。

代码: 全选

If  the  extglob shell option is enabled using the shopt builtin, several extended pattern
       matching operators are recognized.  In the following description, a pattern-list is a list
       of  one  or more patterns separated by a |.  Composite patterns may be formed using one or
       more of the following sub-patterns:

              ?(pattern-list)
                     Matches zero or one occurrence of the given patterns
              *(pattern-list)
                     Matches zero or more occurrences of the given patterns
              +(pattern-list)
                     Matches one or more occurrences of the given patterns
              @(pattern-list)
                     Matches one of the given patterns
              !(pattern-list)
                     Matches anything except one of the given patterns
以前看书不仔细。 :oops:
回复

回到 “Shell脚本”