shell中的函数

梦想的初衷 提交于 2020-02-16 19:11:42

函数:功能, function

作用:代码重用

库:so

function: 功能
结构化编程,不能独立运行,需要调用时执行,可以被多次调用

定义一个函数:
function FUNCNAME {
  command
}

or

FUNCNAME() {
  command
}
举例:
#!/bin/bash
cat << EOF
d|D)show disk usages
m|M)show memory usages
s|S)show swap usages
q|Q)quit.
EOF
read -p "Your choice:" CHOICE
until [ $CHOICE == "q" -o $CHOICE == "Q" ];do
case $CHOICE in
d|D)df -h;;
m|M)free -m|grep "^Mem" ;;
s|S)free -m|grep "^Swap" ;;
*)
cat << EOF
d|D)show disk usages
m|M)show memory usages
s|S)show swap usages
q|Q)quit
EOF
read -p "Your choice:" CHOICE
esac
cat << EOF
d|D)show disk usages
m|M)show memory usages
s|S)show swap usages
q|Q)quit
EOF
read -p "Your choice:" CHOICE
done

使用函数
#!/bin/bash
function showmenu {
cat << EOF
d|D)show disk usages
m|M)show memory usages
s|S)show swap usages
q|Q)quit.
EOF
}
showmenu
read -p "Your choice:" CHOICE
until [ $CHOICE == "q" -o $CHOICE == "Q" ];do
case $CHOICE in
d|D)df -h;;
m|M)free -m|grep "^Mem" ;;
s|S)free -m|grep "^Swap" ;;
*)
showmenu
read -p "Your choice:" CHOICE
esac
showmenu
read -p "Your choice:" CHOICE
done
或
#!/bin/bash
showmenu () {
cat << EOF
d|D)show disk usages
m|M)show memory usages
s|S)show swap usages
q|Q)quit.
EOF
}
showmenu
read -p "Your choice:" CHOICE
until [ $CHOICE == "q" -o $CHOICE == "Q" ];do
case $CHOICE in
d|D)df -h;;
m|M)free -m|grep "^Mem" ;;
s|S)free -m|grep "^Swap" ;;
*)
showmenu
read -p "Your choice:" CHOICE
esac
showmenu
read -p "Your choice:" CHOICE
done
引用函数执行结果

#!/bin/bash
#
TWOINT () {
A=9
B=7
C=$[$A+$B]
echo $C
}
M=11
SUM=$[$M+`TWOINT`]
echo $SUM

引用函数执状态结果:

#!/bin/bash
#
TWOINT () {
A=9
B=7
C=$[$A+$B]
echo $C
}
M=11
SUM=$[$M+`TWOINT`]
echo $SUM
echo $?

自定义执行状态返回值:

return #
0-255
测试是否添加用户
#!/bin/bash
ADDUSER() {
USERNAME=hadoop
if ! id -u $USERNAME &> /dev/null;then
   useradd $USERNAME
   echo $USERNAME |passwd --stdin $USERNAME &> /dev/null
   return 0
else
   return 1
fi
}

接受参数的函数:
./a.sh m n 
$1: m
$2: n

#!/bin/bash
#
TWOINT () {
echo $[$1+$2]
}
TWOINT 5 6
SUM=`TWOINT 5 6`
echo $SUM

TWOINT 5 6
$1: 5
$2: 6

函数复用
#!/bin/bash
#
TWOINT () {
echo $[$1+$2]
}
for I in {1..10};do
  let J=$[$I+1]
  TWOINT $I $J
done

#!/bin/bash
#
TWOINT () {
echo $[$1+$2]
}
for I in {1..10};do
  let J=$[$I+1]
  echo "$I+$J=`TWOINT $I $J`"
done
#!/bin/bash
ADDUSER(){
USERNAME=$1
if ! id -u $USERNAME &> /dev/null;then
   useradd $USERNAME
   echo $USERNAME |passwd --stdin $USERNAME &> /dev/null
   return 0
else
   return 1
fi
}
for I in {1..10};do
ADDUSER user$I
if [ $? -eq 0 ];then
   echo "add user user$I finished."
else
  echo "add user$I Failure"
fi
done

练习

写一个脚本,判定192.168.0.200-192.168.0.254之间的主机哪些在线。要求:
1、使用函数来实现一台主机的判定过程;
2、在主程序中来调用此函数判定指定范围内的所有主机的在线情况。

#!/bin/bash
#
PING() {
  for I in {200..254};do
    if ping -c 1 -W 1 192.168.0.$I &> /dev/null; then
      echo "192.168.0.$I is up."
    else
      echo "192.168.0.$I is down."
    fi
  done
}

PING
#!/bin/bash
#
PING() {
    if ping -c 1 -W 1 $1 &> /dev/null; then
      echo "$1 is up."
    else
      echo "$1 is down."
    fi
}

for I in {200..254}; do
  PING 192.168.0.$I
done


#!/bin/bash
#
PING() {
    if ping -c 1 -W 1 $1 &> /dev/null; then
      return 0
    else
      return 1
    fi
}

for I in {200..254}; do
  PING 192.168.0.$I
  if [ $? -eq 0 ]; then
    echo "192.168.0.$I is up."
  else
    echo "192.168.0.$I is down."
  fi
done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!