rn.sh

来自「BASH Shell 编程 经典教程 《高级SHELL脚本编程》中文版」· Shell 代码 · 共 52 行

SH
52
字号
#! /bin/bash## Very simpleminded filename "rename" utility (based on "lowercase.sh").##  The "ren" utility, by Vladimir Lanin (lanin@csd2.nyu.edu),#+ does a much better job of this.ARGS=2E_BADARGS=65ONE=1                     # For getting singular/plural right (see below).if [ $# -ne "$ARGS" ]then  echo "Usage: `basename $0` old-pattern new-pattern"  # As in "rn gif jpg", which renames all gif files in working directory to jpg.  exit $E_BADARGSfinumber=0                  # Keeps track of how many files actually renamed.for filename in *$1*      #Traverse all matching files in directory.do   if [ -f "$filename" ]  # If finds match...   then     fname=`basename $filename`            # Strip off path.     n=`echo $fname | sed -e "s/$1/$2/"`   # Substitute new for old in filename.     mv $fname $n                          # Rename.     let "number += 1"   fidone   if [ "$number" -eq "$ONE" ]                # For correct grammar.then echo "$number file renamed."else  echo "$number files renamed."fi exit 0# Exercises:# ---------# What type of files will this not work on?# How can this be fixed?##  Rewrite this script to process all the files in a directory#+ containing spaces in their names, and to rename them,#+ substituting an underscore for each space.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?