linux查找包装grep脚本

来源:互联网 发布:java 动态方法 编辑:程序博客网 时间:2024/05/01 15:55
#########################################fu xxx c########################################!/bin/bashmypath=$MYPATHcurrent_pid=$$if [ ! -d "${mypath}" ]thenmkdir "${mypath}"fiparent_id=$(ps -o pid,ppid,comm | grep $current_pid | awk -v current_pid_var=$current_pid '$1==current_pid_var{print $2}')if [ ! -f "$mypath/$parent_id" ]thentouch "$mypath/$parent_id"fifiletype=""if [[ $# -ge 2 && "$2" != "-" ]];thenfiletype="*.$2"fifilepath="*"path1="."if [ $# -ge 3 ];thenfilepath="$3/*"path1="$3"fiif [ "$filetype" = "" ];thengrep -rn -E "$1" $filepath|\ awk 'BEGIN{FS=":";count=1}{printf("[%d]: %s:%d:",count,$1, $2); for (i=3; i<=NF;i++) printf("%s",$i) print "";count+=1;}' | tee "$mypath/$parent_id" else find $path1 -name "$filetype" -print | xargs grep -nE "$1" \ | awk 'BEGIN{FS=":";count=1}{printf("[%d]: %s:%d:",count,$1, $2); for (i=3; i<=NF;i++) printf("%s",$i) print "";count+=1;}' | tee "$mypath/$parent_id" fi cnt=$(awk 'END{print FNR}' "$mypath/$parent_id") if [[ $cnt -eq 0 ]] then echo "******** / ^_^ \ |__| / ^_^ \\ ********" exit -1 fi num="" echo "请输入正确的数字" read num if [[ $num == "" || $num == "\n" ]] then exit -1 fi testnum=$(echo "$num" | grep [^0-9]) if [ "$testnum" == "" ] then fn "$num" "$parent_id" else echo "请输入正确的数字"#read num#testnum=$(echo "$num" | grep [^0-9])# if [ "$testnum" == "" ]# then# fn "$num" "$parent_id"# else# echo "请输入正确的数字,不给你机会了!"# exit -1# fi fi#########################################fn xxx#######################################mypath=$MYPATHline=""if [ $# -ge 1 ]thenline=$1elseline=1ficurrent_pid=$$parent_id="-1"if [ $# -ge 2 ] #第二个参数传递指定文件号thenparent_id=$2elseparent_id=$(ps -o pid,ppid,comm | \ grep $current_pid | \ awk -v current_pid_var=$current_pid '$1==current_pid_var{print $2}')fiif [ ! -f "$mypath/$parent_id" ]thenecho "请先用fu命令"exit -1fiif [ $line -eq 0 ]thenline=$(awk 'END{print FNR}' "$mypath/$parent_id")echo $linefiresult=$(awk -v li=$line 'BEGIN{FS=":"; count=0} NR==li{print $2 " " $3}' "$mypath/$parent_id")p=""l=""count=0for i in $result;do[ $count -eq 0 ] && p=$i;[ $count -eq 1 ] && l=$i;count=$((count + 1)) done; echo "$p":"$1" view "+$l" "$p"
0 0
原创粉丝点击