【问题标题】:How do I call rename successfully from a bash script on Ubuntu?如何从 Ubuntu 上的 bash 脚本成功调用重命名?
【发布时间】:2016-07-08 04:10:31
【问题描述】:

我有一个 bash 脚本 #!/usr/bin/env bash,它被称为 make 过程的一部分。该脚本创建一个目录,其中包含与实现相关的文件,然后tars 将它们保存起来。我想复制该目录并重命名一些文件以将版本标识符替换为“最新”一词。这将使从网络服务器获取最新文件的脚本变得简单。当我运行我的脚本时,对rename 的调用似乎什么也没做,这是为什么呢?

#!/usr/bin/env bash

DATE_NOW="$(date +'%Y%m%d')"
product_id_base="$1"

firmware_dir="${product_id_base}-full-${DATE_NOW}"

# ...rest of file ommitted to protest the innocent
# It creates and fills the ${firmware_dir} with some files that end in 
# -$DATE_NOW.<extention> and I would like to rename the copies of them so that they end in
# -latest.<extention>
cp -a "./${firmware_dir}" "./${product_id_base}-full-latest"

# see what there is in pwd
cd "./${product_id_base}-full-latest"
list_output=`ls`
echo $list_output
# Things go OK until this point. 

replacment="'s/${DATE_NOW}/latest/'"
rename_path=$(which rename)
echo $replacment
perl $rename_path -v $replacment *
echo $cmd
pwd

$cmd

echo "'s/-${DATE_NOW}/-latest/g'" "${product_id_base}-*"
echo $a

# check what has happened
list_output=`ls`
echo $list_output

我用./rename.sh product-id 调用上面的内容,并从ls 获得预期的输出,这表明当前的工作目录是我想要重命名的文件。

$ ./rename.sh product-id ET-PIC-v1.1.dat ET-PIC-v1.1.hex
product-id-20160321.bin product-id-20160321.dat
product-id-20160321.elf product-id-20160321.gz 's/20160321/latest/'

/home/thomasthorne/work/product-id/build/product-id-full-latest
's/-20160321/-latest/g' product-id-*

ET-PIC-v1.1.dat ET-PIC-v1.1.hex product-id-20160321.bin
product-id-20160321.dat product-id-20160321.elf product-id-20160321.gz

我想看到的是一些重命名的文件。当我从终端模拟器直接调用重命名函数时,我看到重命名发生了。

~/work/product-id/build/product-id-full-latest$ rename -vn
's/-20160321/-latest/g' * product-id-20160321.bin renamed as
product-id-latest.bin product-id-20160321.dat renamed as
product-id-latest.dat product-id-20160321.elf renamed as
product-id-latest.elf ...

我尝试了一些转义字符串的变体,使用 ` 或 $(),从命令行中删除所有替换。到目前为止,没有任何工作,所以我一定错过了一些基本的东西。

我读到#!/usr/bin/env bash 的行为很像#!/bin/bash,所以我不认为这是在起作用。我知道 Ubuntu 和 Debian 对其他一些发行版有不同版本的重命名脚本,我在 Ubuntu 上运行。这导致我尝试调用 perl /usr/bin/rename ... 而不仅仅是重命名,但这似乎没有明显的区别。

【问题讨论】:

    标签: bash rename file-rename batch-rename file-manipulation


    【解决方案1】:

    这个字符串:

    replacment="'s/${DATE_NOW}/latest/'"
    

    将保持完全相同,因为您将其放在单引号之间。

    您是否尝试过:

    replacment="s/${DATE_NOW}/latest/"
    

    这个在我的 Ubuntu 上工作,没有 perl:

    $ ./test_script
    filename_20160321 renamed as filename_latest
    filename2_20160321 renamed as filename2_latest
    filename3_20160321 renamed as filename3_latest
    

    test_script 内容为:

    #!/bin/bash
    
    DATE_NOW="$(date +'%Y%m%d')"
    
    replacment="s/${DATE_NOW}/latest/"
    
    rename -v $replacment *
    

    【讨论】:

    • 我确信我在过去一个小时所做的许多排列中尝试过,但它失败了。我刚才试过了,它有效!即使当我回到我开始的琐碎rename -v "s/${DATE_NOW}/latest/" * 时,现在看起来也不错。谢谢你的解决方案。我不知道过去 2 小时我在做什么。
    • 很高兴能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 2011-05-06
    • 2016-12-15
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2014-10-08
    • 2020-10-24
    相关资源
    最近更新 更多