UNIX如何使我的脚本删除多个文件和通配符?

时间:2019-05-24 13:27:57

标签: bash unix

我的任务是制作一个模仿rm命令的删除脚本。如您所知,如果要键入rm file1 file2之类的命令,rm命令将删除所有文件。使用此示例,我的脚本将只删除file2。谁能帮助我,使我的删除脚本删除列出的所有文件?我的脚本在下面。抱歉,我有点不熟悉编码。

    #!/bin/bash

       function directory(){
       #Checks if deleted directory & .restore.info file exists
       #If they don't exist, it creates them
       if [ ! -d ~/deleted ]
           then
               mkdir ~/deleted
       fi

      if [ ! -f ~/.restore.info ]
          then
              touch ~/.restore.info
      fi
      }

      function movefile(){
      #not currently using
      mv "$1" ~/deleted/$1
      echo "file moved to recycle bin"
      }

      function error_conditions(){
      #not currently using
      #Prints error messages and checks if file is in project directory
      if [ ! -f ~/project ]
          then
              echo "cannot remove $filename: no such file or directory"
      elif [ -d ~/project ]
              then
                  echo "cannot remove $filename: is a directory"
      else
          echo "missing operand"
      fi
      }

      function delete_file(){
      #Gets inode for filename
      #Takes user input and puts file wherever based on user input
      inode=$(stat -c%i "$filename")
      pwd=$(readlink -e $filename)
      if "$interactive"
          then
              if [ "$verbose" = true ]; then
                  read -p "Are you sure you want to delete $filename? " user_input
                      if [ $user_input == "y" ] || [ $user_input == "Y" ] || [ $user_input == "yes" ] || [ $user_input == "Yes" ];
                          then
                      mv $filename ~/deleted/${filename}_$inode
                      #moves deleted file to deleted directory (with inode at end)
                      echo ${filename}_$inode:$pwd>>~/.restore.info
                      #stores info of removed file in .restore.info (with path)
                      echo "removed '$filename'"
                          else
                          echo "Nothing has been deleted"
                      fi
              else
                  read -p "Are you sure you want to delete $filename? " user_input
                      if [ $user_input == "y" ] || [ $user_input == "Y" ] || [ $user_input == "yes" ] || [ $user_input == "Yes"];
                      then
                      mv "$filename" ~/deleted/${filename}_$inode
                      echo ${filename}_$inode:$pwd>>~/.restore.info
                      else
                      echo "Aborted"
              fi
          fi
      elif "$verbose"
          then
              mv "$filename" ~/deleted/${filename}_$inode
              echo ${filename}_$inode:$inode:pwd>>~/.restore.info
              echo "removed '$filename'"
      else
          mv "$filename" ~/deleted/${filename}_$inode
          echo ${filename}_$inode:$pwd>>~/.restore.info
          echo "Executed"
  fi
  }
  #Setting all flags to false
  interactive=false
  verbose=false
  recursive=false
  while getopts :ivr optionvar
  do
      case "$optionvar" in
          i) interactive=true;;
          v) verbose=true;;
          r) recursive=true;;
      esac
  done
  shift $((OPTIND-1)) #process arguments.
  #doing error commands with help of recursive
  for i in $*
      do
  filename=$i
  basefile=$(basename $i)
  if [ "$filename" == " " ];
      then
          echo "No filename provcided"
      elif [ -d $filename ];
          then
              if [ ! $recursive = true ];
                 then
                     echo "Directory name provided, please provide a file"
             fi
     elif [ ! -f $filename ];
         then
             echo "File does not exist"
     #   elif [ "$basefile" == "safe_rm" ]
     #   then
     #       echo "Attempting to delete safe_rm"
 fi
 done
 #################################M A I N###############################
 directory
 delete_file $*
 #error_conditions $* #- this gives me duplicate output lines
 #movefile "$@" - this gives me an unnecessary "mv: cannot stat" output line

1 个答案:

答案 0 :(得分:0)

我不会对您的整个脚本进行详细的代码审查,但是这里有一些注意事项。

  • 您正在遍历脚本主要部分中的参数,但是随后您将使用多个参数调用delete函数。该函数没有循环。将循环从main()移至delete_files()(请注意,为清楚起见,我将其名称复数了。
  • 谈到main(),您还可以将该代码(选项处理,函数分派等)封装在具有该名称的函数中,然后在脚本底部有一行调用它: main "$@"
  • 除非您需要$*并了解其用法,否则不要使用"$@"-几乎总是使用[[ ]]并始终引用它(非常很少) / li>
  • 始终使用缩进
  • 如果您的脚本不需要移植到Bash之外的其他shell上,请使用特定于Bash的功能,例如[ ]而不是function f()
  • 您正在同时使用两种命名函数的方法(function)。使用一个或另一个-使用f () { ...; }优先使用括号-因此pwd=$(readlink -e "$filename")
  • 使用更多引号,例如:
    mv "$filename" ~/deleted/"${filename}_$inode"
    echo "${filename}_$inode:$pwd" >> ~/.restore.info
    ~
  • 但是我不建议在脚本中使用波浪号($HOME)-改为使用getent。而且,如果您需要查找用户的主目录,请使用JOIN而不是其他方法。