Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make @sh work with objects; add @shassoc (fix #1947) #2828

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2098,8 +2098,14 @@ sections:
* `@sh`:

The input is escaped suitable for use in a command-line
for a POSIX shell. If the input is an array, the output
will be a series of space-separated strings.
for a POSIX shell using `eval`. If the input is an array,
the output will be a series of space-separated strings.
If the input is an object, the output will be a series of
space-separated variable assignments for all the keys in
the object that are valid shell variable names (other keys
will be ignored).

E.g., `eval $(jq -r '@sh' f.json)`
Copy link
Member

@emanuele6 emanuele6 Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this example is supposed to show, why would you run @sh to quote everything into a single quoted shell word, and use eval? It would be the exact same thing as running "$(jq -r . f.json)". But, anyway, it is still wrong.
You need to quote the expansion, and arbitrary strings passed to eval need to be prefixed by a space to avoid making the shell interpret a string that starts with - as an option to eval: eval " $(foo)"

Most of the times I use @sh, I don't use it with eval, I use it with | sh:

jq -r '.things[] | @sh "cmd -o \(.foo) -- \(.majigs)"' | sh

eval is only useful if you want to set multiple variables at once (if you want to set only one variable, you just use foo=$(jq)), or an indexed/associative array variable, though it is more convenient to use declare for that.

If we want to put an example, it should at least make sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is supposed to be an example for the new @sh behaviour for objects that creates a string of assignments.
e.g. jq -n '{foo:1, bar:2} | @sh' => foo='1' bar='2'.
You still need to use proper quoting to make this actually work, but at least now I get it. ^^

eval " $(jq -r '@sh' file.json)"


* `@base64`:

Expand Down Expand Up @@ -2135,6 +2141,14 @@ sections:
input: "\"O'Hara's Ale\""
output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]

- program: '@sh'
input: '["a b", "c d"]'
output: ["\"'a b' 'c d'\""]

- program: '@sh'
input: '{"foo":"a b", "bar":"c d", "x y":"ignored"}'
output: ["\"foo='a b' bar='c d'\""]

- program: '@base64'
input: '"This is a message"'
output: ['"VGhpcyBpcyBhIG1lc3NhZ2U="']
Expand Down
13 changes: 12 additions & 1 deletion jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,48 @@ static jv f_format(jq_state *jq, jv input, jv fmt) {
}
jv_free(input);
return line;
} else if (!strcmp(fmt_s, "sh") && jv_get_kind(input) == JV_KIND_OBJECT) {
jv_free(fmt);
jv line = jv_string("");
int first = 1;
jv_object_foreach(input, k, x) {
if (strspn(jv_string_value(k),
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789_") != (size_t)jv_string_length_bytes(jv_copy(k)) ||
strspn(jv_string_value(k), "0123456789") != 0) {
/* Not a valid shell variable name; we don't support assignments to array variables */
jv_free(k);
jv_free(x);
continue;
}
if (!first) line = jv_string_append_str(line, " ");
first = 0;
line = jv_string_concat(line, k);
line = jv_string_append_str(line, "=");
switch (jv_get_kind(x)) {
case JV_KIND_NULL:
case JV_KIND_TRUE:
case JV_KIND_FALSE:
case JV_KIND_NUMBER:
line = jv_string_concat(line, jv_dump_string(x, 0));
break;

case JV_KIND_STRING: {
line = jv_string_append_str(line, "'");
line = jv_string_concat(line, escape_string(x, "''\\''\0"));
line = jv_string_append_str(line, "'");
break;
}

default:
jv_free(input);
jv_free(line);
return type_error(x, "can not be escaped for shell");
}
}
jv_free(input);
return line;
} else if (!strcmp(fmt_s, "sh")) {
jv_free(fmt);
if (jv_get_kind(input) != JV_KIND_ARRAY)
Expand Down
8 changes: 8 additions & 0 deletions tests/man.test

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.