次の関数は、仕事をする:より良い何
# read a number of arguments on the left-hand side; those actual arguments; then the RHS
pipe() {
local nargs
local -a firstCmd
nargs=$1; shift
firstCmd=()
for ((i=0; i<nargs; i++)); do
firstCmd+=("$1"); shift
done
"${firstCmd[@]}" | "[email protected]"
}
# basic use case
pipe 3 ps -ef somethinghere grep java
# or, for a pipeline with more than two components:
pipe 3 ps -ef somethinghere pipe 2 grep java tee log.txt
がeval
を用いて、溶液とは異なり、それがさらに複雑な値でうまくいくということです。
pipe 3 ps -ef 'something with spaces here' grep java
一つは、可能性がありまた、sigil値を使用するこの関数のバージョンを記述してください:
pipe() {
local sigil
local -a firstCmd
sigil=$1; shift
firstCmd=()
while (($#)); do
if [[ $1 = "$sigil" ]]; then
shift
"${firstCmd[@]}" | pipe "$sigil" "[email protected]"
return
else
firstCmd+=("$1")
shift
fi
done
"${firstCmd[@]}"
}
この場合、
、あなたも行うことができます:
sigil=$(uuidgen) # generate a random, per-session value
pipe "$sigil" ps -ef 'something with spaces here' "$sigil" grep java "$sigil" tee log.txt
キーボードレイアウトを変更する方が簡単ですか? – Derlin
@Derlin no。同じロケールの他のユーザーが、同じユーザーの信用を使用している可能性があります。 –
**データ**が**構文**として解析されていないため、これは動作しません。もしそうであれば、悪意のあるデータを処理しています(例えば '/ tmp/$(rm -rf $ HOME)'というファイル)ほとんど不可能だろう。 –