shell学习日志(七)

it2025-03-17  15

1.在当前目录下,对所有文件遍历并查找相应的词

search.sh

#!/usr/bin/env bash #SELFDIR=$(dirname $(realpath "$BASH_SOURCE")) #grep --exclude Defines.h Truthtable.h * SELFDIR=$(cd $(dirname "$BASH_SOURCE");pwd) if [[ ${SELFDIR} == "" ]]; then echo "Internal error: can not locate the script dir" exit 1 fi echo $SELFDIR for file in `ls $SELFDIR` do if ( grep $1 $file );then echo $file fi done

运行:bash search.sh whatIwantFind

#!/usr/bin/env bash #SELFDIR=$(dirname $(realpath "$BASH_SOURCE")) #grep --exclude Defines.h Truthtable.h * SELFDIR=$(cd $(dirname "$BASH_SOURCE");pwd) if [[ ${SELFDIR} == "" ]]; then echo "Internal error: can not locate the script dir" exit 1 fi echo $SELFDIR for file in `ls $SELFDIR` do if ( echo "$file:" && grep "delim" $file );then echo "" fi done

2. cd到子目录里找

#!/usr/bin/env bash #SELFDIR=$(dirname $(realpath "$BASH_SOURCE")) #grep --exclude Defines.h Truthtable.h * SELFDIR=$(cd $(dirname "$BASH_SOURCE");pwd) if [[ ${SELFDIR} == "" ]]; then echo "Internal error: can not locate the script dir" exit 1 fi echo $SELFDIR FDIR="$SELFDIR/atalanta/" cd $FDIR for file in `ls` do if ( echo "$file:" && grep "cube" "$file" );then echo "" fi done

上一篇:https://blog.csdn.net/wxkhturfun/article/details/108873997

最新回复(0)