zoukankan      html  css  js  c++  java
  • shell 函数

    1.如何定义函数和调用函数

    fun01 () { echo "Hello World"; } 
    fun01
    

    2.如何给函数传递一个参数

    fun01 () { echo "Hello $1"; } 
    fun01 SHell			#执行时跟上一个固定的参数
    fun01 $1			#执行时跟上一个不固定的参数    (脚本的位置参数,需要执行脚本时传递)
    

    3.函数接收N多个参数传递

    fun01 () { echo "Hello $*"; } 
    fun01 liunx shell  Python  Test
    

    4.函数传参

    第一种方式
    	fun01 () {
    		echo "Hello $1"
    	}
    
    #脚本的位置参数
    fun01 $1
    

    需求1,写一个脚本,该脚本可以实现计算器的功能,可以进行 +- * / 四种计算。

    例如: sh cal.sh 30 + 40 | sh cal.sh 30 - 40 | sh cal.sh 30 * 40 | sh cal.sh 30 / 40
    [root@manager functions]# cat fun02.sh 
    fun () {
    	case $2 in
    		+)
    			echo $1 + $3 = $[ $1 + $3 ]
    			;;
    		-)
    			echo $1 - $3 = $[ $1 - $3 ]
    			;;
    		x)
    			echo $1 x $3 = $[ $1 * $3 ]
    			;;		
    		/)
    			echo $1 / $3 = $[ $1 / $3 ]
    			;;
    	esac
    }
    	fun $1 $2 $3
    
    

    需求2:写一个脚本,实现nginx服务的启动、停止、重启 (简单实现,在判断阶段使用函数来做。)

    1.先实现启动和停止
    2.在优化脚本
    
    [root@manager functions]# cat fun05.sh 
    #!/bin/bash
    
    Author:      Oldux.com QQ: 552408925
    
    Date:       2019-11-04
    
    FileName:   fun05.sh
    
    Description: 
    
    ngx_status=$1
    
    nginx_is_status () {
    
    systemctl $ngx_status nginx
    if [ $? -eq 0 ];then
    	echo "nginx is $ngx_status"
    else
    	echo "nginx is $ngx_stauts err"
    	exit 1
    fi
    
    }
    
    case $ngx_status in
    	start)
    		nginx_is_status
    		;;
    
    stop)
    	nginx_is_status
    ;;
    *)
    	echo "USAGE: $0 { start | stop | restart }"
    	exit
    
    esac
    

    函数实例脚本: echo

    echo "The 1 user is : root"
    echo "The 2 user is : bin"
    
    [root@manager functions]# cat fun06.sh 
    #!/bin/bash
    
    Date:       2019-11-04
    
    FileName:   fun06.sh
    
    Description: 
    
    get_users () {
    	user=$(awk -F ":" '{print $1}' /etc/passwd)
    	echo $user
    }
    
    #将函数输出的结果,从新赋值给user_list变量存储起来
    user_list=$(get_users)
    
    index=1
    for u in ${user_list}
    do
    	echo "The $index user is : $u"
    	let index++
    done
    
    函数实例脚本: return
    

    函数实例脚本: return

    [root@manager functions]# cat fun07.sh 
    #!/bin/bash
    
    Date:       2019-11-04
    
    FileName:   fun07.sh
    
    Description: 
    
    file=$1            #定义文件
    
    t_file(){                   #函数判断
        if [ -f $file ];then
            return 0
        else
            return 1
        fi
    }
    
    $?
    t_file
    rc=$?
    
    if [ $rc -eq 0 ];then
    	echo "该文件存在 $file"
    else
    	echo "该文件不存在 $file"
    
    fi
    
    [root@manager functions]# cat fun08.sh 
    #!/bin/bash
    
    Date:       2019-11-04
    
    FileName:   fun08.sh
    
    Description: 
    
    fun () {
    	systemctl status nginx &>/dev/null
    
    if [ $? -eq 0 ];then
    	return 0
    else
    	return 100
    fi
    
    }
    fun
    echo $?
    
    retrun: 主要控制函数的返回值   可以理解是命令执行后的结果
    echo:  主要控制函数的返回数据
    
    [root@manager functions]# cat fun09.sh 
    #!/bin/bash
    
    Date:       2019-11-04
    
    FileName:   fun09.sh
    
    Description: 
    
    #根据用户传递的服务名称,获取不同服务的状态
    is_server_running(){
    	systemctl status $1 &>/dev/null
        if [ $? -eq 0 ];then
            return 0
        else
            return 1
        fi
    }
    #调用函数,并根据函数返回状态码进行输出
    is_server_running $1 && echo "$1 is Running" || echo "$1 is stoped
    

    需求3:使用函数、循环、case实现系统管理工具箱。

    Command action
    h 显示命令帮助
    f 显示磁盘分区
    d 显示磁盘挂载
    m 查看内存使用
    u 查看系统负载
    q 退出程序
    
    [root@manager functions]# cat fun10.sh 
    #!/bin/bash
    
    Date:       2019-11-04
    
    FileName:   fun10.sh
    
    Description: 
    
    meminfo () {
    
    cat <<-EOF
    
    Command action
    h 显示命令帮助
    f 显示磁盘分区
    d 显示磁盘挂载
    m 查看内存使用
    u 查看系统负载
    q 退出程序
    
    ---------------------------
    
    EOF
    }
    	meminfo
    
    while true
    do
    	read -p "请输出你要选择选项: " Action
    	case $Action in 
    		h)
    			help
    			;;
    		f)
    			lsblk
    			;;
    		d)
    			df -h
    			;;
    		m)
    			free -m
    			;;
    		u)
    			uptime
    			;;
    		q)
    			exit
    			;;
    		*)
    			continue
    
    esac
    
    done
    

    需求4:使用case、循环、函数、实现JumpServer跳板机功能。

    1.用户登陆该服务器则自动执行该脚本。 pass
    2.脚本提示可连接主机列表。
    3.该脚本无法直接退出。		pass
    
    [root@manager functions]# cat fun11.sh 
    #!/bin/bash
    
    Date:       2019-11-04
    
    FileName:   fun11.sh
    
    Description: 
    
    meminfo(){
    
    ​        cat <<-EOF
    
    ​        |       1) lb01-172.16.1.5      |
    ​        |       2) lb02-172.16.1.6      |
    ​        |       3) web01-172.16.1.7     |
    ​        |       4) web02-172.16.1.8     |
    
    ​        |       h) help                 |
    
    ​	EOF
    }
    ​	meminfo
    ​	trap "" HUP INT TSTP
    
    while true
    do
    	read -p "请输入你要登录的主机: " Action
    	
    
    case $Action in
    	1|lb01)
    		ssh root@172.16.1.5
    	;;
    	2|lb02)
    		ssh root@172.16.1.6
    	;;
    	3|web01)
    		ssh root@172.16.1.7
    	;;
    	4|web02)
    		ssh root@172.16.1.8
    	;;
    	h)
    		clear
    		meminfo
    	;;
    	exec)
    		exit
    		;;
    	*)
    		continue
    esac
    
    done
    

    需求5.case场景示例,实现多级菜单功能,需要使用到函数、case、循环、if判断、变量

    [root@ansible ~]# cat file25.sh 
    #!/bin/bash
    menu() {
    
    cat <<EOF
    
    菜 	单
    
    (1) Install Nginx
    (2) Install Mysql
    (3) Install PHP
    
    (4) quit
    
    EOF
    }
    menu2(){
    
    cat <<EOF
    
    版本
    
    (1) Install Nginx1.1
    (2) Install Nginx1.2
    (3) Install Nginx1.3
    
    (4) 返回上一级
    
    EOF
    }
    menu3(){
    
    cat <<EOF
    
    版本
    
    (1) Install mysql1.1
    (2) Install mysql1.2
    (3) Install mysql1.3
    
    (4) 返回上一级
    
    EOF
    }
    menu4(){
    
    cat <<EOF
    
    版本
    
    (1) Install php1.1
    (2) Install php1.2
    (3) Install php1.3
    
    (4) 返回上一级
    
    EOF
    }
    while true
    do
    menu
    read -p "请选择服务" Action
    case $Action in
    (1)
    menu2
    while true
    do
    read -p "请选择你的版本" Action1
    case $Action1 in
    (1)
    echo -e "33[35m nginx 1.1 is done33[0m"
    sleep 1
    echo -e "33[35m nginx 1.1 is ok33[0m"
    ;;
    (2)
    echo -e "33[35m nginx 1.2 is done33[0m"
    sleep 1
    echo -e "33[35m nginx 1.2 is ok33[0m"
    ;;
    (3)
    echo -e "33[35m nginx 1.3 is done33[0m"
    sleep 1
    echo -e "33[35m nginx 1.3 is ok33[0m"
    ;;
    (4)
    clear
    break
    esac
    done
    ;;
    (2)
    menu3
    while true
    do
    read -p "请选择你的版本" Action1
    case $Action1 in
    (1)
    echo -e "33[35m mysql 1.1 is done33[0m"
    sleep 1
    echo -e "33[35m mysql 1.1 is ok33[0m"
    ;;
    (2)
    echo -e "33[35m mysql 1.2 is done33[0m"
    sleep 1
    echo -e "33[35m mysql 1.2 is ok33[0m"
    ;;
    (3)
    echo -e "33[35m mysql 1.3 is done33[0m"
    sleep 1
    echo -e "33[35m mysql 1.3 is ok33[0m"
    ;;
    (4)
    clear
    break
    esac
    done
    ;;
    (3)
    menu4
    while true
    do
    read -p "请选择你的版本" Action1
    case $Action1 in
    (1)
    echo -e "33[35m php 1.1 is done33[0m"
    sleep 1
    echo -e "33[35m php 1.1 is ok33[0m"
    continue
    ;;
    (2)
    echo -e "33[35m php 1.2 is done33[0m"
    sleep 1
    echo -e "33[35m php 1.2 is ok33[0m"
    ;;
    (3)
    echo -e "33[35m php 1.3 is done33[0m"
    sleep 1
    echo -e "33[35m php 1.3 is ok33[0m"
    ;;
    (4)
    clear
    break
    esac
    done
    ;;
    (4)
    exit
    ;;
    (*)
    clear
    continue
    esac
    done
    
  • 相关阅读:
    SDOI2008 Sandy的卡片
    BZOJ2555 Substring
    CTSC2012 熟悉的文章
    递增
    丢失的牛
    【模板】点分治
    陌上花开(三维偏序)
    Holes(河鼠入洞)
    弹飞河鼠
    树状数组1
  • 原文地址:https://www.cnblogs.com/baozexu/p/11794168.html
Copyright © 2011-2022 走看看