How to escape spaces etc in passed variable, for system call to cp in awk
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);
shell-script awk quoting
add a comment |
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);
shell-script awk quoting
add a comment |
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);
shell-script awk quoting
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);
shell-script awk quoting
shell-script awk quoting
edited Dec 20 '18 at 0:15
Rui F Ribeiro
39k1479130
39k1479130
asked Aug 5 '14 at 8:11
Escher
283615
283615
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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'
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 agsub
command).
– Stéphane Chazelas
Aug 5 '14 at 9:08
@StéphaneChazelas: Thanks for suggestion, fixed!
– cuonglm
Aug 5 '14 at 9:21
|
show 6 more comments
If you are passing an Awk variable to system, you need to shell quote it:
function quote(str, d, m, x, y, z) {
d = "47"; m = split(str, x, d)
for (y in x) z = z d x[y] d (y < m ? "\" d : "")
return z
}
Example:
system(sprintf("cp -r %s assets", quote($1)))
Source
add a comment |
Executing external commands is meant for the shell, not for awk:
while read image_dir __; do
cp -r "$image_dir" assets
done < file
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
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f148454%2fhow-to-escape-spaces-etc-in-passed-variable-for-system-call-to-cp-in-awk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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'
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 agsub
command).
– Stéphane Chazelas
Aug 5 '14 at 9:08
@StéphaneChazelas: Thanks for suggestion, fixed!
– cuonglm
Aug 5 '14 at 9:21
|
show 6 more comments
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'
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 agsub
command).
– Stéphane Chazelas
Aug 5 '14 at 9:08
@StéphaneChazelas: Thanks for suggestion, fixed!
– cuonglm
Aug 5 '14 at 9:21
|
show 6 more comments
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'
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'
edited Aug 5 '14 at 10:44
answered Aug 5 '14 at 8:20
cuonglm
102k23201301
102k23201301
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 agsub
command).
– Stéphane Chazelas
Aug 5 '14 at 9:08
@StéphaneChazelas: Thanks for suggestion, fixed!
– cuonglm
Aug 5 '14 at 9:21
|
show 6 more comments
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 agsub
command).
– Stéphane Chazelas
Aug 5 '14 at 9:08
@StéphaneChazelas: Thanks for suggestion, fixed!
– cuonglm
Aug 5 '14 at 9:21
Worked perfectly, and embarassingly obivous in hindsight. Thanks very much.
– Escher
Aug 5 '14 at 8:34
Worked perfectly, and embarassingly obivous in hindsight. Thanks very much.
– Escher
Aug 5 '14 at 8:34
1
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
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
@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
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
@StéphaneChazelas: Thanks for suggestion, fixed!
– cuonglm
Aug 5 '14 at 9:21
|
show 6 more comments
If you are passing an Awk variable to system, you need to shell quote it:
function quote(str, d, m, x, y, z) {
d = "47"; m = split(str, x, d)
for (y in x) z = z d x[y] d (y < m ? "\" d : "")
return z
}
Example:
system(sprintf("cp -r %s assets", quote($1)))
Source
add a comment |
If you are passing an Awk variable to system, you need to shell quote it:
function quote(str, d, m, x, y, z) {
d = "47"; m = split(str, x, d)
for (y in x) z = z d x[y] d (y < m ? "\" d : "")
return z
}
Example:
system(sprintf("cp -r %s assets", quote($1)))
Source
add a comment |
If you are passing an Awk variable to system, you need to shell quote it:
function quote(str, d, m, x, y, z) {
d = "47"; m = split(str, x, d)
for (y in x) z = z d x[y] d (y < m ? "\" d : "")
return z
}
Example:
system(sprintf("cp -r %s assets", quote($1)))
Source
If you are passing an Awk variable to system, you need to shell quote it:
function quote(str, d, m, x, y, z) {
d = "47"; m = split(str, x, d)
for (y in x) z = z d x[y] d (y < m ? "\" d : "")
return z
}
Example:
system(sprintf("cp -r %s assets", quote($1)))
Source
answered Jan 15 '17 at 6:57
Steven Penny
1
1
add a comment |
add a comment |
Executing external commands is meant for the shell, not for awk:
while read image_dir __; do
cp -r "$image_dir" assets
done < file
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
add a comment |
Executing external commands is meant for the shell, not for awk:
while read image_dir __; do
cp -r "$image_dir" assets
done < file
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
add a comment |
Executing external commands is meant for the shell, not for awk:
while read image_dir __; do
cp -r "$image_dir" assets
done < file
Executing external commands is meant for the shell, not for awk:
while read image_dir __; do
cp -r "$image_dir" assets
done < file
answered Aug 5 '14 at 8:23
konsolebox
1,22799
1,22799
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
add a comment |
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
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
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
system()
is not consistent with all types of input. It's awfully difficult to sanitize it.– konsolebox
Aug 5 '14 at 10:39
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f148454%2fhow-to-escape-spaces-etc-in-passed-variable-for-system-call-to-cp-in-awk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown