ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

shell基础

2022-02-15 15:34:38  阅读:148  来源: 互联网

标签:shell 基础 echo usr 数组 array bash


shell是用户(user)与linux内核(kernel)沟通的桥梁,是一种解释性编程语言
shell功能:命令行解释器、启动程序、输入输出重定向、管道连接、文件名置换、变量维护、环境控制、shell编程

shell脚本就是将完成一个任务的所有命令按照执行的先后顺序、自上而下写入到一个文本文件中,然后给予执行权限
    vim nginx_install.sh
        nginx_install.sh
            #<1>定义脚本的执行环境
            #!/usr/bin/bash
            #<2>  #代表注释    #!指定执行解释器
            #<3>脚本信息
            #Author: gonglinzhi
            #Create time: 2022/02/11 17:00
            #Release: 1.0
            #Script description: nginx install script 
            
            # nginx install code
            yum -y install wget gcc pcre-devel zlib-devel
            wget http://nginx.org/download/nginx-1.16.0.tar.gz
            tar xf nginx-1.16.0.tar.gz
            cd nginx-1.16.0
            ./configure --prefix=/usr/local/nginx
            make -j 4
            make install
        #<4>脚本组成
        # 解释环境 
            #!/usr/bin/env bash | python | perl
            #!/usr/bin/bash
        # 注释说明
        # 执行代码
    #<5>运行脚本
        #1给执行权限
            chmod 700 nginx_install.sh
            ./nginx_install.sh
        #2解释器直接运行脚本
            bash nginx_install.sh

shell特殊符号
    ~    家目录
    !    执行历史命令
    !!    执行上一条命令
    $    变量取内容符 $var
    + - * / %    加减乘除取余
    &    后台执行
    *     通配符,匹配所有
    ?    通配符,匹配除回车以外的一个字符
    ;    命令间分割符
    |    管道符,前命令输出作为后命令输入 cat filename | grep "abc"
    \    转义字符
    ``    反引号,命令中执行命令 echo "Today is `date +%F`"
    ''    单引号,字符串,单引号不解释变量    echo '$USER' 输出 $USER
    ""    双引号,字符串,双引号解释变量    echo "$USER" 输出 root
    $?    上一条命令是否执行成功    echo $?
                    expr 7 + 3.8 &>/dev/null ; echo $?

vim创建脚本添加固定头部信息
    vim /etc/vimrc
        /etc/vimrc
            #!/usr/bin/bash
            autocmd BufNewFile
                *.sh,*.script exec ":call AddTitle()"
                function AddTitle()
                call setline(0, "#!/usr/bin/bash")
                call append(0, "############################")
                call append(1, "#Author: gonglinzhi")
                call append(2, "#Create time: ".strftime("%Y-%m-%d"))
                call append(3, "#Release: 1.0")
                call append(4, "#Script description:  ")
                call append(5, "############################")
    vim test.sh
        ############################
        #Author: gonglinzhi
        #Create time: 2022/02/11
        #Release: 1.0
        #Script description: 
        ############################

    例:自动化磁盘分区
        vim disk_partition
        disk_partition
            #!/usr/bin/bash
            fdisk /dev/sdb <<EOF
            n
            p
            3
            
            +500M
            w        
            EOF

shell运算
    1). expr 3 \* 2
    2). echo $((3*2))

shell格式化输出
    echo会将输入的字符串标准输出
    echo [命令选项] 字符串
        命令选项    -n    取消自动换行
            -e    对以下字符串做特殊处理
                \a    发出警告声
                \b    删除前一个字符
                \c    最后不加换行符
                \n     换行且光标移至行首    
    例:时间倒计输出
        count_down.sh
            #!/usr/bin/bash
            for time in `seq 9 -1 0`;do
                echo -n -e "\b$time"
                sleep 1
            done

shell颜色代码
    格式:echo -e "\033[字背景颜色; 文字颜色m字符串\033[0m"
    例:echo -e "\033[41; 36m nobody is body \033[0m"

shell基本输入
    read 命令选项    接受键盘的输入,回车代表输入结束
        -p    打印信息
        -t    限定时间
        -s    不回显(密码不显示输入)
        -n    输入字符个数
    account_password.sh
        #!/usr/bin/bash
        clear
        read -p "Login: " acc
        echo -n -e "Password: "
        read -s -t60 -n8 pw        # 不回显,输入限定时间60秒,最多输入8个字符
        echo
        echo "account: $acc           password: $pw"
    login.sh
        #!/usr/bin/bash
        clear
        echo "Centos Linux 7 (Core)"
        echo -e "kernel `uname -r` an `uname` -m\n"
        echo -n -e "$HOSTNAME login: "
        read acc
        read -s -p "password: "
        read pw

shell变量
    name="jack"
    name(逻辑地址) <==>0x5...0x8(物理地址) <==> 存储数据"jack"

    变量分类 
        本地变量:用户私有变量,保存在家目录下的.bash_profile .bashrc文件
        全局变量:所有用户都可使用,保存在/etc/profile /etc/bashrc
        自定义变量:用户自定义,如脚本变量
    定义变量     变量名=值
        name="mary"    #变量名、等号、值之间不能有空格
    读取变量     $变量名
        echo $name
    取消变量    unset 变量名
        unset name
    定义本地变量
        LIKE="swim"
    定义全局变量/etc/profile
        export COUNTRY="China"
    永久变量(定义在~/.bash_profile)

