Code: Select all
#!/bin/sh
#
# Get multiple files from a remote system and concatenate them to a local (z/OS)
# file using the cozsftp command.
#
# This script connects a cozsftp client to a remote system running sshd
# and issues an ls command to get a list of files to get. Each of these files
# is then retrieved and written to the specified local file on z/OS.
#
# cozsftp transfer options (lzopts) can be specified to customize the
# transfer. This script is typically "dotted" (sourced) into a driver
# script which sets the following shell variables to configure the connection:
#
# TRANSFER VARIABLES:
# ldsn - *MUST* be set to the local dataset or DD to be written
# rpat - *MUST* be set to the remote file pattern to get
# lzopts - May be set to the set of cozsftp transfer options
# to use for the transfer (e.g. mode=text,replace=no)
#
# SFTP CONNECTION VARIABLES:
# See the sftp_connect.sh script for additional variables that are available/required for
# proper operation
ldsn=${ldsn:?"No local dataset specified"}
rpat=${rpat:?"No remote file specified"}
if test ! -z "${lzopts}"
then
lzopts="${lzopts},disp=mod"
else
lzopts="disp=mod"
fi
# Need to make available to awk below
export lzopts=${lzopts}
export ldsn=${ldsn}
if test ! -z "${script_dir}"
then
script_dir=${script_dir%/}
else
script_dir=$(dirname $(command -v sftp_connect.sh) 2>/dev/null)
fi
sftp_connect_sh=${script_dir}/sftp_connect.sh
if test ! -x ${sftp_connect_sh}
then
echo "$sftp_connect_cmd_sh not found or is not executable. Check \$PATH or set \$script_dir." 1>&2
exit 1
fi
echo "Concatenating remote files matching $rfile to local file $lfile..." 1>&2
# Set up a temporary file for list of files to get, using umask to make
# accessible only from this userid. The trap command will remove the
# temporary file on exit
umask 077
export tmpfile=${TMPDIR-/tmp}/sftp_cat.$$
trap 'rm -f $tmpfile' EXIT
{
. ${sftp_connect_sh} <<EOB
ls -l $rpat
EOB
} | awk '
BEGIN {
print "lzopts " ENVIRON["lzopts"] >> ENVIRON["tmpfile"]
skip = 1
}
{
if (skip == 1) {
if ($1 == "cozsftp>") skip = 0
next;
}
print "get " $9 " " ENVIRON["ldsn"] >> ENVIRON["tmpfile"]
#(optional) delete the remote file after uploading
#print "rm " $9 >> ENVIRON["tmpfile"]
}'
# In a normal POSIX environment, the awk output would be piped directly to the sftp command,
# but z/OS Unix System Services is not normal. The pipe followed by a shell script causes
# the script to be run in a separate address space, leaving any DDs inaccessible. To solve this,
# a temporary file is used to capture the sftp commands created by awk, then used as input to
# sftp without using a pipeline
. ${sftp_connect_sh} < $tmpfile