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

I need to make this work from within the same script.

UPDATE:

The error is not there anymore, which I had also tried that, but it is not working as expected, for the date I'm using there are no files there:

tooladm@mxmcato01:tooladm/scripts> bash -x colplot.sh 20190205
+ PLOTDIR=/var/lib/zabbix/collectlfiles
+ RAWDIR=/opt/app/collectl
+ IMDG=3
+ HOSTNAME=mxmcaim03
+ USERNAME=abpdg3
+ DATE=20190205
+ SSH_CONN=zabbix@mxmcamon05
+ SEARCH_STRING='/opt/app/collectl/mxmcaim03-20190205*'
+ [[ 1 -ne 1 ]]
+ [[ 20190205 =~ ^[0-9]{8}$ ]]
+ date -d 20190205
+ 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
At least one file matches the name
+ (( 0 ))
  • 2
    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 Feb 2 at 1:47
  • 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 Feb 2 at 1:49
  • Then you'll have to escape (\$) the variables that should be expanded on the remote machine. – Uncle Billy Feb 2 at 1:50
  • I've also tried that, and nothing seems to work. – Eduardo Santiago López Feb 2 at 1:52
  • The error message shows that ${FILENAMES[0]} is expanded to nothing on the local machine. – Uncle Billy Feb 2 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.