学以致用二十三-----shell脚本里调用脚本

当前脚本可以调用其他目录下的脚本,并可以直接使用其他脚本里的函数。

首先查看脚本目录

执行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$1e[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 }
原文地址:https://www.cnblogs.com/liongong/p/9820399.html