Linux 脚本; For循环重命名;脚本新手

标签 linux scripting ksh

请原谅我,我编写 Linux 脚本的经验很少。好的,我想做的是重命名具有指定名称的文件的一部分,但我遇到的问题是我在 For 循环期间收到的错误是 0403-011 指定的替换对此命令无效 我不确定我在 for 循环中做错了什么,有人可以帮忙吗?

    #Creates Directory
    echo "Name of New Directory"
    read newdir
    if [[ -n "$newdir" ]]
    then
      mkdir $newdir
    fi
    echo $userInput Directory Created
    echo 
    echo "Directory you wish to Copy?"
    read copydir
    if [[ -n "$copydir" ]]
    then
    #Copies contents of Specified Directory
     cp -R $copydir/!(*.UNC) $newdir;

    #Searches through directory  
    for file in $newdir/$copydir*; do
    mv -v -- "$file" "${file/old/new}";
    done


fi

最佳答案

您使用的是哪个版本的 ksh?

"${file//old/new}""${file/old/new}"ksh93 中的有效语法。 .

如果你的环境是ksh88 不支持“${file//old/new}”替换。

您必须使用 sed/tr 来替换模式。这是 sed 的示例。

mv -v -- "$file""$(echo ${file}|sed 's/old/new/')"

关于Linux 脚本; For循环重命名;脚本新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52877523/

相关文章:

linux - 在脚本中使用 uname -n 与 VAR=$(uname -n) 的优点/缺点?

shell - 检查文件是否存在以及它是否包含特定字符串

linux - 在 Linux i686 上使用 sqlite-3.7.6.3 出现编译错误

linux - Linux 中的串口路由

linux - Postfix 邮件发送问题?

c# - 写一个C#脚本测试上百个域名

linux - egrep 打印只有一个 t 的行

ruby-on-rails - 适用于 Ruby 的 Linux 发行版

linux - Bash 脚本 : Diference between "$(command)" and `command`

linux - 如何将 ksh -x 的输出重定向到 unix 中的文件