Running a Block of Shell Script remotely using SSH

2019-03-04 16:05发布

I am try to execute a block of commands on a different server using a shell script

Can anyone please help me on this

while [ $RecordCount -gt 0 ]
do
  expXXXXX=`sed -n ${RecordCount}p ${GUID_DLT_EXPR_FILE} | cut -d "|" -f1`
  exprXXXXXn_id=`sed -n ${RecordCount}p ${GUID_DLT_EXPR_FILE} | cut -d'|' -f2`
  run_dt=`sed -n ${RecordCount}p ${GUID_DLT_EXPR_FILE} | cut -d'|' -f3`

  #START OF THE BLOCK - IN SERVER 2   

  if [ -d "/sas/ADH/exXd_$expXXXXX" ]; then
    if [ -d "/sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id" ]; then
      mv -f /sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id/latest/* \
        /sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id/archives
    else    
      mkdir /sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id
      mkdir /sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id/latest
      mkdir /sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id/archives
    fi
  else 
    mkdir /sas/ADH/exXd_$expXXXXX
    mkdir /sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id
    mkdir /sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id/latest
    mkdir /sas/ADH/exXd_$expXXXXX/version_$exprXXXXXn_id/archives
  fi

  #END OF THE BLOCK - IN SERVER 2

done

exit 0

标签: shell ksh
2条回答
三岁会撩人
2楼-- · 2019-03-04 16:17

This will do essentially what you're asking for:

while [[ $RecordCount -gt 0 ]]
do
  field1=$( sed -n ${RecordCount}p ${GUID_DLT_EXPR_FILE} | cut -d'|' -f1 )
  field2=$( sed -n ${RecordCount}p ${GUID_DLT_EXPR_FILE} | cut -d'|' -f2 )
  run_dt=$( sed -n ${RecordCount}p ${GUID_DLT_EXPR_FILE} | cut -d'|' -f3 )


  dir="/sas/ADH/exXd_${field1}"
  id_path="$dir/version_${field2}
  latest="$id_path/latest"
  archives="id_path/archives"

  ssh ${REMOTE_SERVER:?} "if [[ -d $id_path ]] &&
      ls -d $latest/* > /dev/null 2>& 1
    then
      mv -f $latest/* $archives
    else
      mkdir -p $latest $archives
    fi"

  update_recordcount

done

exit 0

BTW, you will need to provide your own versions of:

  • REMOTE_SERVER (variable)
  • update_recordcount (function) -- make sure it counts down to zero somehow.
查看更多
霸刀☆藐视天下
3楼-- · 2019-03-04 16:23

Just stream those commands into ssh stdin, like:

ssh remoteserver << EOF
command1
command2
command3
...
EOF

<< here means here-doc - a multiline quote.

查看更多
登录 后发表回答