use EOF in a one-liner?
Clash Royale CLAN TAG#URR8PPP
I have a simple script that executes commands on remote host using ssh and EOF
I was trying to do it with a one-liner at command line but I couldn't figure out how to do the EOF outside of a script. is it possible? I spent some time googling and reading HereDocs documentation but it wasn't coming to me
this script works fine - I needed the EOF section in order to run awk remotely via ssh but I usually like to do one-liners for simple stuff:
#!/bin/bash
# it looks up all my hosts with 'db' in the name
# then gets the PID of any rsyncs running as user 'research'
# and pumps them into xargs
getHosts=(`curl --silent "http://assetts.lab/all_hosts" | grep -v ^# | awk -F" " 'print$1'|grep db`)
for BOX in $getHosts[@];do
echo "$BOX: "
ssh -T sshUser@$BOX <<"EOF"
ps -ef | egrep "rsync|iasync" | awk -F" " 'if ($1 ~ "research") print $2'|sudo xargs -i ps -fp ''
#ps -ef | egrep "rsync|iasync" | awk -F" " 'if ($1 ~ "research") print $2'|sudo xargs -i kill ''
EOF
echo
done
i'm probably missing something simple...
anyway thanks for any suggestions :)
bash
add a comment |
I have a simple script that executes commands on remote host using ssh and EOF
I was trying to do it with a one-liner at command line but I couldn't figure out how to do the EOF outside of a script. is it possible? I spent some time googling and reading HereDocs documentation but it wasn't coming to me
this script works fine - I needed the EOF section in order to run awk remotely via ssh but I usually like to do one-liners for simple stuff:
#!/bin/bash
# it looks up all my hosts with 'db' in the name
# then gets the PID of any rsyncs running as user 'research'
# and pumps them into xargs
getHosts=(`curl --silent "http://assetts.lab/all_hosts" | grep -v ^# | awk -F" " 'print$1'|grep db`)
for BOX in $getHosts[@];do
echo "$BOX: "
ssh -T sshUser@$BOX <<"EOF"
ps -ef | egrep "rsync|iasync" | awk -F" " 'if ($1 ~ "research") print $2'|sudo xargs -i ps -fp ''
#ps -ef | egrep "rsync|iasync" | awk -F" " 'if ($1 ~ "research") print $2'|sudo xargs -i kill ''
EOF
echo
done
i'm probably missing something simple...
anyway thanks for any suggestions :)
bash
Your current code is simple and readable. You already have 2 kinds of quotes in the remote command: using some more condensed form will make your code "clever", and that's usually contrary to maintainable
– glenn jackman
Oct 8 '14 at 1:14
What you are referring to asEOF
is called a "here-document". It's a type of redirection.
– Kusalananda
Jan 15 at 10:26
yep mentioned that in the OP
– j-marr
Jan 17 at 2:45
add a comment |
I have a simple script that executes commands on remote host using ssh and EOF
I was trying to do it with a one-liner at command line but I couldn't figure out how to do the EOF outside of a script. is it possible? I spent some time googling and reading HereDocs documentation but it wasn't coming to me
this script works fine - I needed the EOF section in order to run awk remotely via ssh but I usually like to do one-liners for simple stuff:
#!/bin/bash
# it looks up all my hosts with 'db' in the name
# then gets the PID of any rsyncs running as user 'research'
# and pumps them into xargs
getHosts=(`curl --silent "http://assetts.lab/all_hosts" | grep -v ^# | awk -F" " 'print$1'|grep db`)
for BOX in $getHosts[@];do
echo "$BOX: "
ssh -T sshUser@$BOX <<"EOF"
ps -ef | egrep "rsync|iasync" | awk -F" " 'if ($1 ~ "research") print $2'|sudo xargs -i ps -fp ''
#ps -ef | egrep "rsync|iasync" | awk -F" " 'if ($1 ~ "research") print $2'|sudo xargs -i kill ''
EOF
echo
done
i'm probably missing something simple...
anyway thanks for any suggestions :)
bash
I have a simple script that executes commands on remote host using ssh and EOF
I was trying to do it with a one-liner at command line but I couldn't figure out how to do the EOF outside of a script. is it possible? I spent some time googling and reading HereDocs documentation but it wasn't coming to me
this script works fine - I needed the EOF section in order to run awk remotely via ssh but I usually like to do one-liners for simple stuff:
#!/bin/bash
# it looks up all my hosts with 'db' in the name
# then gets the PID of any rsyncs running as user 'research'
# and pumps them into xargs
getHosts=(`curl --silent "http://assetts.lab/all_hosts" | grep -v ^# | awk -F" " 'print$1'|grep db`)
for BOX in $getHosts[@];do
echo "$BOX: "
ssh -T sshUser@$BOX <<"EOF"
ps -ef | egrep "rsync|iasync" | awk -F" " 'if ($1 ~ "research") print $2'|sudo xargs -i ps -fp ''
#ps -ef | egrep "rsync|iasync" | awk -F" " 'if ($1 ~ "research") print $2'|sudo xargs -i kill ''
EOF
echo
done
i'm probably missing something simple...
anyway thanks for any suggestions :)
bash
bash
edited Oct 7 '14 at 21:17
j-marr
asked Oct 7 '14 at 21:11
j-marrj-marr
236
236
Your current code is simple and readable. You already have 2 kinds of quotes in the remote command: using some more condensed form will make your code "clever", and that's usually contrary to maintainable
– glenn jackman
Oct 8 '14 at 1:14
What you are referring to asEOF
is called a "here-document". It's a type of redirection.
– Kusalananda
Jan 15 at 10:26
yep mentioned that in the OP
– j-marr
Jan 17 at 2:45
add a comment |
Your current code is simple and readable. You already have 2 kinds of quotes in the remote command: using some more condensed form will make your code "clever", and that's usually contrary to maintainable
– glenn jackman
Oct 8 '14 at 1:14
What you are referring to asEOF
is called a "here-document". It's a type of redirection.
– Kusalananda
Jan 15 at 10:26
yep mentioned that in the OP
– j-marr
Jan 17 at 2:45
Your current code is simple and readable. You already have 2 kinds of quotes in the remote command: using some more condensed form will make your code "clever", and that's usually contrary to maintainable
– glenn jackman
Oct 8 '14 at 1:14
Your current code is simple and readable. You already have 2 kinds of quotes in the remote command: using some more condensed form will make your code "clever", and that's usually contrary to maintainable
– glenn jackman
Oct 8 '14 at 1:14
What you are referring to as
EOF
is called a "here-document". It's a type of redirection.– Kusalananda
Jan 15 at 10:26
What you are referring to as
EOF
is called a "here-document". It's a type of redirection.– Kusalananda
Jan 15 at 10:26
yep mentioned that in the OP
– j-marr
Jan 17 at 2:45
yep mentioned that in the OP
– j-marr
Jan 17 at 2:45
add a comment |
1 Answer
1
active
oldest
votes
The EOF bash feature is basically for use as you do in your example, ie in order to send multiple lines of text to another command.
If you want this as a one-liner, the EOF has to go.
In order to execute a command on a remote host using ssh, all you need to do is:
ssh host command
For example:
ssh host "ls -alh"
will run the command on host and return the output on the console.
Your example code could be rewritten in a one line if you take note of how you use the quotation marks, something like this:
ssh host "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'"
the awk doesn't actually work on the remote host when used this way - this is why I used heredocs to begin with #works:$ ssh luser@box "ps -ef | egrep 'rsync|iasync'" luser 7457 7454 0 11:44 ? 00:00:00 bash -c ps -ef | egrep 'rsync|iasync' luser 7476 7457 0 11:44 ? 00:00:00 egrep rsync|iasync
# no worky ` $ssh luser@box "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'" Usage: awk [POSIX or GNU style options] -f progfile [--] file ... `
– j-marr
Oct 16 '14 at 15:45
^^ sorry I don't yet know how to format in comments :( ^^
– j-marr
Oct 16 '14 at 15:52
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%2f159911%2fuse-eof-in-a-one-liner%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
The EOF bash feature is basically for use as you do in your example, ie in order to send multiple lines of text to another command.
If you want this as a one-liner, the EOF has to go.
In order to execute a command on a remote host using ssh, all you need to do is:
ssh host command
For example:
ssh host "ls -alh"
will run the command on host and return the output on the console.
Your example code could be rewritten in a one line if you take note of how you use the quotation marks, something like this:
ssh host "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'"
the awk doesn't actually work on the remote host when used this way - this is why I used heredocs to begin with #works:$ ssh luser@box "ps -ef | egrep 'rsync|iasync'" luser 7457 7454 0 11:44 ? 00:00:00 bash -c ps -ef | egrep 'rsync|iasync' luser 7476 7457 0 11:44 ? 00:00:00 egrep rsync|iasync
# no worky ` $ssh luser@box "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'" Usage: awk [POSIX or GNU style options] -f progfile [--] file ... `
– j-marr
Oct 16 '14 at 15:45
^^ sorry I don't yet know how to format in comments :( ^^
– j-marr
Oct 16 '14 at 15:52
add a comment |
The EOF bash feature is basically for use as you do in your example, ie in order to send multiple lines of text to another command.
If you want this as a one-liner, the EOF has to go.
In order to execute a command on a remote host using ssh, all you need to do is:
ssh host command
For example:
ssh host "ls -alh"
will run the command on host and return the output on the console.
Your example code could be rewritten in a one line if you take note of how you use the quotation marks, something like this:
ssh host "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'"
the awk doesn't actually work on the remote host when used this way - this is why I used heredocs to begin with #works:$ ssh luser@box "ps -ef | egrep 'rsync|iasync'" luser 7457 7454 0 11:44 ? 00:00:00 bash -c ps -ef | egrep 'rsync|iasync' luser 7476 7457 0 11:44 ? 00:00:00 egrep rsync|iasync
# no worky ` $ssh luser@box "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'" Usage: awk [POSIX or GNU style options] -f progfile [--] file ... `
– j-marr
Oct 16 '14 at 15:45
^^ sorry I don't yet know how to format in comments :( ^^
– j-marr
Oct 16 '14 at 15:52
add a comment |
The EOF bash feature is basically for use as you do in your example, ie in order to send multiple lines of text to another command.
If you want this as a one-liner, the EOF has to go.
In order to execute a command on a remote host using ssh, all you need to do is:
ssh host command
For example:
ssh host "ls -alh"
will run the command on host and return the output on the console.
Your example code could be rewritten in a one line if you take note of how you use the quotation marks, something like this:
ssh host "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'"
The EOF bash feature is basically for use as you do in your example, ie in order to send multiple lines of text to another command.
If you want this as a one-liner, the EOF has to go.
In order to execute a command on a remote host using ssh, all you need to do is:
ssh host command
For example:
ssh host "ls -alh"
will run the command on host and return the output on the console.
Your example code could be rewritten in a one line if you take note of how you use the quotation marks, something like this:
ssh host "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'"
answered Oct 15 '14 at 14:37
Martin OlikaMartin Olika
36923
36923
the awk doesn't actually work on the remote host when used this way - this is why I used heredocs to begin with #works:$ ssh luser@box "ps -ef | egrep 'rsync|iasync'" luser 7457 7454 0 11:44 ? 00:00:00 bash -c ps -ef | egrep 'rsync|iasync' luser 7476 7457 0 11:44 ? 00:00:00 egrep rsync|iasync
# no worky ` $ssh luser@box "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'" Usage: awk [POSIX or GNU style options] -f progfile [--] file ... `
– j-marr
Oct 16 '14 at 15:45
^^ sorry I don't yet know how to format in comments :( ^^
– j-marr
Oct 16 '14 at 15:52
add a comment |
the awk doesn't actually work on the remote host when used this way - this is why I used heredocs to begin with #works:$ ssh luser@box "ps -ef | egrep 'rsync|iasync'" luser 7457 7454 0 11:44 ? 00:00:00 bash -c ps -ef | egrep 'rsync|iasync' luser 7476 7457 0 11:44 ? 00:00:00 egrep rsync|iasync
# no worky ` $ssh luser@box "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'" Usage: awk [POSIX or GNU style options] -f progfile [--] file ... `
– j-marr
Oct 16 '14 at 15:45
^^ sorry I don't yet know how to format in comments :( ^^
– j-marr
Oct 16 '14 at 15:52
the awk doesn't actually work on the remote host when used this way - this is why I used heredocs to begin with #works:
$ ssh luser@box "ps -ef | egrep 'rsync|iasync'" luser 7457 7454 0 11:44 ? 00:00:00 bash -c ps -ef | egrep 'rsync|iasync' luser 7476 7457 0 11:44 ? 00:00:00 egrep rsync|iasync
# no worky ` $ssh luser@box "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'" Usage: awk [POSIX or GNU style options] -f progfile [--] file ... `– j-marr
Oct 16 '14 at 15:45
the awk doesn't actually work on the remote host when used this way - this is why I used heredocs to begin with #works:
$ ssh luser@box "ps -ef | egrep 'rsync|iasync'" luser 7457 7454 0 11:44 ? 00:00:00 bash -c ps -ef | egrep 'rsync|iasync' luser 7476 7457 0 11:44 ? 00:00:00 egrep rsync|iasync
# no worky ` $ssh luser@box "ps -ef | egrep 'rsync|iasync' | awk -F" " 'if ($1 ~ "research") print $2'" Usage: awk [POSIX or GNU style options] -f progfile [--] file ... `– j-marr
Oct 16 '14 at 15:45
^^ sorry I don't yet know how to format in comments :( ^^
– j-marr
Oct 16 '14 at 15:52
^^ sorry I don't yet know how to format in comments :( ^^
– j-marr
Oct 16 '14 at 15:52
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.
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%2f159911%2fuse-eof-in-a-one-liner%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
Your current code is simple and readable. You already have 2 kinds of quotes in the remote command: using some more condensed form will make your code "clever", and that's usually contrary to maintainable
– glenn jackman
Oct 8 '14 at 1:14
What you are referring to as
EOF
is called a "here-document". It's a type of redirection.– Kusalananda
Jan 15 at 10:26
yep mentioned that in the OP
– j-marr
Jan 17 at 2:45