我想添加第三个参数,它将文件名从上到下或从下到上更改,但是在这第三个参数中,我想指定必须更改哪个文件名?这个脚本怎么了?预先谢谢你。
#!/bin/bash
if test "$1" = "lower" && test "$2" = "upper"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
fi
done
elif test "$1" = "upper" && test "$2" = "lower"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:upper:] [:lower:])";
fi
done
fi
if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ];
then
for file in * ; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi
答案 0 :(得分:0)
如果我猜对了你想要的东西,请尝试
#!/bin/bash
case $1:$2 in
upper:lower | lower:upper ) ;;
*) echo "Syntax: $0 upper|lower lower|upper files ..." >&2; exit 1;;
esac
from=$1
to=$2
shift; shift
for file; do
mv "$file" "$(echo "$file" | tr "[:$from:]" "[:$to:]")"
done
这具有明显的优势,它允许三个以上的参数,其中前两个指定要执行的操作。
还请注意我们如何始终对包含文件名的字符串加引号。另请参见When to wrap quotes around a shell variable?
以上脚本实际上也应与/bin/sh
一起使用;我们不使用任何仅Bash功能,因此它应在任何POSIX sh
下运行。
但是,更好的设计可能是使用一个选项来确定要应用的映射,并简单地接受(可能为空)选项列表和文件名参数列表。然后,您也可以使用内置的Bash parameter expansion。但是,大小写转换参数扩展操作仅在Bash 4中可用。
#!/bin/bash
op=',,'
# XXX FIXME: do proper option parsing
case $1 in -u) op='^^'; shift;; esac
for file; do
eval mv "\$file" "\${file$op}"
done
默认情况下,这会转换为小写,如果在文件名前输入-u
,则会切换为大写。
在这两个脚本中,我们都使用for file
作为for file in "$@"
的简写,即,我们遍历(剩余的)命令行参数。也许这就是您想要的细节。
答案 1 :(得分:0)
如果 I 严重误解,请原谅我,但我认为您可能误解了参数传递的工作原理。
named / numbered参数表示您在命令行中按顺序位置传递的值。从理论上讲,每个值都可以具有可卡在字符串中的任何值。您不需要第三个参数,只需第三个值。
让我们尝试一个示例。
#! /bin/env bash
me=${0#*/} # strip the path
use="
$me { upper | lower } file
changes the NAME of the file given to be all upper or all lower case.
"
# check for correct arguments
case $# in
2) : exactly 2 arguments passed - this is correct ;;
*) echo "Incorrect usage - $me requires exactly 2 arguments $use" >&2
exit 1 ;;
esac
declare -l lower action # these variables will downcase anything put in them
declare -u upper # this one will upcase anything in it
declare newname # create a target variable with unspecified case
action="$1" # stored the *lowercased* 1st argument passed as $action
case $action in # passed argument has been lowercased for simpler checking
upper) upper="$2" # store *uppercased* 2nd arg.
newname="$upper" # newname is now uppercase.
;;
lower) lower="$2" # store *lowercased* 2nd arg.
newname="$lower" # newname is now lowercase.
;;
*) echo "Incorrect usage - $me requires 2nd arg to be 'upper' or 'lower' $use" >&2
exit 1 ;;
esac
if [[ -e "$2" ]] # confirm the argument exists
then echo "Renaming $2 -> $newname:"
ls -l "$2"
echo " -> "
mv "$2" "$newname" # rename the file
ls -l "$newname"
else echo "'$2' does not exist. $use" >&2
exit 1
fi
答案 2 :(得分:-1)
首先,此脚本存在缩进问题,首先检查条件是否应该在fi之前出现
下面是正确的。
if test "$1" = "lower" && test "$2" = "upper"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi
第二个您问的问题:
#/bin/bash -xe
[ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1
if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f "$3" ];
then
mv "$3" "$(echo $3 | tr [:lower:] [:upper:])";
fi
希望这会有所帮助。