3

In ~/.bash_aliases I want to create an alias for repeat word that will repeat given command n times.

repeat() {
    n=$1
    shift
    while [ $(( n -= 1 )) -ge 0 ]
    do
        "$@"
    done
}

I want to use repeat to list updated files in a directory so I made following function (WAIT,CLEAR,LIST):

wcls() {
    m=$1
    shift
    clear
    date
    ls -l "$@"
    sleep $m
}

I have a folder where are my_file1 and my_file2. If I run the script :

repeat 500 wcls 2 my_file*

i get

my_file1 ...
my_file2 ...

and in the mean time I change my_file2 to my_file3 the script wont update the contents showing

my_file1 ...
my_file2 no such file or directory

what should I do for my functions to correctly handle asterisks?

4

1 回答 1

2

这里的问题是您的星号正在被交互式外壳扩展。当你执行别名时,你给它一个文件列表,而不是一个带有星号的文件规范。

除了转义星号并让别名进行扩展之外,我认为没有简单的方法可以解决此问题。这是一个坏主意,因为带有有趣字符的文件名可能仅通过它们的存在来影响正在执行的内容。

于 2012-11-09T12:50:03.930 回答