代码之家  ›  专栏  ›  技术社区  ›  Josh Adams

Linux脚本;For循环重命名;不熟悉脚本

  •  -1
  • Josh Adams  · 技术社区  · 8 年前

    0403-011指定的替换对此命令无效

        #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
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   nixtalker    8 年前

    "${file//old/new}" 和 "${file/old/new}" ksh93 .

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

    必须使用sed/tr替换模式。下面是sed的一个例子。

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

        2
  •  0
  •   wizzwizz4    8 年前

    冒犯的一句话:

    mv -v -- "$file" "${file/old/new}";
    

    mv -v -- "$file" "${file//old/new}";
    

    如果你想替换 $old 具有 $new "old" 具有 "new" ),写入:

    mv -v -- "$file" "${file//$old/$new}";