搜索文件格式+选项

时间:2014-06-08 13:28:09

标签: bash shell find

上个月我正在用bash摆弄,我正在尝试创建一个脚本。

我希望脚本在文件夹中搜索具有由参数-e定义的某种扩展名的文件。文件夹是在没有-option的情况下定义的。输出是2列,其中第一列打印找到的文件,第二列是相应的文件夹。

这是最有效和/或最灵活的方式吗?

我也无法让-l命令工作。知道什么是错的吗?当我输入-name \${CHAR}*时,它根本不起作用。另外,如何识别正在使用的范围?使用if函数查找“ - ”字符或什么?

我认为我设法安装了一个块设备,但是如何将路径添加为参数以便它可以用作文件夹?将数字设置为var不起作用,它告诉我它无法识别该命令。

出于某种原因,'no recursion'标签有效,但'无数字'不起作用。我不知道为什么会有所不同。

使用“无递归”(nn)和“无数字”(nr)标记时,我使用长标记--tag作为参数。是否可以只使用1 -tag?这可以通过get opts实现,但是在使用get opts后我无法使用其他标签。有人解决了吗?

最后,当找到具有相同文件名的2个文件而不是两次打印文件时,它是否可能只显示文件一次。但是对于每个具有相同名称的文件都保留一个空格,所以它仍然可以显示第二列中的所有文件夹?

#!/bin/bash

#FUNCTIONS
#Error
#Also written to stderr
err() {
  echo 1>&2;
  echo "Error, not enough arguments" 1>&2;
  echo "Usage: $0 [-e <file extension>] [<folder>]";
  echo "Please enter the argument -e and at least 1 folder.";
  echo "More: Please chek Help by using -h or --help.";
  echo 1>&2;
  exit
 }

#Help
help() {
  echo
  echo "--- Help ---"
  echo
  echo "This script will look for file extentions in 1 or more directories. The output shows the found files with the according folder where it's located."
  echo
  echo "Argument -e <ext> is required."
  echo "Other arguments the to-look-trough folders."
  echo
  echo "These are also usable options:"
  echo "-h or --help shows this."
  echo "-l <character> looks for files starting with the character."
  echo "-l <character1>-<character2> does the same, but looks trough a range of characters."
  echo "-b <block-device> mounts a partition to /mnt and let it search through."
  echo "--nn (no numbers) makes sure there are no numbers in the file name."
  echo "--nr (no recursion) doesn't look trough subdirectories."
  echo "-r of –-err <file> writes the errors (f.e. corrupted directory) to <file>."
  echo "-s <word> searches the word through the files and only shows the files having that word."
  echo
  exit
}

#VARS

#execute getopt
OPTS=$(getopt -o e:hl:b:r:s: -l "help,nn,nr,err" -n "FileExtensionScript" -- "$@");

#Bad arguments
if [ $? -ne 0 ];
then
  err;
  exit
fi

#Rearrange arguments
eval set -- "$OPTS";

#echo "AFTER SET -- \$OPTS: $@";

while true; do
  case "$1" in
    -e)
      shift;
      if [ -n "$1" ]; then
        EXT=$1;
        shift;
      fi
      ;;
    -h|--help)
      shift;
      help;
      ;;
    -l)
      shift;
      if [ -n "$1" ]; then
        CHAR=$1;
        shift;
      fi
      ;;
    -b)
      shift;
      if [ -n "$1" ]; then
        sudo mkdir /mnt/$1;
        sudo echo -e "/dev/$1       /mnt/$1           vfat    defaults        0       0 " >> /etc/fstab;
        sudo mount -a;
        999=/mnt/$1;
        shift;
      fi
      ;;
    --nn)
      shift;
      NONUM=" ! -name '*[0-9]*'";
      ;;
    --nr)
      shift;
      NOREC="-maxdepth 1";
      ;;
    -f|--err)
      shift;
      if [ -n "$1" ]; then
        ERROR="| 2>filename | tee -a $1";
        shift;
      fi
      ;;
    -s)
      shift;
      if [ -n "$1" ]; then
        SEARCH="-name '*$1*'";
        shift;
      fi
      ;;
    --)
      shift;
      break;
      ;;
  esac
done

#No folder or arguments given
if [ $# -lt 1 ];
then
  err;
  exit
fi

#Debug
echo "Folder argumenten: $@" >2;
echo \# $# >2;

#Create arrays with found files and according folders
FILES=( $(find $@ $NOREC $SEARCH $NONUM -name \*.${EXT} $ERROR | rev | cut -d/ -f 1 | rev) )
FOLDERS=( $(find $@ $NOREC $SEARCH $NONUM -name \*.${EXT} $ERROR | rev | cut -d/ -f 1 --complement | rev) )
#Show arrays in 2 columns
for ((i = 0; i <= ${#FILES[@]}; i++));
do
    printf '%s %s\n' "${FILES[i]}" "${FOLDERS[i]}"
done | column -t | sort -k1 #Make columns cleaner + sort on filename

我不是以英语为母语的人,我希望得到一些提示来完成我的剧本:)提前致谢!

0 个答案:

没有答案