Bash Scripting not able to parse remote SSH commands correctly
I'm trying to build a script to scp somefiles to another machine, but I'm trying to do some checkups before starting the actual SCP.
One of the checks is to see if there are somefiles (collectl raw files) on a remote host.
This is the part I have from my script:
ssh -T $USERNAME@$HOSTNAME bash << EOF
shopt -s nullglob
FILENAMES=( ${RAWDIR}/*${DATE}* )
if (( ${#FILENAMES[@]} )) && [[ -e ${FILENAMES[0]} ]]
then
echo "At least one file matches the name" >&2
exit 0
else
echo "No files exist" >&2
exit 1
fi
EOF
But I get this error:
tooladm@mxmcato01:tooladm/scripts> bash -x colplot.sh 20190201
+ PLOTDIR=/var/lib/zabbix/collectlfiles
+ RAWDIR=/opt/app/collectl
+ IMDG=3
+ HOSTNAME=mxmcaim03
+ USERNAME=abpdg3
+ DATE=20190201
+ SSH_CONN=zabbix@mxmcamon05
+ SEARCH_STRING='/opt/app/collectl/mxmcaim03-20190201*'
+ [[ 1 -ne 1 ]]
+ [[ 20190201 =~ ^[0-9]{8}$ ]]
+ date -d 20190201
+ is_valid=0
+ [[ 0 -ne 0 ]]
+ file_exists
+ ssh -T zabbix@mxmcamon05
FILES ARE NOT THERE, PROCEEDING WITH THE IMPORT
+ (( 0 ))
+ import_files
+ ssh -T abpdg3@mxmcaim03 bash
bash: line 5: unexpected argument `]]' to conditional unary operator
bash: line 5: syntax error near `]]'
bash: line 5: `if (( 0 )) && [[ -e ]]'
What I had to do to make it work as expected is creating a second script the same hosts from where I'm executing this script and redirect its out to my ssh connection, like this:
lookup_remote_files()
{
ssh -T $USERNAME@$HOSTNAME "bash -s" < ./colplot_remote.sh "$DATE"
}
bash -x colplot.sh 20190201
+ PLOTDIR=/var/lib/zabbix/collectlfiles
+ RAWDIR=/opt/app/collectl
+ IMDG=3
+ HOSTNAME=mxmcaim03
+ USERNAME=abpdg3
+ DATE=20190201
+ SSH_CONN=zabbix@mxmcamon05
+ SEARCH_STRING='/opt/app/collectl/mxmcaim03-20190201*'
+ [[ 1 -ne 1 ]]
+ [[ 20190201 =~ ^[0-9]{8}$ ]]
+ date -d 20190201
+ is_valid=0
+ [[ 0 -ne 0 ]]
+ lookup_local_files
+ ssh -T zabbix@mxmcamon05
FILES ARE NOT THERE, PROCEEDING WITH THE IMPORT
+ (( 0 ))
+ lookup_remote_files
+ ssh -T abpdg3@mxmcaim03 'bash -s' 20190201
At least one file matches the name
+ (( 0 ))
Can someone tell how can I do to make this work from within the same script please?
Thanks.
bash ssh here-document
add a comment |
I'm trying to build a script to scp somefiles to another machine, but I'm trying to do some checkups before starting the actual SCP.
One of the checks is to see if there are somefiles (collectl raw files) on a remote host.
This is the part I have from my script:
ssh -T $USERNAME@$HOSTNAME bash << EOF
shopt -s nullglob
FILENAMES=( ${RAWDIR}/*${DATE}* )
if (( ${#FILENAMES[@]} )) && [[ -e ${FILENAMES[0]} ]]
then
echo "At least one file matches the name" >&2
exit 0
else
echo "No files exist" >&2
exit 1
fi
EOF
But I get this error:
tooladm@mxmcato01:tooladm/scripts> bash -x colplot.sh 20190201
+ PLOTDIR=/var/lib/zabbix/collectlfiles
+ RAWDIR=/opt/app/collectl
+ IMDG=3
+ HOSTNAME=mxmcaim03
+ USERNAME=abpdg3
+ DATE=20190201
+ SSH_CONN=zabbix@mxmcamon05
+ SEARCH_STRING='/opt/app/collectl/mxmcaim03-20190201*'
+ [[ 1 -ne 1 ]]
+ [[ 20190201 =~ ^[0-9]{8}$ ]]
+ date -d 20190201
+ is_valid=0
+ [[ 0 -ne 0 ]]
+ file_exists
+ ssh -T zabbix@mxmcamon05
FILES ARE NOT THERE, PROCEEDING WITH THE IMPORT
+ (( 0 ))
+ import_files
+ ssh -T abpdg3@mxmcaim03 bash
bash: line 5: unexpected argument `]]' to conditional unary operator
bash: line 5: syntax error near `]]'
bash: line 5: `if (( 0 )) && [[ -e ]]'
What I had to do to make it work as expected is creating a second script the same hosts from where I'm executing this script and redirect its out to my ssh connection, like this:
lookup_remote_files()
{
ssh -T $USERNAME@$HOSTNAME "bash -s" < ./colplot_remote.sh "$DATE"
}
bash -x colplot.sh 20190201
+ PLOTDIR=/var/lib/zabbix/collectlfiles
+ RAWDIR=/opt/app/collectl
+ IMDG=3
+ HOSTNAME=mxmcaim03
+ USERNAME=abpdg3
+ DATE=20190201
+ SSH_CONN=zabbix@mxmcamon05
+ SEARCH_STRING='/opt/app/collectl/mxmcaim03-20190201*'
+ [[ 1 -ne 1 ]]
+ [[ 20190201 =~ ^[0-9]{8}$ ]]
+ date -d 20190201
+ is_valid=0
+ [[ 0 -ne 0 ]]
+ lookup_local_files
+ ssh -T zabbix@mxmcamon05
FILES ARE NOT THERE, PROCEEDING WITH THE IMPORT
+ (( 0 ))
+ lookup_remote_files
+ ssh -T abpdg3@mxmcaim03 'bash -s' 20190201
At least one file matches the name
+ (( 0 ))
Can someone tell how can I do to make this work from within the same script please?
Thanks.
bash ssh here-document
add a comment |
I'm trying to build a script to scp somefiles to another machine, but I'm trying to do some checkups before starting the actual SCP.
One of the checks is to see if there are somefiles (collectl raw files) on a remote host.
This is the part I have from my script:
ssh -T $USERNAME@$HOSTNAME bash << EOF
shopt -s nullglob
FILENAMES=( ${RAWDIR}/*${DATE}* )
if (( ${#FILENAMES[@]} )) && [[ -e ${FILENAMES[0]} ]]
then
echo "At least one file matches the name" >&2
exit 0
else
echo "No files exist" >&2
exit 1
fi
EOF
But I get this error:
tooladm@mxmcato01:tooladm/scripts> bash -x colplot.sh 20190201
+ PLOTDIR=/var/lib/zabbix/collectlfiles
+ RAWDIR=/opt/app/collectl
+ IMDG=3
+ HOSTNAME=mxmcaim03
+ USERNAME=abpdg3
+ DATE=20190201
+ SSH_CONN=zabbix@mxmcamon05
+ SEARCH_STRING='/opt/app/collectl/mxmcaim03-20190201*'
+ [[ 1 -ne 1 ]]
+ [[ 20190201 =~ ^[0-9]{8}$ ]]
+ date -d 20190201
+ is_valid=0
+ [[ 0 -ne 0 ]]
+ file_exists
+ ssh -T zabbix@mxmcamon05
FILES ARE NOT THERE, PROCEEDING WITH THE IMPORT
+ (( 0 ))
+ import_files
+ ssh -T abpdg3@mxmcaim03 bash
bash: line 5: unexpected argument `]]' to conditional unary operator
bash: line 5: syntax error near `]]'
bash: line 5: `if (( 0 )) && [[ -e ]]'
What I had to do to make it work as expected is creating a second script the same hosts from where I'm executing this script and redirect its out to my ssh connection, like this:
lookup_remote_files()
{
ssh -T $USERNAME@$HOSTNAME "bash -s" < ./colplot_remote.sh "$DATE"
}
bash -x colplot.sh 20190201
+ PLOTDIR=/var/lib/zabbix/collectlfiles
+ RAWDIR=/opt/app/collectl
+ IMDG=3
+ HOSTNAME=mxmcaim03
+ USERNAME=abpdg3
+ DATE=20190201
+ SSH_CONN=zabbix@mxmcamon05
+ SEARCH_STRING='/opt/app/collectl/mxmcaim03-20190201*'
+ [[ 1 -ne 1 ]]
+ [[ 20190201 =~ ^[0-9]{8}$ ]]
+ date -d 20190201
+ is_valid=0
+ [[ 0 -ne 0 ]]
+ lookup_local_files
+ ssh -T zabbix@mxmcamon05
FILES ARE NOT THERE, PROCEEDING WITH THE IMPORT
+ (( 0 ))
+ lookup_remote_files
+ ssh -T abpdg3@mxmcaim03 'bash -s' 20190201
At least one file matches the name
+ (( 0 ))
Can someone tell how can I do to make this work from within the same script please?
Thanks.
bash ssh here-document
I'm trying to build a script to scp somefiles to another machine, but I'm trying to do some checkups before starting the actual SCP.
One of the checks is to see if there are somefiles (collectl raw files) on a remote host.
This is the part I have from my script:
ssh -T $USERNAME@$HOSTNAME bash << EOF
shopt -s nullglob
FILENAMES=( ${RAWDIR}/*${DATE}* )
if (( ${#FILENAMES[@]} )) && [[ -e ${FILENAMES[0]} ]]
then
echo "At least one file matches the name" >&2
exit 0
else
echo "No files exist" >&2
exit 1
fi
EOF
But I get this error:
tooladm@mxmcato01:tooladm/scripts> bash -x colplot.sh 20190201
+ PLOTDIR=/var/lib/zabbix/collectlfiles
+ RAWDIR=/opt/app/collectl
+ IMDG=3
+ HOSTNAME=mxmcaim03
+ USERNAME=abpdg3
+ DATE=20190201
+ SSH_CONN=zabbix@mxmcamon05
+ SEARCH_STRING='/opt/app/collectl/mxmcaim03-20190201*'
+ [[ 1 -ne 1 ]]
+ [[ 20190201 =~ ^[0-9]{8}$ ]]
+ date -d 20190201
+ is_valid=0
+ [[ 0 -ne 0 ]]
+ file_exists
+ ssh -T zabbix@mxmcamon05
FILES ARE NOT THERE, PROCEEDING WITH THE IMPORT
+ (( 0 ))
+ import_files
+ ssh -T abpdg3@mxmcaim03 bash
bash: line 5: unexpected argument `]]' to conditional unary operator
bash: line 5: syntax error near `]]'
bash: line 5: `if (( 0 )) && [[ -e ]]'
What I had to do to make it work as expected is creating a second script the same hosts from where I'm executing this script and redirect its out to my ssh connection, like this:
lookup_remote_files()
{
ssh -T $USERNAME@$HOSTNAME "bash -s" < ./colplot_remote.sh "$DATE"
}
bash -x colplot.sh 20190201
+ PLOTDIR=/var/lib/zabbix/collectlfiles
+ RAWDIR=/opt/app/collectl
+ IMDG=3
+ HOSTNAME=mxmcaim03
+ USERNAME=abpdg3
+ DATE=20190201
+ SSH_CONN=zabbix@mxmcamon05
+ SEARCH_STRING='/opt/app/collectl/mxmcaim03-20190201*'
+ [[ 1 -ne 1 ]]
+ [[ 20190201 =~ ^[0-9]{8}$ ]]
+ date -d 20190201
+ is_valid=0
+ [[ 0 -ne 0 ]]
+ lookup_local_files
+ ssh -T zabbix@mxmcamon05
FILES ARE NOT THERE, PROCEEDING WITH THE IMPORT
+ (( 0 ))
+ lookup_remote_files
+ ssh -T abpdg3@mxmcaim03 'bash -s' 20190201
At least one file matches the name
+ (( 0 ))
Can someone tell how can I do to make this work from within the same script please?
Thanks.
bash ssh here-document
bash ssh here-document
asked 1 min ago
Eduardo Santiago LópezEduardo Santiago López
34
34
add a comment |
add a comment |
0
active
oldest
votes
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%2f498237%2fbash-scripting-not-able-to-parse-remote-ssh-commands-correctly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f498237%2fbash-scripting-not-able-to-parse-remote-ssh-commands-correctly%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