腳本名稱:ffind
#!/bin/sh
echo_x()
{
if [ "${1}" = "info" ]; then
echo -e "\033[37m${2}\033[0m";
elif [ "${1}" = "notice" ];then
echo -e "\033[36m${2}\033[0m";
elif [ "${1}" = "warn" ];then
echo -e "\033[33m${2}\033[0m";
elif [ "${1}" = "error" ]; then
echo -e "\033[31m${2}\033[0m";
fi
}
core()
{
file_var="${1}"
find_str="${2}"
if [ ! -f "${file_var}" ]; then
continue
fi
if [ `file -i "${file_var}" | grep "charset=binary" | wc -l` != 0 ]; then
continue
fi
target_num=`cat "${file_var}" | grep "${find_str}" | wc -l`
if [ "${target_num}" == 0 ]; then
continue
fi
echo_x "error" "查找到${target_num}處: ${file_var}"
line_num=0
cat "${file_var}" | while read line; do
line_num=$((${line_num} + 1))
if [[ "${line}" == *"${find_str}"* ]]; then
echo_x "warn" "第${line_num}行: ${line}"
fi
done
echo_x "info" "---------------------------------------------"
}
use_help()
{
echo_x "error" "ffind ${*} 參數錯誤"
echo_x "notice" "Model 1: find /home | ./ffind \"target str\""
echo_x "notice" "Model 2: ./ffind \"target dir\" \"target str\""
}
if [ $
target_dir="${1}"
find_str="${2}"
if [ ! -d "${target_dir}" ]; then
use_help
exit 0
fi
find "${target_dir}" -type f | while read file_var; do
core "${file_var}" "${find_str}"
done
elif [ $
var_stdin=`cat /dev/stdin`
if [ "${#var_stdin}" != 0 ]; then
find_str="${1}"
for file_var in ${var_stdin}; do
core "${file_var}" "${find_str}"
done
fi
else
use_help ${*}
exit 0
fi
使用示範1:
[root@localhost logs]# ./ffind /home/wnh/filesync/ "const string&"
查找到1處: /home/wnh/filesync/src/include/wnh_system_operation/wnh_system_operation_file_operation.cpp
第6行: //實現rm_dir(const string& path)函數刪除目錄中的所有文件,在rm_dir()中遍歷每一個文件,如果遇到目錄文件,則遞歸刪除該目錄文件。
---------------------------------------------
查找到16處: /home/wnh/filesync/src/include/wnh_openssl/wnh_openssl_string_hash.cpp
第4行: unsigned int wnh_openssl::ngx_hash_key(const string& data)//nginx中使用的simpleHash算法
第113行: int wnh_openssl::additiveHash(const string& key, int prime) //加法hash
第124行: int wnh_openssl::rotatingHash(const string& key, int prime) //旋轉hash
第135行: int wnh_openssl::oneByOneHash(const string& key) //一次一個hash
第151行: int wnh_openssl::bernstein(const string& key) //Bernstein's hash
第163行: unsigned int wnh_openssl::RSHash(const string& str) //RS算法hash
第179行: int wnh_openssl::JSHash(const string& str) //JS算法
第192行: int wnh_openssl::PJWHash(const string& str) //PJW算法
第215行: int wnh_openssl::ELFHash(const string& str) //ELF算法
第234行: uint32_t wnh_openssl::BKDRHash32(const string& str)
第247行: uint64_t wnh_openssl::BKDRHash64(const string& str)
第260行: int wnh_openssl::SDBMHash(const string& str) //SDBM算法
第273行: unsigned int wnh_openssl::DJBHash(const string& str) //DJB算法
第286行: int wnh_openssl::DEKHash(const string& str) //DEK算法
第299行: int wnh_openssl::APHash(const string& str) //AP算法
第313行: int wnh_openssl::java(const string& str) //JAVA自己帶的算法
---------------------------------------------
使用示範2(管道):
[root@localhost logs]# find /home/wnh/filesync/ | grep -E '.h$|.cpp$' | ./ffind "const string&"
查找到1處: /home/wnh/filesync/src/include/wnh_system_operation/wnh_system_operation_file_operation.cpp
第6行: //實現rm_dir(const string& path)函數刪除目錄中的所有文件,在rm_dir()中遍歷每一個文件,如果遇到目錄文件,則遞歸刪除該目錄文件。
---------------------------------------------
查找到16處: /home/wnh/filesync/src/include/wnh_openssl/wnh_openssl_string_hash.cpp
第4行: unsigned int wnh_openssl::ngx_hash_key(const string& data)//nginx中使用的simpleHash算法
第113行: int wnh_openssl::additiveHash(const string& key, int prime) //加法hash
第124行: int wnh_openssl::rotatingHash(const string& key, int prime) //旋轉hash
第135行: int wnh_openssl::oneByOneHash(const string& key) //一次一個hash
第151行: int wnh_openssl::bernstein(const string& key) //Bernstein's hash
第163行: unsigned int wnh_openssl::RSHash(const string& str) //RS算法hash
第179行: int wnh_openssl::JSHash(const string& str) //JS算法
第192行: int wnh_openssl::PJWHash(const string& str) //PJW算法
第215行: int wnh_openssl::ELFHash(const string& str) //ELF算法
第234行: uint32_t wnh_openssl::BKDRHash32(const string& str)
第247行: uint64_t wnh_openssl::BKDRHash64(const string& str)
第260行: int wnh_openssl::SDBMHash(const string& str) //SDBM算法
第273行: unsigned int wnh_openssl::DJBHash(const string& str) //DJB算法
第286行: int wnh_openssl::DEKHash(const string& str) //DEK算法
第299行: int wnh_openssl::APHash(const string& str) //AP算法
第313行: int wnh_openssl::java(const string& str) //JAVA自己帶的算法
---------------------------------------------