Use grep and cut inside a bash function
i have the following bash code:
function ()
curl="$(curl -s "$2" >> "$1")"
version="$(grep '$3' $1
function "test" "https://google.com" "String" "cut -d' ' -f3 | cut -d'<' -f1"
Basically the function downloads the page and then uses grep to look for a specific string. After that "cut", cuts down the results further. But ... unfortenatly the cut inside the function doesn't work. I only get the following output:
"usage: cut -b list [-n] [file ...]
cut -c list [file ...]
cut -f list [-s] [-d delim] [file ...]"
Maybe i overlooked something ... or maybe you have a better idea :-)
bash function curl grep cut
add a comment |
i have the following bash code:
function ()
curl="$(curl -s "$2" >> "$1")"
version="$(grep '$3' $1
function "test" "https://google.com" "String" "cut -d' ' -f3 | cut -d'<' -f1"
Basically the function downloads the page and then uses grep to look for a specific string. After that "cut", cuts down the results further. But ... unfortenatly the cut inside the function doesn't work. I only get the following output:
"usage: cut -b list [-n] [file ...]
cut -c list [file ...]
cut -f list [-s] [-d delim] [file ...]"
Maybe i overlooked something ... or maybe you have a better idea :-)
bash function curl grep cut
2
See: Difference between single and double quotes in bash
– Cyrus
Nov 12 at 19:25
1
function
is a bash builtin command/keyword. See:help
– Cyrus
Nov 12 at 19:27
1
I don't understand what you're trying to do withcurl="$(curl ...)"
, as you don't ever use thecurl
variable after that.
– Benjamin W.
Nov 12 at 20:52
yeah, there won't be anything in it anyway. stdout is redirected, and stderr won't be collected into the var.
– Paul Hodges
Nov 12 at 22:07
A yes the curl variable was nonse and the function isn't titled function of course. It was just for the posting here :)
– Stfn
Nov 13 at 6:55
add a comment |
i have the following bash code:
function ()
curl="$(curl -s "$2" >> "$1")"
version="$(grep '$3' $1
function "test" "https://google.com" "String" "cut -d' ' -f3 | cut -d'<' -f1"
Basically the function downloads the page and then uses grep to look for a specific string. After that "cut", cuts down the results further. But ... unfortenatly the cut inside the function doesn't work. I only get the following output:
"usage: cut -b list [-n] [file ...]
cut -c list [file ...]
cut -f list [-s] [-d delim] [file ...]"
Maybe i overlooked something ... or maybe you have a better idea :-)
bash function curl grep cut
i have the following bash code:
function ()
curl="$(curl -s "$2" >> "$1")"
version="$(grep '$3' $1
function "test" "https://google.com" "String" "cut -d' ' -f3 | cut -d'<' -f1"
Basically the function downloads the page and then uses grep to look for a specific string. After that "cut", cuts down the results further. But ... unfortenatly the cut inside the function doesn't work. I only get the following output:
"usage: cut -b list [-n] [file ...]
cut -c list [file ...]
cut -f list [-s] [-d delim] [file ...]"
Maybe i overlooked something ... or maybe you have a better idea :-)
bash function curl grep cut
bash function curl grep cut
asked Nov 12 at 19:23
Stfn
62
62
2
See: Difference between single and double quotes in bash
– Cyrus
Nov 12 at 19:25
1
function
is a bash builtin command/keyword. See:help
– Cyrus
Nov 12 at 19:27
1
I don't understand what you're trying to do withcurl="$(curl ...)"
, as you don't ever use thecurl
variable after that.
– Benjamin W.
Nov 12 at 20:52
yeah, there won't be anything in it anyway. stdout is redirected, and stderr won't be collected into the var.
– Paul Hodges
Nov 12 at 22:07
A yes the curl variable was nonse and the function isn't titled function of course. It was just for the posting here :)
– Stfn
Nov 13 at 6:55
add a comment |
2
See: Difference between single and double quotes in bash
– Cyrus
Nov 12 at 19:25
1
function
is a bash builtin command/keyword. See:help
– Cyrus
Nov 12 at 19:27
1
I don't understand what you're trying to do withcurl="$(curl ...)"
, as you don't ever use thecurl
variable after that.
– Benjamin W.
Nov 12 at 20:52
yeah, there won't be anything in it anyway. stdout is redirected, and stderr won't be collected into the var.
– Paul Hodges
Nov 12 at 22:07
A yes the curl variable was nonse and the function isn't titled function of course. It was just for the posting here :)
– Stfn
Nov 13 at 6:55
2
2
See: Difference between single and double quotes in bash
– Cyrus
Nov 12 at 19:25
See: Difference between single and double quotes in bash
– Cyrus
Nov 12 at 19:25
1
1
function
is a bash builtin command/keyword. See: help
– Cyrus
Nov 12 at 19:27
function
is a bash builtin command/keyword. See: help
– Cyrus
Nov 12 at 19:27
1
1
I don't understand what you're trying to do with
curl="$(curl ...)"
, as you don't ever use the curl
variable after that.– Benjamin W.
Nov 12 at 20:52
I don't understand what you're trying to do with
curl="$(curl ...)"
, as you don't ever use the curl
variable after that.– Benjamin W.
Nov 12 at 20:52
yeah, there won't be anything in it anyway. stdout is redirected, and stderr won't be collected into the var.
– Paul Hodges
Nov 12 at 22:07
yeah, there won't be anything in it anyway. stdout is redirected, and stderr won't be collected into the var.
– Paul Hodges
Nov 12 at 22:07
A yes the curl variable was nonse and the function isn't titled function of course. It was just for the posting here :)
– Stfn
Nov 13 at 6:55
A yes the curl variable was nonse and the function isn't titled function of course. It was just for the posting here :)
– Stfn
Nov 13 at 6:55
add a comment |
1 Answer
1
active
oldest
votes
Since $4
contains shell metacharacters that aren't processed when expanding variables, you need to use eval
to execute it.
Also, you need to put $3
in double quotes, not single quotes, otherwise the variable won't be expanded.
version="$(grep "$3" "$1" | eval "$4" )"
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53268773%2fuse-grep-and-cut-inside-a-bash-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since $4
contains shell metacharacters that aren't processed when expanding variables, you need to use eval
to execute it.
Also, you need to put $3
in double quotes, not single quotes, otherwise the variable won't be expanded.
version="$(grep "$3" "$1" | eval "$4" )"
add a comment |
Since $4
contains shell metacharacters that aren't processed when expanding variables, you need to use eval
to execute it.
Also, you need to put $3
in double quotes, not single quotes, otherwise the variable won't be expanded.
version="$(grep "$3" "$1" | eval "$4" )"
add a comment |
Since $4
contains shell metacharacters that aren't processed when expanding variables, you need to use eval
to execute it.
Also, you need to put $3
in double quotes, not single quotes, otherwise the variable won't be expanded.
version="$(grep "$3" "$1" | eval "$4" )"
Since $4
contains shell metacharacters that aren't processed when expanding variables, you need to use eval
to execute it.
Also, you need to put $3
in double quotes, not single quotes, otherwise the variable won't be expanded.
version="$(grep "$3" "$1" | eval "$4" )"
answered Nov 12 at 20:02
Barmar
419k34243344
419k34243344
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53268773%2fuse-grep-and-cut-inside-a-bash-function%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
2
See: Difference between single and double quotes in bash
– Cyrus
Nov 12 at 19:25
1
function
is a bash builtin command/keyword. See:help
– Cyrus
Nov 12 at 19:27
1
I don't understand what you're trying to do with
curl="$(curl ...)"
, as you don't ever use thecurl
variable after that.– Benjamin W.
Nov 12 at 20:52
yeah, there won't be anything in it anyway. stdout is redirected, and stderr won't be collected into the var.
– Paul Hodges
Nov 12 at 22:07
A yes the curl variable was nonse and the function isn't titled function of course. It was just for the posting here :)
– Stfn
Nov 13 at 6:55