findline

#!/bin/bash

######################################################
# file name: findline                                #
# find lines in files, recursively in current folder #
######################################################

ME=`basename $0`

showHelp()
{
  echo "usage $ME -h"
  echo "usage $ME [-i] [-s] [-n] [-c] -e regex_pattern [file_suffix1] [file_suffix2] [...]"
  echo "usage $ME [-i] [-s] [-n] [-c] text_pattern [file_suffix1] [file_suffix2] [...]"
  echo "usage $ME [-l] [-s] [-i] [-c] -e regex_pattern | text_pattern [file_suffix1] [file_suffix2] [...]"
  echo "usage $ME [-l] [-s] [-i] [-c] text_pattern [file_suffix1] [file_suffix2] [...]"
  echo "-h: show help"
  echo "-i: ignore case"
  echo "-s: silence mode, do not show command"
  echo "-n: show line number"
  echo "-c: exclude comment line, which is start with #"
  echo "-e: regex pattern"
  echo "-l: list file name only, will use -n by default"
  echo "The file suffix is case insensitive"
}

INVALID=1
IGNORE_CASE=1
REGEX=1
SILENCE=1
NUMBER=1
COMMENT=1
LIST=1

# check arguments
while getopts :ilhscne: OPTION
do
  case $OPTION in
    i) IGNORE_CASE=0
       ;;
    s) SILENCE=0
       ;;
    e) REGEX=0
       PATTERN=$OPTARG
       ;;
    n) NUMBER=0
       ;;
    c) COMMENT=0
       ;;
    l) LIST=0
       ;;
    h) showHelp
       exit 0
       ;;  
    *) INVALID=0
       ;;
  esac
done

# user input invalid arguments
[ $INVALID -eq 0 -o $# -eq 0 ] && showHelp && exit 1

#echo $OPTIND

# shift to pattern arg position
shift `expr $OPTIND - 1`
 
if [ $REGEX -ne 0 ]
then
  PATTERN=$1
fi

# no pattern arg was assigned
[ -z "$PATTERN" ] && showHelp && exit 1

# if no -e was specified, use next args as suffixes
[ $REGEX -ne 0 ] && shift
SUFFIX=$*

# set exec args for options
[ $IGNORE_CASE -eq 0 ] && IGNORE_CASE_ARG="-i"
[ $REGEX -eq 0 ] && REGEX_ARG="-E \"$PATTERN\"" || REGEX_ARG=\"$PATTERN\"
[ $NUMBER -eq 0 ] && NUMBER_ARG=" | nl"
[ $COMMENT -eq 0 ] && COMMENT_ARG=" | grep -E -v '^.+:[0-9]+:\s*#'"

if [ -n "$SUFFIX" ]
then
  for VAR in $SUFFIX
  do
    S=$S"|"$VAR
  done
 
  SUFFIX_PATTERN=`echo $S | sed 's/|//'`
  SUFFIX_ARG=" | grep -i -E \"\.($SUFFIX_PATTERN‘)\$\""
fi

RET=0

# find command
if [ $LIST -ne 0 ]
then
  # show find lines
  CMD="find . -print0 -type f -follow -name \"*\" 2>/dev/null | xargs -0 ls -d | awk '{ORS=\"\";print\"'\\''\";print\$0;print\"'\\''\";print\"\\n\"}' $SUFFIX_ARG | xargs grep -n -H $IGNORE_CASE_ARG $REGEX_ARG $COMMENT_ARG $NUMBER_ARG | sort"
else
  # show file list
  CMD="find . -print0 -type f -follow -name \"*\" 2>/dev/null | xargs -0 ls -d | awk '{ORS=\"\";print\"'\\''\";print\$0;print\"'\\''\";print\"\\n\"}' $SUFFIX_ARG | xargs grep -n -H $IGNORE_CASE_ARG $REGEX_ARG $COMMENT_ARG | grep ":" | cut -d: -f1 | sort -u"
fi

# display find command
[ $SILENCE -eq 0 ] || echo $CMD

if [ $LIST -ne 0 ]
then
  # execute show find lines
  eval $CMD
else
  # execute show file list
  FILES=(`eval "$CMD"`)
  INDEX=0
  while [ $INDEX -lt ${#FILES[*]} ]
  do
    NO=`expr $INDEX + 1`
    echo "$NO.   ${FILES[$INDEX]}"
    eval "_$NO=${FILES[$INDEX]}"
    INDEX=$NO
  done
 
  if [ ${#FILES[*]} -gt 0 ]
  then
    # execute command
    echo -e "Input command('\$_n' as file name):\c"
    read TMP
    
    if [ -n "$TMP" ]
    then
      COMMAND="$TMP 2>/dev/null"
      eval $COMMAND
    fi
  fi
fi

RET=$?

exit $RET


#### Running command: "make -j16 -l16" in "/home/wangyize/catkin_racecar/build" #### [ 0%] Built target std_msgs_generate_messages_lisp [ 0%] Built target std_msgs_generate_messages_eus [ 5%] Built target serial [ 5%] Built target std_msgs_generate_messages_py [ 5%] Built target std_msgs_generate_messages_nodejs [ 5%] Built target std_msgs_generate_messages_cpp [ 7%] Built target ackermann_cmd_mux_gencfg make[2]: *** 没有规则可制作目标“/usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.4.2.0”,由“/home/wangyize/catkin_racecar/devel/lib/racecar_gazebo/findLine” 需求。 停止。 [ 7%] Built target nav_msgs_generate_messages_py make[1]: *** [CMakeFiles/Makefile2:3572:racecar/racecar_gazebo/CMakeFiles/findLine.dir/all] 错误 2 make[1]: *** 正在等待未完成的任务.... [ 8%] Built target hokuyo_node_gencfg [ 11%] Built target joy_node [ 14%] Built target libhokuyo [ 14%] Built target rosgraph_msgs_generate_messages_lisp [ 14%] Built target nodelet_generate_messages_eus [ 14%] Built target roscpp_generate_messages_lisp [ 14%] Built target nodelet_generate_messages_lisp [ 14%] Built target rosgraph_msgs_generate_messages_eus [ 14%] Built target rosgraph_msgs_generate_messages_py [ 14%] Built target _vesc_msgs_generate_messages_check_deps_VescState [ 14%] Built target _ackermann_msgs_generate_messages_check_deps_AckermannDriveStamped [ 14%] Built target _ackermann_msgs_generate_messages_check_deps_AckermannDrive [ 14%] Built target _vesc_msgs_generate_messages_check_deps_VescStateStamped make: *** [Makefile:141:all] 错误 2 Invoking "make -j16 -l16" failed
最新发布
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值