使用SSH远程运行Shell脚本块

使用SSH远程运行Shell脚本块,shell,ksh,Shell,Ksh,我尝试使用shell脚本在不同的服务器上执行命令块 谁能帮我一下吗 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

我尝试使用shell脚本在不同的服务器上执行命令块

谁能帮我一下吗

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

只需将这些命令流式传输到ssh stdin中,如:

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

ssh-remoteserver这将基本满足您的要求:

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
顺便说一句,您需要提供自己版本的:

  • 远程服务器(变量)
  • update\u recordcount
    (函数)——以某种方式确保它的计数为零

我尝试了下面的选项,但发现“ioctl dor设备不正确”错误。(ssh-t-t remoteserver@user3299344在重定向stdin时不要为ssh组合
-t
选项。请尝试
ssh${REMOTE_SERVER:?}echo ok
。如果失败,则远程帐户的环境会出现问题,例如shell初始化期间的stty命令。