Ruchir Bharadwaj
Ruchir Bharadwaj

Reputation: 1272

Getting issue in substituting multiple files from sed

I want to use sed for replacing multiple files from bash script.

When I call it from bash I get below error

DEBUG FLOW:-

FILELIST='/tmp/components/ab.sql /tmp/b.sql'
+ SUBSTITUTE_STRING=abc
+ sed -i.bak -e s/abc/xyz/g '/tmp/components/ab.sql /tmp/b.sql': No such file or directory

however when I used this command directly on terminal it executes successfully

sed -i.bak -e s/abc/xyz/g /tmp/components/ab.sql /tmp/b.sql

The difference from terminal and script is of quotes around the file.

I have tried defining File list variable without quotes as well

kindly suggest

Upvotes: 0

Views: 44

Answers (1)

devnull
devnull

Reputation: 123608

Instead of saying:

FILELIST='/tmp/components/ab.sql /tmp/b.sql'

make it an array by saying:

FILELIST=(/tmp/components/ab.sql /tmp/b.sql)

and while invoking say:

sed -i.bak -e "s/abc/xyz/g" "${FILELIST[@]}"

If you look at the debug flow, it'd be evident that the shell parses the filenames as a single token ('/tmp/components/ab.sql /tmp/b.sql') which causes the No such file or directory error.

Upvotes: 1

Related Questions