Shell变量的作用域

今天在测试shell脚本的时候发现在函数中设置了变量后,居然可以在另外的函数中直接调用该变量

#!/bin/bash

METHOD=$1
BRANCH=$2
GROUP_LIST=$3

function IP_list(){
  if [[ ${GROUP_LIST} == "GROUP1" ]];then
     Server_IP="192.168.0.142"              //可以看到这里变量Server_IP作用于IP_list这个函数
     echo ${Server_IP}
     #ssh root@172.31.0.103 ""echo disable  server linux39-web-80-master/172.31.0.105" | socat stdio /run/haproxy/admin.sock"
     #ssh root@172.31.0.104 ""echo disable  server linux39-web-80-master/172.31.0.105" | socat stdio /run/haproxy/admin.sock"
  elif [[ ${GROUP_LIST} == "GROUP2" ]];then
     Server_IP="192.168.0.79"
     echo ${Server_IP}
     #从调度器下线后端服务器,如果调度器开启了多进程,则需要从每一个进程都下线
     #ssh root@172.31.0.103 ""echo enable  server linux39-web-80-master/172.31.0.105" | socat stdio /run/haproxy/admin.sock"
     #ssh root@172.31.0.104 ""echo enable  server linux39-web-80-master/172.31.0.105" | socat stdio /run/haproxy/admin.sock"
  elif [[ ${GROUP_LIST} == "GROUP3" ]];then
     Server_IP="192.168.0.142 192.168.0.79"
     echo ${Server_IP}
  fi
}

function clone_code(){
  cd /data/git/linux39/ && rm -rf web1
  git clone -b ${BRANCH} git@159.138.5.152:linux39/web1.git
  echo "代码clone完成"
}

function stop_tomcat(){
   for node in ${Server_IP};do //这里居然直接调用了,真的不可思议 ssh www@${node} "/opt/apps/tomcat.sh start" done } main(){ case $1 in deploy) IP_list; clone_code; #scanner_code; #make_zip; #down_node; stop_tomcat; #scp_zipfile; #start_tomcat; #web_test; #delete_history_version; ;; # rollback_last_version) # IP_list; #echo ${Server_IP} # down_node; # stop_tomcat; # rollback_last_version; # start_tomcat; # web_test; # ;; esac } main $1 $2 $3

 于是百度了一番

http://c.biancheng.net/view/773.html

这里说Shell 函数和 C++、Java、C# 等其他编程语言函数的一个不同点就是:在 Shell 函数中定义的变量默认也是全局变量,它和在函数外部定义变量拥有一样的效果。

要想变量的作用域仅限于函数内部,可以在定义时加上local命令,此时该变量就成了局部变量。请看下面的代码:

#!/bin/bash

#定义函数
function func(){
    local a=99
}

#调用函数
func

#输出函数内部的变量
echo $a

 于是修改脚本

function IP_list(){
  if [[ ${GROUP_LIST} == "GROUP1" ]];then
     local Server_IP="192.168.0.142"
     echo ${Server_IP}
     #ssh root@172.31.0.103 ""echo disable  server linux39-web-80-master/172.31.0.105" | socat stdio /run/haproxy/admin.sock"
     #ssh root@172.31.0.104 ""echo disable  server linux39-web-80-master/172.31.0.105" | socat stdio /run/haproxy/admin.sock"
  elif [[ ${GROUP_LIST} == "GROUP2" ]];then
     local Server_IP="192.168.0.79"
     echo ${Server_IP}
     #从调度器下线后端服务器,如果调度器开启了多进程,则需要从每一个进程都下线
     #ssh root@172.31.0.103 ""echo enable  server linux39-web-80-master/172.31.0.105" | socat stdio /run/haproxy/admin.sock"
     #ssh root@172.31.0.104 ""echo enable  server linux39-web-80-master/172.31.0.105" | socat stdio /run/haproxy/admin.sock"
  elif [[ ${GROUP_LIST} == "GROUP3" ]];then
     local Server_IP="192.168.0.142 192.168.0.79"
     echo ${Server_IP}
  fi
}

测试执行

]# ./test.sh deploy develop GROUP2

192.168.0.79
Cloning into 'web1'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 14 (delta 4), reused 0 (delta 0)
Receiving objects: 100% (14/14), done.
Resolving deltas: 100% (4/4), done.
代码clone完成

果然没有执行 stop_tomcat()

 

posted @ 2021-12-29 22:49  不会跳舞的胖子  阅读(167)  评论(0编辑  收藏  举报