學以致用二十三-----shell腳本里呼叫指令碼
阿新 • • 發佈:2018-11-09
當前指令碼可以呼叫其他目錄下的指令碼,並可以直接使用其他腳本里的函式。
首先檢視指令碼目錄
執行net_set.sh,同時執行colos.sh 並可直接使用 color.sh中的函式
net_set.sh 注意紅色方框和綠色方框部分
Echo_green為 color.sh的函式
程式碼部分
net_set.sh
1 #!/usr/bin/bash 2 #lion 3 #2018-10-19 4 . script/color.sh 5 net_path=/etc/sysconfig/network-scripts/ 6 net_file=$(ls ${net_path}| grep ifcfg | awk 'NR==1{print}') 7 net_file_name=$(ls ${net_path} | grep ifcfg | awk 'NR==1{print}'| cut -c 7-) 8 ip_filed=$(ifconfig ${net_file_name} | grep netmask) 9 ip=$(ifconfig ${net_file_name} | grep inet | grep -v inet6 | awk '{print $2}') 10 if [ -z "$ip_filed" ];then 11 sed -i 's/ONBOOT=no/ONBOOT=yes/g' ${net_path}${net_file}12 service network restart 13 else 14 Echo_green "The ip is valid:"${ip} 15 fi
color.sh
1 #!/usr/bin/bash 2 #2018-10-18 3 #lion 4 #color set up 5 6 Color_text() 7 { 8 echo -e "\e[1;$2m$1\e[0m" 9 } 10 Echo_gray() 11 { 12 echo $(Color_text "$1" "30") 13 } 14 Echo_red()15 { 16 echo $(Color_text "$1" "31") 17 } 18 Echo_green() 19 { 20 echo $(Color_text "$1" "32") 21 } 22 Echo_yellow() 23 { 24 echo $(Color_text "$1" "33") 25 } 26 Echo_blue() 27 { 28 echo $(Color_text "$1" "34") 29 } 30 Echo_pink() 31 { 32 echo $(Color_text "$1" "35") 33 } 34 Echo_aqua() 35 { 36 echo $(Color_text "$1" "36") 37 } 38 Echo_white() 39 { 40 echo $(Color_text "$1" "37") 41 }