Bash Scripting not able to parse remote SSH commands correctly












0















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.










share|improve this question























  • If you're using ssh ... <<EOF, all the variables in the here-doc will be expanded on the local, not on the remote machine. Is that what you want? Try ssh .. <<'EOF' instead (single quotes around EOF).

    – Uncle Billy
    7 mins ago











  • Yes, I've tried that, but given that I'm using some local variables as well, it doesn't work either. Thanks

    – Eduardo Santiago López
    6 mins ago











  • Then you'll have to escape ($) the variables that should be expanded on the remote machine.

    – Uncle Billy
    4 mins ago











  • I've also tried that, and nothing seems to work.

    – Eduardo Santiago López
    2 mins ago











  • The error message shows that ${FILENAMES[0]} is expanded to nothing on the local machine.

    – Uncle Billy
    27 secs ago
















0















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.










share|improve this question























  • If you're using ssh ... <<EOF, all the variables in the here-doc will be expanded on the local, not on the remote machine. Is that what you want? Try ssh .. <<'EOF' instead (single quotes around EOF).

    – Uncle Billy
    7 mins ago











  • Yes, I've tried that, but given that I'm using some local variables as well, it doesn't work either. Thanks

    – Eduardo Santiago López
    6 mins ago











  • Then you'll have to escape ($) the variables that should be expanded on the remote machine.

    – Uncle Billy
    4 mins ago











  • I've also tried that, and nothing seems to work.

    – Eduardo Santiago López
    2 mins ago











  • The error message shows that ${FILENAMES[0]} is expanded to nothing on the local machine.

    – Uncle Billy
    27 secs ago














0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 17 mins ago









Eduardo Santiago LópezEduardo Santiago López

34




34













  • If you're using ssh ... <<EOF, all the variables in the here-doc will be expanded on the local, not on the remote machine. Is that what you want? Try ssh .. <<'EOF' instead (single quotes around EOF).

    – Uncle Billy
    7 mins ago











  • Yes, I've tried that, but given that I'm using some local variables as well, it doesn't work either. Thanks

    – Eduardo Santiago López
    6 mins ago











  • Then you'll have to escape ($) the variables that should be expanded on the remote machine.

    – Uncle Billy
    4 mins ago











  • I've also tried that, and nothing seems to work.

    – Eduardo Santiago López
    2 mins ago











  • The error message shows that ${FILENAMES[0]} is expanded to nothing on the local machine.

    – Uncle Billy
    27 secs ago



















  • If you're using ssh ... <<EOF, all the variables in the here-doc will be expanded on the local, not on the remote machine. Is that what you want? Try ssh .. <<'EOF' instead (single quotes around EOF).

    – Uncle Billy
    7 mins ago











  • Yes, I've tried that, but given that I'm using some local variables as well, it doesn't work either. Thanks

    – Eduardo Santiago López
    6 mins ago











  • Then you'll have to escape ($) the variables that should be expanded on the remote machine.

    – Uncle Billy
    4 mins ago











  • I've also tried that, and nothing seems to work.

    – Eduardo Santiago López
    2 mins ago











  • The error message shows that ${FILENAMES[0]} is expanded to nothing on the local machine.

    – Uncle Billy
    27 secs ago

















If you're using ssh ... <<EOF, all the variables in the here-doc will be expanded on the local, not on the remote machine. Is that what you want? Try ssh .. <<'EOF' instead (single quotes around EOF).

– Uncle Billy
7 mins ago





If you're using ssh ... <<EOF, all the variables in the here-doc will be expanded on the local, not on the remote machine. Is that what you want? Try ssh .. <<'EOF' instead (single quotes around EOF).

– Uncle Billy
7 mins ago













Yes, I've tried that, but given that I'm using some local variables as well, it doesn't work either. Thanks

– Eduardo Santiago López
6 mins ago





Yes, I've tried that, but given that I'm using some local variables as well, it doesn't work either. Thanks

– Eduardo Santiago López
6 mins ago













Then you'll have to escape ($) the variables that should be expanded on the remote machine.

– Uncle Billy
4 mins ago





Then you'll have to escape ($) the variables that should be expanded on the remote machine.

– Uncle Billy
4 mins ago













I've also tried that, and nothing seems to work.

– Eduardo Santiago López
2 mins ago





I've also tried that, and nothing seems to work.

– Eduardo Santiago López
2 mins ago













The error message shows that ${FILENAMES[0]} is expanded to nothing on the local machine.

– Uncle Billy
27 secs ago





The error message shows that ${FILENAMES[0]} is expanded to nothing on the local machine.

– Uncle Billy
27 secs ago










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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Entries order in /etc/network/interfaces

新発田市

Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)