Skip to content

Instantly share code, notes, and snippets.

@Hayao0819
Last active December 24, 2019 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hayao0819/de335ac6795218e265051b69e24283a4 to your computer and use it in GitHub Desktop.
Save Hayao0819/de335ac6795218e265051b69e24283a4 to your computer and use it in GitHub Desktop.
指定したディレクトリ内のテキストファイルをcatで開き、検索語句があればそのファイルのパスを表示するスクリプト search.sh [ディレクトリ] [検索語句]
#!/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