Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

As the title suggests, this problem has enough layers of complexity to make simple escaping characters difficult for me. I have a bash script which, for a large part, has an embedded awk script that reads a file delimited by semicolons. In this text file, one of the fields is a directory path to some JPEGs. I'd like to copy the JPEGs somewhere else, but the original directory path has spaces in it (and could potentially have other strings that could do damage). I can't refer to the directory in question with single quotes because this stops the awk interpreter, and double quotes turns it into a literal string in awk.

I'm using gawk 4.1.1. Here's some awk code of what I'm trying to accomplish:

imageDirectory = $1;
cpJpegVariable="cp -r " imageDirectory " assets";
#screws up if imageDirectory has spaces and other naughty characters
system(cpJpegVariable);
share|improve this question

2 Answers 2

up vote 3 down vote accepted

You can use:

awk '...
    cpJpegVariable="cp -r '\''" imageDirectory "'\'' assets";
    ...'

(note that ' doesn't need escaping for awk, but you need '\'' for the shell). So when awk expands variable cpJpegVariable, it looks like:

cp -r 'file_contain space' assets

With that, you can avoid problem with all special characters, except ' itself. If imageDirectory may contain single quote characters, you can escape them with gsub(). Example:

awk '{gsub("'\''","'\''\"'\''\"",imageDirectory)}1'
share|improve this answer
    
Worked perfectly, and embarassingly obivous in hindsight. Thanks very much. –  Escher Aug 5 '14 at 8:34
1  
Note that while it fixes the problem with space, tab, ;, |, & and many other characters, it does not fix it for filenames that contain backtick, backslash, $ or " characters (or whose name starts with -). –  Stéphane Chazelas Aug 5 '14 at 8:46
    
@StéphaneChazelas: If change \" to \', problem maybe fixed? –  cuonglm Aug 5 '14 at 8:48
    
To ' you mean? Then, the only problematic character becomes ' itself (which you can escape with a gsub command). –  Stéphane Chazelas Aug 5 '14 at 9:08
    
@StéphaneChazelas: Thanks for suggestion, fixed! –  cuonglm Aug 5 '14 at 9:21

Executing external commands is meant for the shell, not for awk:

while read image_dir __; do
    cp -r "$image_dir" assets
done < file
share|improve this answer
    
It is sometimes necessary or useful to execute a system command based on information that was read by awk. This is why system() exists. –  Escher Aug 5 '14 at 10:30
    
system() is not consistent with all types of input. It's awfully difficult to sanitize it. –  konsolebox Aug 5 '14 at 10:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.