8

i have the below code to replace some strigns in multiple files, but the for loop is checking for the first file and not executing the perl script. below is my code

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
1
  • 2
    Remove the $(...) around your filename globbing pattern. You don't need a command substitution there. You may also combine the four Perl invocations to a single one for speed. Commented Aug 22, 2019 at 15:43

1 Answer 1

10

The $(foo) construct will run the command foo and replace $(foo) with the output of running foo. You want a glob, that's not a command. What you're doing is attempting to run all files called ./zebu.work.post_opt/ZEBU_CTO_FT_MOD*. All you need is:

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

Or, more simply:

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

Or even more simply:

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
2
  • 3
    Is the loop even needed? Can perl -ip work on several files at once? I've never tested taht. Commented Aug 22, 2019 at 15:45
  • @Kusalananda indeed it can. Should have thought of that, thanks! Commented Aug 22, 2019 at 15:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.