-2

これが私のスクリプトです:

while [[ $startTime -le $endTime ]]
do

thisfile=$(find * -type f | xargs grep -l $startDate | xargs grep -l $startTime)
fordestination=`cut -d$ -f2 $thisfile | xargs cut -d ~ -f4`

echo $fordestination

startTime=$(( $startTime + 1 ))

done
4

1 に答える 1

1

cut および grep コマンドが動かなくなる可能性があると思います。[ -n "$string" ]コマンドを使用して空でないかどうかを確認し、パラメータが空でないことを確認する必要$stringがあります。あなたの場合、それが空の場合、後でそれを使用するコマンドにファイルを追加しません。つまり、コマンドはおそらくコマンドラインからの入力を待機します(例:$stringが空の場合grep regex $string、 grep はから入力ファイルを受信$stringせず、代わりにコマンド ラインからの入力を待ちます)。これは、どこで問題が発生する可能性があるかを示す「複雑な」バージョンです。

while [[ $startTime -le $endTime ]]
do

thisfile=$(find * -type f)
if [ -n "$thisfile" ]; then
    thisfile=$(grep -l $startDate $thisfile)
    if [ -n "$thisfile" ]; then
        thisfile=$(grep -l $startTime $thisfile)
        if [ -n "$thisfile" ]; then
            thisfile=`cut -d$ -f2 $thisfile`

            if [ -n "$thisfile" ]; then
                forDestination=`cut -d ~ -f4 $thisfile`
                echo $fordestination
            fi
        fi
    fi
fi

startTime=$(( $startTime + 1 ))

done

そして、これがより単純なバージョンです:

while [[ $startTime -le $endTime ]]
do

thisfile=$(grep -Rl $startDate *)
[ -n "$thisfile" ] && thisfile=$(grep -l $startTime $thisfile)

[ -n "$thisfile" ] && thisfile=`cut -d$ -f2 $thisfile`
[ -n "$thisfile" ] && cut -d ~ -f4 $thisfile

startTime=$(( $startTime + 1 ))

done

"-R" は grep にファイルを再帰的に検索するように指示し、&&は bash に前のコマンドが成功した場合にのみ後続のコマンドを実行するように指示し、前のコマンド&&はテスト コマンド ( ifs で使用) です。

これが役立つことを願っています=)

于 2012-09-30T02:32:07.283 に答える