2

展開する必要があるスペースとグロブ文字 (*?) の両方を含むパターンに一致する Posix シェル スクリプト関数を作成したいと考えています。Python では、glob.glob('/tmp/hello world*')は正しいリストを返します。シェルでこれを行うにはどうすればよいですか?

#!/bin/sh

## this function will list
## all of the files in the /tmp
## directory that match pattern
f() {
  PATTERN="$1"
  ls -1 "/tmp/$PATTERN"
}

touch '/tmp/hello world {1,2,3}.txt'
f 'hello world*'
4

2 に答える 2

5

*引用符以外のすべてを囲むことができます。

ls -l "hello world"*
ls -l "hello world"*".txt"

その後、引用符で囲まれた文字列を に渡すことができますf()。内部で文字列を使用するにf()は が必要ですeval

#!/bin/sh

## this function will list
## all of the files in the /tmp
## directory that match pattern
f() {
  PATTERN=$1
  eval ls -1 "/tmp/$PATTERN"
}

touch '/tmp/hello world {1,2,3}.txt'
f '"hello world"*'
于 2013-03-27T22:07:10.820 に答える
1

findのパターン マッチングは、シェルのものとまったく同じではありませんが、かなり近いので、それを利用できます。

f() {
    find . -mindepth 1 -maxdepth 1 -name "$1" | sed 's#^.*/##'
}

(sedファイル名からパス接頭辞を削除するためのコマンドがあります。)

于 2013-03-28T01:37:29.717 に答える