I'm having some trouble with a bash script I'm working on. Basically I have a file which contains several strings like:
path1 --to--> path2
I want to take all of the path2 parts of the string and run touch on that path. I have this line which will output it as a single line with quotes around each path and spaces between but I cannot get touch to work with this. Either it ignores the quotes or it treats the whole thing as a single path.
but I can't get the touch command to accept that as an argument. I can't get it to go in to an array either without getting split in to a different element each time there's a space.
So basically I just want to take all of the file paths from a text file and run touch on those files.
I am not familiar with awk, but I too had the same problem of piping file names containing spaces only to have them appear as multiple parameters to the next command, what I do is insert a sed in the pipeline that would escape all spaces like so: first_command | sed 's_ _\\ _g' | next command Hope this helps edit: comment encoding messed with the escaped '\' character
I was going to try something like that, what I ended up doing was adding an @ to the end of each line and then using IFS="@" to change the delimiter so that each line would get added to an element of an array. This was the first thing I tried and it worked so I've stuck with it, I could probably have used /n or whatever new line is, and I didn't try if I could use it directly with the touch command but instead put it in to an array and made a for loop.
like this
IFS="@" #Set delimiter to @ for array entry
TOUCH=($( grep -o " --to--> .*" $SORT_OUTPUT | cut -c10- | awk '{ print $0"@"}' | tr -d '\n' ))
for ((i = 0; i < ${#TOUCH[@]}; i++))
do
touch "${TOUCH[$i]}"
done
unset IFS #Return delimiter to default
But I think it would work with IFS="\n" and just TOUCH=($( grep -o " --to--> .*" $SORT_OUTPUT | cut -c10- )) but it works so I'm leaving it as is.
Hey man if it worked I didn't understand everything though...so the TOUCH=($( grep -o " --to--> .*" $SORT_OUTPUT | cut -c10- | awk '{ print $0"@"}' | tr -d '\n' )) adds a '@' character at the end of each path, but doesn't your loop only touch the first path ? I mean you stop at the first '@' character right ?
It loads each path in to an element of the array, the @ character tells it where to break, rather than using a space like usual. This way the whole path including spaces is loaded in to an element. Then the for loop runs through each element and runs touch on that element.
But it should already have a \n for new line separating each path which I could have used instead, so the whole @ thing is kind of redundant, it was just something I tried to test it and it worked so I left it alone