构建服务器上,要运行脚本来部署服务的话。遇到的一个问题就是要用ssh和scp来执行远程服务器。 但是有一个问题,就是变量的使用。因为没有掌握到,所以之前有走入误区。还好在百度过程找到如下代码
#!/bin/bash BAK_TIME='2014-10-06'COMMAND="sudo /bin/tar -zxf /data/db_backup/ssh_test_${BAK_TIME}-*.tar.gz -C /data; \ EXTRACT_DIR=\`sudo /bin/find /data -type d -name ssh_test_${BAK_TIME}-*\`; \ for i in \`sudo /bin/ls \$EXTRACT_DIR\` ; do mysql -uroot -p'123456' testdb -e \"source \$EXTRACT_DIR/\$i\" ;done" ssh -lroot -p22 192.168.10.169 "${COMMAND}"
发现原来我这种写法是有问题的
function deploye (){ #cd $CODEPATH #scp target/$JARNAME $DEPLOYESERVER:$DEPLOYEPATH/$JARNAME.new ssh -o StrictHostKeyChecking=no $DEPLOYESERVER << eeooff echo '-------------------' echo $DEPLOYEPATH/$JARNAME echo '------------------' # MTIME=`ls -l --time-style="long-iso" $DEPLOYEPATH/$JARNAME |awk '{ print $6 "-" $7}'` echo '----------------' echo $MTIME echo '------------------------' cd $DEPLOYEPATH # mv -f $JARNAME $JARNAME.$MTIME # mv -f $JARNAME.new $JARNAME exit eeooff}
用这种方法还会报错 修改成下面的方法就没问题了
function deploye (){ cd $CODEPATH scp target/$JARNAME $DEPLOYESERVER:$DEPLOYEPATH/$JARNAME.newssh -o StrictHostKeyChecking=no $DEPLOYESERVER << eeooff #用变量的方法有问题,解决了 # echo > tpid MTIME=\`ls -l --time-style="long-iso" $DEPLOYEPATH/$JARNAME |awk '{ print \$6 "-" \$7}'\` cd $DEPLOYEPATH mv -f $JARNAME $JARNAME.\$MTIME mv -f $JARNAME.new $JARNAME exiteeooff}
#!/bin/bashexport PGPASSWORD="login password" ssh -o StrictHostKeyChecking=no user@domain > /dev/null 2>&1 << eeooff you cmd(需要root权限执行时:echo password | sudo -S you cmd) exit eeooff