目前我的计算机上还没有Linux,所以我想知道是否有人可以测试我写的代码。
当您输入类似这样的内容时,应该重命名文件扩展名,以便将其运行到终端中:
chaxxx zzz yyy * .zzz
“chaxxx”是该文件的名称。
这是我写的代码:
>>deleted<<
答案 0 :(得分:5)
使用online compiler & interpreter进行测试。 ideone也支持Bash脚本。
编辑:
确实有效。 ren.sh 是您的脚本名称,在这里:
$ ls
asdf.doc ren.sh text.txt
$ ./ren.sh txt doc *.txt
text.txt
text
$ ls
asdf.doc ren.sh text.doc
答案 1 :(得分:2)
你看过rename
命令了吗?你在这里重新发明轮子。
man rename
重命名.htm .html * .htm
will fix the extension of your html files.
如果您打算在bash
中自行完成,那么我建议您使用以下代码。以下是它的好处:
mv
之前更改
它。basename
test
shift
醇>
#!/bin/bash
if (( $# < 3 )); then
echo "Usage: $0 oldExt newExt files"
exit
fi
EXTf=$1
EXTt=$2
for (( i = 3; i <= $#; i++)); do
NAME=${!i}
if [[ "${NAME##*.}" == "$EXTf" ]]; then
mv "$NAME" "${NAME%.*}.$EXTt"
fi
done