centos6环境远程执行shell脚本报错not a valid identifier的问题处理

# 通过jenkins的apache用户rsync同步php代码到远程服务器报错如下:

SSH: EXEC: STDOUT/STDERR from command [/bin/sh /usr/local/worksh/jenkins_rsync/rsync_cc-product-api_cn.sh all ] ...
/usr/local/worksh/jenkins_rsync/rsync_cc-product-api_cn.sh: line 29: `product-api_rsync_all': not a valid identifier
SSH: EXEC: completed after 201 ms
SSH: Disconnecting configuration [sz_platform01] ... 

# 确认问题,在jenkins服务器中模拟问题,确实报错了,但是在目标服务器 1.1.1.1中root切换到apache直接执行代码是没有问题的
猜测可能是jenkins发布代码时使用了 source /etc/profile 导致环境变量不同引发的问题

[apache@dev9_50:/root]$ ssh -p2025 apache@1.1.1.1 "/bin/sh /usr/local/worksh/jenkins_rsync/rsync_cc-product-api_cn.sh all"
apache@1.1.1.1's password: 
/usr/local/worksh/jenkins_rsync/rsync_cc-product-api_cn.sh: line 29: `product-api_rsync_all': not a valid identifier
[apache@dev9_50:/root]$ 


# 具体的脚本如下
[root@sz_platform_web01:~]# cat /usr/local/worksh/jenkins_rsync/rsync_cc-product-api_cn.sh
#!/bin/bash

##############jenkins拉取git代码同步到 1.1.1.1_sz_platform_web01 上,然后触发 1.1.1.1_sz_platform_web01 上的该脚本进行 szali 内网机房同步####################
###### rsync product-api.chinasoft.com data ######################################
passwd="/data/www/.rsync/rsyncd.pass1"
exclude_list="--exclude=logs/ --exclude=cache/ --exclude=.git --exclude=.gitignore --exclude=.gitattributes --exclude=store_data/ --exclude=invoice_file/"


# szali_product_api_ip

szali_product_api_ip_list="172.18.54.136"


#############################################################

# 同步所有机房
function product-api_rsync_all()
{
# rsync szali_product-api
for szali_product_api_ip in $szali_product_api_ip_list
do    
    echo "####################rsync szali_product-api start################################"
        echo $szali_product_api_ip
    rsync -zavP --delete $exclude_list --password-file=$passwd /data/www/vhosts/product-api.chinasoft.com/ apache@$szali_product_api_ip::apache/data/www/vhosts/product-api.chinasoft.com/
    echo "################### rsync szali_product-api end #######################"
done


}

# 只同步深圳机房
function product-api_rsync_szali()
{
# rsync szali_product-api
for szali_product_api_ip in $szali_product_api_ip_list
do    
    echo "####################rsync szali_product-api start################################"
        echo $szali_product_api_ip
    rsync -zavP --delete $exclude_list --password-file=$passwd /data/www/vhosts/product-api.chinasoft.com/ apache@$szali_product_api_ip::apache/data/www/vhosts/product-api.chinasoft.com/
    echo "################### rsync szali_product-api end #######################"
done
}



#####################  MAIN  ###############################
usage () {
        echo ""
        echo "  Please Input server infomation!"
        echo ""
        echo "  USAGE: `basename $0` [all|product-api_szali]"
        echo ""
}
    
if [ $# != 1 ]
then
        usage >&2
        exit 1
fi
OPT=$1
case $OPT in
all)
        echo "start rsync `basename $0` to all apiservice servers"
        product-api_rsync_all
        ;;
apiservice_szali)
        echo "start rsync `basename $0` to szali_product-api_servers"
        product-api_rsync_szali
    ;;

*)
        echo "Usage:`basename $0` [all|product-api_szali]"
        ;;
esac


# 经过反复修改,发现是中划线的问题,将函数名统一更换为下划线即可_
product-api_rsync_all --> 修改为 product_api_rsync_all

product-api_rsync_szali --> 修改为 product_api_rsync_szali

猜你喜欢

转载自www.cnblogs.com/reblue520/p/11252506.html
今日推荐