for循环在bash中不起作用


7

我有下面的代码来替换多个文件中的某些strigns,但是for循环正在检查第一个文件,而不执行perl脚本。下面是我的代码

if [ -f zebu.work.post_opt/ZEBU_CTO_FT_MOD.v ]
then
    for file in $(./zebu.work.post_opt/ZEBU_CTO_FT_MOD*);
    do
    perl -i -p -e 's/input/inout/g' $file; 
        perl -i -p -e 's/output/inout/g' $file;
        perl -i -p -e 's/wire.*\n/tran\(i0,\ o\);/g' $file;
        perl -i -p -e 's/assign.*\n//g' $file;
    done
fi

2
删除$(...)文件名周围的环绕样式。您不需要那里的命令替换。您也可以将四个Perl调用合并为一个,以提高速度。
库萨兰达

Answers:


9

$(foo)构造将运行命令foo并替换$(foo)为running的输出foo。您想要一个全局,这不是命令。您正在尝试运行名为的所有文件./zebu.work.post_opt/ZEBU_CTO_FT_MOD*。所有你需要的是:

if [ -f zebu.work.post_opt/ZEBU_CTO_FT_MOD.v ]
then
    for file in ./zebu.work.post_opt/ZEBU_CTO_FT_MOD*;
    do
        perl -i -p -e 's/input/inout/g' "$file"
        perl -i -p -e 's/output/inout/g' "$file"
        perl -i -p -e 's/wire.*\n/tran\(i0,\ o\);/g' "$file"
        perl -i -p -e 's/assign.*\n//g' "$file"
    done
fi

或者,更简单地说:

if [ -f zebu.work.post_opt/ZEBU_CTO_FT_MOD.v ]
then
    for file in ./zebu.work.post_opt/ZEBU_CTO_FT_MOD*;
    do
        perl -i -p -e 's/input/inout/g; s/output/inout/g; 
                       s/wire.*\n/tran\(i0,\ o\);/g; 
                       s/assign.*\n//g' "$file"
    done
fi

或更简单地说:

if [ -f zebu.work.post_opt/ZEBU_CTO_FT_MOD.v ]
then
    perl -i -p -e 's/input/inout/g; s/output/inout/g; 
                   s/wire.*\n/tran\(i0,\ o\);/g; 
                   s/assign.*\n//g' ./zebu.work.post_opt/ZEBU_CTO_FT_MOD*
fi

3
是否需要循环?可以perl -ip一次处理多个文件吗?我从未测试过泰铢。
库萨兰达

@Kusalananda确实可以。应该想到的,谢谢!
terdon
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.