0

I have a CT.txt with below input data. CT.txt:

test testing test 1

And with below script:

#!/bin/bash
for CITY in $(cat /home/user/CT.txt)
do
 FILES=/mnt/dir1/dir2/$CITY/*
echo $FILES

Giving me output as follow:

/mnt/dir1/dir2/test
/mnt/dir1/dir2/testing
/mnt/dir1/dir2/test/*
/mnt/dir1/dir2/1/*

where output should be:

/mnt/dir1/dir2/test
/mnt/dir1/dir2/testing
/mnt/dir1/dir2/test 1

How to make CT.txt file to read having space in between test 1.

  • if your CT.txt content is linear you cannot even achieve it by quoting. Is your CT.txt content are in one line only? – αғsнιη Aug 03 '17 at 08:22

2 Answers2

0

If you change the FS (field seperator) in awk you could achieve it.

awk -F: '{ print "/mnt/dir1/dir2/"$1 }' CT.txt
Vafa
  • 312
  • The CT.txt there is no field seperator :! also all contents are in one line but you just reading one, even if it was space or tab, we don't know test 1 is filename or test and 1 are seperate files – αғsнιη Aug 03 '17 at 08:37
  • Yes, if all is in one line, it wouldn't work, but if each line is an entry, than we could achieve it with awk. – Vafa Aug 03 '17 at 10:38
0

Assuming the CT.txt has entries one / line, then the while loop is better suited to your task:

while IFS= read -r CITY; do
   FILES=/mnt/dir1/dir2/$CITY
   echo "$FILES"/*
done < /home/user/CT.txt

The for loop is ill-suited for this, however, it can still be made to work:

# set the input field separator to newline and disable globbing
IFS=$'\n';set -f
for CITY in $(< /home/user/CT.txt); do
   FILES=/mnt/dir1/dir2/$CITY
   echo "$FILES"/*
done