zoukankan      html  css  js  c++  java
  • 【shell学习4》》】系统化整理大纲

    之前看的runnoob整理,细节太多也没有系统起来,昨天公交上看了一些视频,略作总结:

    标题零:学习基础
    //创建文件
    touch testVar.sh
    //vim编辑内容
    #!/bin/bash
    vari="hello"
    var1="my"
    var2="pat"
    //运行的两种方式
    方式一:
    root@ubuntu:~/shell# sh testVar.sh
    root@ubuntu:~/shell# echo $var1
    结果(空):
    方式二
    root@ubuntu:~/shell# . ./testVar.sh
    root@ubuntu:~/shell# echo $var1
    结果:my

    区别:通过pstree命令查看bash进程
    sh启动会新建一条bash进程,本bash进程无法取到结果


    选择:通常运行直接sh,脚本相互调用时使用. ./
          也可传参解决



    标题一:变量定义
    分为系统变量和用户自定义变量

    a、
    root@ubuntu:~/shell# va=`pwd`
    root@ubuntu:~/shell# echo $va
    /root/shell
    b、
    root@ubuntu:~/shell# va2=$(ll)
    root@ubuntu:~/shell# echo $va2
    total 12 drwxr-xr-x 2 root root 4096 Jun 22 19:11 ./ drwx------ 4 root root 4096 Jun 22 19:11 ../ -rw-r--r-- 1 root root 46 Jun 22 19:09 testVar.sh

    1、系统内置变量
    $HOME(主目录)$PWD(当前运行的目录)$SHELL(当前的解释器)$USER(当前用户)等等。
    使用set命令查看(各个发行版本命令有所区别)

    2、用户自定义变量

    1、变量名=变量值(=号两边不能有空格)
    2、变量名一般都大写
    3、双引号与单引号区别,双引号会将空格脱意,单引号会将所有特殊字符脱意($失去作用)
    4、unset 变量名 删除变量
    5、readonly A=1 声明静态变量A,不能unset,值也无法被改变
    6、export 变量名,将变量提升为全局变量,可供其它shell使用
    7、上一条命令的值赋值给变量:

    3、系统特殊变量
    $? 表示上一个命令退出状态(返回值?并非输出)
    $$ 表示当前进程编号
    $0 表示当前脚本名称
    $n 脚本中表示n位置的参数
    内容:
    #!/bin/bash
    echo "param1 is $1"
    echo "param2 is $2"
    eg:
    root@ubuntu:~/shell# sh testVar.sh aaa bbb
    param1 is aaa
    param2 is bbb
    $# 表示变量个数,常用于循环(不常用)
    $*、$@都表示参数列表(常用)
    eg:
    #!/bin/bash
    echo "param1 is $1"
    echo "param2 is $2"
    echo '$* test'
    echo $*
    echo '$@ test'
    echo $@
    eg:
    root@ubuntu:~/shell# sh testVar.sh aaa bbb
    param1 is aaa
    param2 is bbb
    $* test
    aaa bbb
    $@ test
    aaa bbb
    eg:
    !/bin/bash
    echo "param1 is $1"
    echo "param2 is $2"
    echo '$* test'
    echo $*
    echo '$@ test'
    echo $@
    echo 'test for'
    for X in $*
    do
    echo $X
    done
    eg:
    root@ubuntu:~/shell# sh testVar.sh ccc ddd eee
    param1 is ccc
    param2 is ddd
    $* test
    ccc ddd eee
    $@ test
    ccc ddd eee
    test for
    ccc
    ddd
    eee


    标题二:表达式与流程控制:

    1、for

    eg:

    #!/bin/bash
    for J in 1 2 3 4 5
    do
      echo $J
    done

    for J in 6 7 8 9 10;
    do echo $J;
    done

    for J in {20..25};
    do echo $J;
    done

    #!/bin/bash

    for X in $*
    do
    echo $X
    done

    for((i=0;i<=5;i++))
    do
     echo $i
    done

    2、while

    eg:

    while [ 1 -gt 0 ]
     do echo 11
    done

    while [ 1 -gt 0 ]; do echo 11; done

    while (( 1<=1 )); do echo 11; done

    root@ubuntu:~/shell# K=2
    root@ubuntu:~/shell# while (( K<=10 )); do echo $K;let K++;  done


    3、case
    eg:
    #/bin/bash
    MM=$1
    case $MM in
    tingting)
            echo "to park"
    ;;
    fangfang)
            echo "to caffeBar"
    ;;
    weiwei)
            echo "to hotel"
    ;;
    *)
            echo "balabala~"
    esac

    root@ubuntu:~/shell# sh for.sh fangfang

    to caffeBar

    4、if
    判断条件:
    =   字符串对比
    //数字
    -lt 小于
    -le 小于等于
    -eq 等于
    -gt 大于
    -ge 大于等于
    -ne 不等于
    //其它
    -r 有读的权限
    -w 有写的权限
    -x 有执行权限
    -f 文件存在并且是个常规文件
    -s 文件存在且不为空
    -d 文件存在且是个目录
    -b 文件存在并且是一个块设备
    -L 文件存在且是一个链接

    eg:
    #/bin/bash
    read -p "input you name :" NAME
    if [ "$NAME" = "admin" ]
    then
    echo "1111"
    elif [ "$NAME" = "root" ]
    then
    echo "2222"
    else
    echo "3333"
    fi
    //一般比对后面加一个x防止空
    if [ "$basepath"x = "/usr/local/apache-tomcat-8.5.29"x ]
    //条件满足后执行
    [ condition ]&& echo ">>>>>>"
    root@ubuntu:~/shell# [ 1 = 1 ]&&echo ">>"
    >>
    root@ubuntu:~/shell# [ "1" = "2" ]&&echo ">>"||echo "<<"
    <<


    标题三:运算符

    格式 expr m + n  或 $((m+n))
    eg(空格导致结果不同):
    root@ubuntu:~/shell# S=`expr 1+2`
    root@ubuntu:~/shell# echo $S
    1+2
    root@ubuntu:~/shell# S=`expr 1 + 2`
    root@ubuntu:~/shell# echo $S
    3
    root@ubuntu:~/shell# S=$(((2+3)*4))
    root@ubuntu:~/shell# echo $S
    20

    标题四:函数
    三种声明方式
    function fname(){}
    function fname{}
    fname(){}
    返回值只能返回int(0-255)
    返回值只能$?取
    不能传参!
    无返回值默认返回最后一句有效语句
    先写后调用

    标题五:调试

    1sh -vx aa.sh
    2脚本中添加 set -x



    整理细节:
    //线程等待
    sleep 20
    //展示所有进程
    pstree
    //显示系统中所有变量
    set
    //重新执行文件中所有export命令
    source /etc/profile
    //split字符串
    root@ubuntu:~/shell# wc -c testVar.sh
    46 testVar.sh
    root@ubuntu:~/shell# wc -c testVar.sh | cut -d ' ' -f1
    46
    root@ubuntu:~/shell# wc -c testVar.sh | cut -d 'V' -f1
    46 test
    //引用变量
    $PATH
    //查看脚本的方法

    echo $(截取语句)
    //获取终端输入参数
    read -p(提示语句) -m(字符个数) -t(等待时间)

    root@ubuntu:~/shell# read -p  "what is your name:" NAME
    what is your name:asd
    root@ubuntu:~/shell# echo $NAME
    asd

  • 相关阅读:
    高级数据类型--字典(dict)
    一些JS常用的方法
    怎样提高WebService的性能
    Microsoft Enterprise Library 5.0 系列(三)
    Microsoft Enterprise Library 5.0 系列(四)
    Expression Blend学习二UI布局
    Expression Blend学习四控件
    Expression Blend学习5控件
    Expression Blend学习动画基础
    WPF三维图形
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054118.html
Copyright © 2011-2022 走看看