Naishu Babu
Naishu Babu

Reputation: 61

File with multiple lines read and append to single variable

I need to read a file with 3 lines

backuplist.txt

/etc/

/home

/boot

and append it to a single variable to compress it

#!/bin/bash

cat backuplist.txt |
{
while read -r line
do
echo $line
/bin/tar -zcf /backup/test.tar.gz $line
done
echo $line
}

While running the script the tar file is created only with /boot directory.

Anyway to read 3 lines to 3 different variables and compress it?

Upvotes: 0

Views: 157

Answers (2)

Wes Hardaker
Wes Hardaker

Reputation: 22252

You'll find you can do that much more easily with:

#!/bin/bash

/bin/tar -zcf /backup/test.tar.gz $(<cat /backuplist.txt)

As was, your script was over-writing test.tar.gz for each line in the file.

Upvotes: 1

Abhijit Pritam Dutta
Abhijit Pritam Dutta

Reputation: 5591

Also you can do it like:-

#!/bin/bash

file_list=$(cat /backuplist.txt)

/bin/tar -zcf /backup/test.tar.gz $file_list

Upvotes: 0

Related Questions