shell数组
    基本数组
        定义数组:数组名称=(元素1 元素2 元素3)
            array=("mary" "jack" "pony" "tom")
        读取数组:${数组名称[索引]}
            echo ${array[0]}
        数组赋值:数组名称[索引]=值
            array[4]="kner"
            array=(`cat /etc/passwd`)
        查看数组:declare -a
        访问数组:echo ${array[0]}    访问数组中第一个元素
            echo ${array[@]}    访问数组中所有元素,等同于echo ${array[*]}
            echo ${#array[@]}    统计数组元素个数
            echo ${!array[@]}    获取数组元素的索引
            echo ${array[@]:2}    从索引2开始访问元素
            echo ${array[@]:1:2}     从索引1开始访问两个元素
        遍历数组:echo ${array[0]}
            echo ${array[1]}
            echo ${array[2]}
            echo ${array[3]}
    关联数组
        1.声明关联数组
            declare -A ass_array
        2.关联数组赋值
            ass_array[name]="liangkuo"
            ass_array[age]=18
            declare -A acc_array=([name]="fengdu" [age]=20 [country]="China")
        3.访问数组
            echo ${ass_array[name]}
            echo ${acc_array[name]}


shell数学比较运算(对浮点数运算友好)
    -eq    等于
    -gt    大于
    -lt    小于
    -ge     大于等于
    -le     小于等于
    -ne    不等于
    test 10  -eq 5;echo $?
    test 10  -gt 5;echo $?
    test 10  -lt 5;echo $?
    test 10  -ge 5;echo $?
    test 10  -le 5;echo $?
    test 10  -ne 5;echo $?
    calculate.sh
        num1=`echo "1.5*10"|bc|cut -d "." -f1`
        num2=$((2*10))
        test $num1 -gt $num2;echo $?

shell文件比较与检查
    -d    检查文件是否存在且为目录
    -e    检查文件是否存在
    -f    检查文件是否存在且为文件
    -r    检查文件是否存在且可读
    -s    检查文件是否存在且不为空
    -w    检查文件是否存在且可写
    -x    检查文件是否存在且可执行
    -O    检查文件是否存在且被当前用户拥有    
    -G    检查文件是否存在且默认组为当前用户组
    file1 -nt file2    比较file1是否比file2新
    file1 -ot file2    比较file1是否比file2旧
    test -d /tmp/abc;echo $?
-    test myfile.txt -nt youfile.txt;echo $?
    
shell字符串比较运算
    ==    等于
    !=    不等于
    -n     检查字符串是否不为空
    -z    检查字符串是否为空
    test $USER=="root";echo $?
    test -n $abc;echo $?

shell逻辑运算    
    &&    逻辑与运算
    ||    逻辑或运算
    !    逻辑非运算
    if [ 2 -eq 2 ] && [ 2 -eq 3 ];then echo "True";else echo "False"; fi

shell赋值运算
    =    赋值

shell条件语句
    if语句
        if [condition]
            then
                commands
        fi
            #!/usr/bin/bash
            if [ $USER != "root"]
                then
                    echo "ERROR: need to be root so that"
                    exit 1
            if
    if-else语句
        if [condition]
            then
                commands
        else
            commands
        fi
            #!/usr/bin/bash
            if [ $USER == "root"]
                then
                    echo "he is root"
            else
                echo "he is guest"
            if
    if-elif-else语句
        if [condition]
            then
                commands
        elif [condition]
            then
                commands
        ......
        else
            commands
        fi
            #!/usr/bin/bash
            if [ $1 -eq $2 ]
                then
                    echo "$1=$2"
            elif [ $1 -gt $2 ]
                then
                    echo "$1>$2"
            else
                echo "$1<$2"
            fi
    
(( conditions )) 双圆括号:条件中可植入数学表达式
    #!/usr/bin/bash 
    if (( 100%3+1>10 ));then
        echo "True"
    else
        echo "False"
    fi
[[ conditions ]] 双方括号:条件中可使用通配符
    #!/usr/bin/bash 
    for i in r1 rr2 cc rr3
        do
            if [[ $i == r* ]];then
                echo $i
            fi
    done
    

shell流程控制-for循环语句
    for var in value1 value2 value3 ...
        do
            commands
    done
        #!/usr/bin/bash 
        for i in `seq 1 9`        // seq 9 -2 1 ==>  9 7 5 3 1
            do
                echo $i
        done

    for (( 变量;条件;自增减运算 ))
        do
            commands
    done
        #!/usr/bin/bash 
        for (( i=0,j=10;i<10;i++,j-- ))
            do
                echo $i,$j
        done

 

标签:shell,基础,echo,usr,数组,array,bash
来源: https://www.cnblogs.com/glz666/p/15896681.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有