2012-03-02 53 views
3

我是壳牌的绿色之手。请参阅我的以下代码。如何使它工作?带参数扩展的外壳通配符

[[email protected] ~]# ls {*.txt,*.exe} 
a.txt b.txt a.exe b.exe 
[[email protected] ~]# filter={*.txt,*.exe} 
[[email protected] ~]# echo $filter 
{*.txt,*.exe} 
[[email protected] ~]# ls $filter 
ls: {*.txt,*.exe}: No such file or directory 
[[email protected] ~]# 
+0

哪贝壳?他们都不同。 – 0xC0000022L 2012-03-02 04:07:32

+0

这是Bash Shell – 2012-03-02 04:11:38

+1

另外,这看起来像是根提示。在尝试时以root身份登录被普遍认为是一个非常糟糕的主意。 – tripleee 2012-03-02 06:08:13

回答

5

这应该做的伎俩:

eval ls $filter 
2

注:假设Bash和Linux的在这里没有任何具体的信息。

首先,您可以通过find(例如,

find -type f -name '*.sql' 

...或echo

echo *.{sql,log} 

为好。许多方法在* nix shell中执行一件事:)

其次,您必须使用eval表达式来使用通配符功能(globbing是通配符扩展的技术术语)。

你也想在单引号的分配,以避免膨胀发生得太早:

filter='*.{sql,log}' 

然后观察:

$ echo $filter 
*.{sql,log} 

但:

$ eval "echo $filter" 
test.sql test.log