Last active
December 24, 2019 03:08
-
-
Save Hayao0819/de335ac6795218e265051b69e24283a4 to your computer and use it in GitHub Desktop.
指定したディレクトリ内のテキストファイルをcatで開き、検索語句があればそのファイルのパスを表示するスクリプト search.sh [ディレクトリ] [検索語句]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if [[ ! -d $1 || -z $1 ]]; then | |
echo "ディレクトリを指定してください。" | |
exit 1 | |
fi | |
if [[ -z $2 ]]; then | |
echo "検索する語句がありません。" | |
exit 1 | |
fi | |
dir=$1 | |
word=$2 | |
dir_list=( $(find ${dir} -type f 2> /dev/null | perl -nle 'print if -f && -T') ) | |
for file in ${dir_list[@]}; do | |
if [[ -n $(cat $file 2> /dev/null | grep $word) ]]; then | |
echo $file | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment