使用SSH远程运行Shell脚本块

时间:2014-02-11 23:04:09

标签: 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}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

2 个答案:

答案 0 :(得分:2)

将这些命令流式传输到ssh stdin,例如:

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

<<这里的意思是here-doc - 多行引用。

答案 1 :(得分:0)

这基本上可以满足您的要求:

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

  • REMOTE_SERVER(变量)
  • update_recordcount(功能) - 确保以某种方式减少到零。
相关问题