Home > Software design >  How to create a list from a text fille using SH?
How to create a list from a text fille using SH?

Time:10-01

I need to create a SH script that reads a file that contains a list of directories

dirA
dirB
dirC

And use this information to generate a command like this:

go test -coverprofile=coverage.out dirA dirB dirC

The package file is called .package-list and this is the script I have at the moment:

while read package;
do
  go test -coverprofile=coverage.out ./$package
done <.package-list

The problem is that that script executes the go test command three times:

go test -coverprofile=coverage.out ./dirA
go test -coverprofile=coverage.out ./dirB
go test -coverprofile=coverage.out ./dirC

What can I do to read the file and generate a command like I need it?

CodePudding user response:

If using bash instead of sh (You have both tagged, so it's not clear which shell you're targeting), you can read the lines of the file into an array:

readarray -t packages < .package-list
go test -coverprofile=coverage.out "${packages[@]}"

CodePudding user response:

Use xargs

xargs go test -coverprofile=coverage.out < .package-list
  • Related