• 【shell】shell编程总结


      总结一下在写shell脚本时的常见注意事项:

    1.shell脚本中的命令最好用命令的全路径,如果不知道全路径可以用which cmd查找命令的全路径。

    2.shell脚本中定义环境变量用export xxx=/dir1/dir2.....

    3.shell脚本中取变量所以变量前都需加$,或者最好是${变量}

    4.掌握常见的if、for、case语法的使用方法

    5.shell脚本中最好写清楚注释

    6. shell脚本中善于使用函数

    7.用 $? 来判断上一个shell命令的执行结果,返回值是0代表正常结束,返回值是其他则代表不正常

    8. 将一些命令的执行结果重定向到/dev/null,错误结果也需要重定向到/dev/null (/dev/null是linux的无底洞,我们丢进去的东西就都找不回来了)

    9.善于使用ps 加grep命令判断一些服务是否启动,根据是否有相应的进程判断对应的服务是否启动。

    10 善于使用awk命令提取一些需要的信息

    killUser.sh  (输入登录的用户名,然后强制退出用户的脚本)

    #!/bin/bash
    #input username and kill relactive process for kill user
    echo "please input username for kill"
    read username
    #if username is root ,exit 
    if [ ${username} = 'root' ]
            then
            echo "root can not kill"
            exit 1
    fi
    #get user PID
    PID=`/usr/bin/ps -aux | /usr/bin/grep qlq | /usr/bin/awk '$1="qlq" {print $2}'`
    for killpid in $PID
    do
            kill -9 $killpid
    done
    echo "killed ok!"

    tomcat.sh    (测试tomcat服务是否启动,开启、关闭tomcat)

    #!/bin/bash
    #chkconfig: 2345 80 90
    #description:tomcat
    #input(start stop status) to operate tomcat service
    #start funciton(start tomcat service use /opt/apache-tomcat/apache-tomcat-7.0.72/bin/start.sh)
    export CATALINA_HOME=/opt/apache-tomcat/apache-tomcat-7.0.72
    start(){
            /usr/bin/sh "${CATALINA_HOME}"/bin/startup.sh
            if [ "$?" != "0" ]
            then 
                    echo "service is not success start"
            else
                    echo "service is success start"
    
                                                            fi
            exit 1
    }
    #stop function
    stop(){
            /usr/bin/sh "${CATALINA_HOME}"/bin/shutdown.sh
            if [ "$?" != "0" ]
            then 
                    echo "service is not success stop"
            else
                    echo "service is success stop"
    
                                                            fi
    }
    #status function
    status(){
            /usr/bin/ps -le | /usr/bin/grep java >/dev/null 2> /dev/null
            if [ "$?" != "0" ]
            then 
                    echo "service is not start"
            else
                    echo "service is running"
    
                                                    fi
    }
    #read input and dispose function
    input=${1}
    case ${input} in
    start)
            start
            ;;
    stop)
            stop
            ;;
    status)
            status
            ;;
    *)
            echo "please use {start to start tomcat,stop to stop tomcat,status to read tomcat status!}"
    esac

    webmin.ssh  (测试webmin服务是否启动、开启、关闭webmin服务)

    #!/bin/bash
    #start webmin service
    start()
    {
    /etc/webmin/start >/dev/null 2> /dev/null
    if [ $? = '0' ] 
            then 
            echo "webmin is success start"
    fi
    }
    
    #stop webmin service
    stop()
    {
    /etc/webmin/stop > /dev/null 2> /dev/null
    if [ $? = '0' ] 
            then 
            echo "webmin is success stop"
    fi
    }
    
    #status webmin
    status()
    {
       /usr/bin/netstat -ano | /usr/bin/grep 10000 > /dev/null
       if [ $? != '0' ]
            then
            echo "webmin is not start"
       else
            echo "webmin is running"
        fi
    }
    
    ########read input##############
    str=$1
    if [ ${str} = 'start' ]
            then
            start
    elif [ ${str} = 'stop' ]
            then
            stop
    elif [ ${str} = 'status' ]
            then
            status
    else
            echo "please use {start,stop,status}"
    fi

    addUserBatch.sh     (批量添加用户的脚本)

    #!/bin/bash
    #adduser batch
    echo "please input username:"
    read username
    echo "please input number to create:"
    read number
    #start to create user
    for(( i=1;i<="${number}";i++ ))
    do
            /usr/sbin/adduser "${username}${i}" > /dev/null 2> /dev/null
    done
    #add finished
    echo "add OK!"
    echo "please input passwd for users"
    read password
    for(( i=1;i<="${number}";i++ ))
    do
            /usr/sbin/usermod -p "${password}" "${username}${i}"  > /dev/null 2> /dev/null
    done

    delUserBatch.sh      (批量删除用户的脚本)

    #!/bin/bash
    #delete user batch
    echo "please input username word to delete"
    read word
    #get All users like word*
    users=`/usr/bin/grep ${word} /etc/passwd | awk -F: -v word1=${word} 'index($1,word1)>0 {print $1}'`
    if [ "${users}" = '' ]
     then
            echo "user is does not exist!"
            exit 1
    fi
    for username in ${users}
    do
            /usr/sbin/userdel -rf ${username} > /dev/null 2>/dev/null
    done
    if [ "0" = "$?" ]
            then
            echo "delete ok!"
    else
            echo "delete failed!"
    fi
  • 相关阅读:
    PyQt(Python+Qt)学习随笔:使用pyqtConfigure建立信号和槽的连接
    PyQt(Python+Qt)学习随笔:调用disconnect进行信号连接断开时的信号签名与断开参数的匹配要求
    PyQt(Python+Qt)学习随笔:什么是信号绑定(Unbound and Bound Signals)?
    PyQt(Python+Qt)学习随笔:信号签名(signature of the signal)是什么?
    第六章、信号和槽进阶--自定义信号及其他信号、槽的高级特性
    第15.19节 PyQt(Python+Qt)入门学习:自定义信号与槽连接
    第五章、信号和槽的实战应用--一个计算器的实现
    第四章 、PyQt中的信号(signal)和槽(slot)机制以及Designer中的使用
    第三章 、使用Qt Designer进行GUI设计
    织梦通过 phpmyadmin 导出的数据,再次导入的时候报错
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/8862168.html
Copyright © 2020-2023  润新知