正規表現を使用して、特定のディレクトリに特定の単語で始まるフォルダがあることを確認してください。

正規表現を使用して、特定のディレクトリに特定の単語で始まるフォルダがあることを確認してください。

ディレクトリに特定の単語で始まるサブディレクトリがあることを確認するスクリプトを作成しています。

これはこれまで私のスクリプトです。

#!/bin/bash

function checkDirectory() {
    themeDirectory="/usr/share/themes"
    iconDirectory="/usr/share/icons"
    # I don't know what to put for the regex. 
    regex=

    if [ -d "$themeDirectory/$regex" && -d "$iconDirectory/$regex" ]; then
       echo "Directories exist."
    else
       echo "Directories don't exist."
    fi
}

それでは、regex特定のディレクトリに特定の単語で始まるフォルダがあるかどうかを確認しますか?

ベストアンサー1

-d正規表現を許可せず、ファイル名を受け入れます。単純なプレフィックスのみを確認するには、ワイルドカードで十分です。

exists=0
shopt -s nullglob
for file in "$themeDirectory"/word* "$iconDirectory"/* ; do
    if [[ -d $file ]] ; then
        exists=1
        break
    fi
done
if ((exists)) ; then
    echo Directory exists.
else
    echo "Directories don't exist."
fi

nullglob一致するものがない場合、ワイルドカードは空のリストに展開されます。大きなスクリプトでは、サブシェルの値を変更するか、必要でない場合は以前の値にリセットします。

おすすめ記事