linux学习笔记三

41、字符串测试:
   -n "字符串"    :若字符串长度不为0,则为真,no zero
   -z "字符串"    :若字符串长度为0,则为真,zero
   "串1" = "串2"  :串1等于串2,则为真,可用==替代
   "串1" != "串2" :串1不等于串2,则为真,不能用!==替代
    root@anLA7856:/home/anla7856# [ -n "abc" ] && echo 1 || echo 0       #字符串长度不为0,则输出1
    1
    root@anLA7856:/home/anla7856# [ 0 -n "abc" ] && echo 1 || echo 0
    bash: [: -n: 需要二元表达式
    0
    root@anLA7856:/home/anla7856# test -n "abc" && echo 1 || echo 0
    1
    root@anLA7856:/home/anla7856# test -n "" && echo 1 || echo 0
    0
    root@anLA7856:/home/anla7856# var="anla7856"
    root@anLA7856:/home/anla7856# [ -n "$var" ] && echo 1 || echo 0
    1
    root@anLA7856:/home/anla7856# [ -z "$var" ] && echo 1 || echo 0           #变量长度为0,则为真
    0
    root@anLA7856:/home/anla7856# [ "abc" = "$var" ] && echo 1 || echo 0         #可以用==替代
    0
    root@anLA7856:/home/anla7856# [ "abc"="$var" ] && echo 1 || echo 0           #等号两边要有空格,否则会有问题
    1
    root@anLA7856:/home/anla7856# [ "abc" == "$var" ] && echo 1 || echo 0
    0
    root@anLA7856:/home/anla7856# [ "abc" != "$var" ] && echo 1 || echo 0             #不能用!==替代
    1
    root@anLA7856:/home/anla7856# [ "abc" !== "$var" ] && echo 1 || echo 0
    bash: [: !==: 需要二元表达式
    0

42、整数二元比较操作符:
    在[]和test中                   在(())和[[]]中
   -eq                            ==或=
   -ne                            !=
   -gt                            >
   -ge                            >=
   -lt                            <
   -le                            <=
   在工作场景中的整数比较,推荐使用[](类似于-eq的用法)

43、逻辑操作符:
    在[]和test中                   在(())和[[]]中
   -a                              &&
   -o                              ||
   !                               !
   连接两个含有[]、test或[[]]的表达式可用&&或||
    root@anLA7856:/home/anla7856# [ -f /etc/hosts -a -f /etc/services ] && echo 1 || echo 0
    1
    root@anLA7856:/home/anla7856# ((m<20||n>30)) && echo 1 || echo 0
    1
    root@anLA7856:/home/anla7856# m=21;n=38
    root@anLA7856:/home/anla7856# [ $m -gt 20 ] || [ $n -lt 30 ] && echo 1 || echo 0
    1

44、遍历某一个文件夹下面所有脚本,查看脚本中匹配文件的字符串:
    root@anLA7856:/home/anla7856# for n in `ls /etc/init.d/*`;do egrep -wn "# " $n && echo $n;done

45、if后面的判断条件表达式可以为test、[]、[[]]、(())等:
    注意then前面加一个分号。
    #!/bin/sh
    read -p "pls input two nums:" a b
    if [ $a -lt $b ]; then
        echo "$a less then $b"
    elif [ $a -eq $b ]; then
        echo "$a equals $b"
    else [ $a -gt $b ]
        echo "$a greater then $b"
    fi

46、判断字符串是否为整数:
    root@anLA7856:/home/anla7856/shell# [ -n "`echo 7856|sed 's/[0-9]//g'`" ] && echo char || echo int
    int
    root@anLA7856:/home/anla7856/shell# [ -z "`echo "${anla782356//[0-9]/}"`" ] && echo int || echo char
    int
    root@anLA7856:/home/anla7856/shell# expr anla7856 +1 &>/dev/null
    root@anLA7856:/home/anla7856/shell# echo $?
    2

47、shell执行系统中各种程序的执行顺序为:系统别名>函数>系统命令>可执行文件
   return是退出函数,而exit是退出脚本文件

48、追加文件以及查看末尾几行:
    root@anLA7856:/home/anla7856/shell# cat >>./testCat.sh<<- EOF
    > anla7856(){
    > echo "I am anla7856"
    > }
    > EOF
    root@anLA7856:/home/anla7856/shell# tail -3 ./testCat.sh
    anla7856(){
    echo "I am anla7856"
    }

49、case的基本用法:
    #!/bin/sh
    #anla7856
    read -p "please input a number:" ans
    case "$ans" in
        1)
        echo "The num you input is 1"
        ;;
        2)
        echo "The num you input is 2"
        ;;
        [3-9])
        echo "The num you input is $ans"
        ;;
        *)
        echo "please input [0-9] int"
        exit;                           #可以省略;;
    esac

50、ubuntu中的functions函数库在/lib/lsb/init-functions而不是/etc/init.d/functions
   可以在/etc/init.d/下建立functions的软链接:ln -s /lib/lsb/init-functions /etc/init.d/functions

51、当型循环结构:
    while true
    do
        uptime
        sleep 2
    done

52、按行读取文件的三种方法:
    #ways 1
    exec <FILE
    sum=0
    while read line
    do
        cmd
    done

    #ways 2
    cat FILE_PATH|while read line
    do
        cmd
    done

    #ways 3
    while read line
    do
        cmd
    done<FILE

53、for循环结构
    for 变量名 in 变量取值列表
    do    
        指令。。。
    done
   c语言结构类型的循环
    for ((i=1;i<3;i++))
    do
        echo $i
    done

54、生成数字序列的两种方式:
    root@anLA7856:/home/anla7856/shell# echo {4..1}
    4 3 2 1
    root@anLA7856:/home/anla7856/shell# echo `seq 5 -1 1`      #5是起始数字,-1是步长,1是终点
    5 4 3 2 1

55、修改文件名的方式,可以扩展到,批量修改多个文件名:
    root@anLA7856:/home/anla7856/shell/modifyName# filename=anla7856.txt
    root@anLA7856:/home/anla7856/shell/modifyName# ls
    anla7856.txt  anla7856.txt1  anla7856.txt2
    root@anLA7856:/home/anla7856/shell/modifyName# echo $filename
    anla7856.txt
    root@anLA7856:/home/anla7856/shell/modifyName# echo $filename|cut -d . -f1
    anla7856
    root@anLA7856:/home/anla7856/shell/modifyName# echo "`echo $filename|cut -d . -f1`.gif"
    anla7856.gif
    root@anLA7856:/home/anla7856/shell/modifyName# mv $filename `echo $filename|cut -d . -f1`.gif
    root@anLA7856:/home/anla7856/shell/modifyName# ls
    anla7856.gif  anla7856.txt1  anla7856.txt2
   另一种修改名字的操作:
    root@anLA7856:/home/anla7856/shell/modifyName# rename 's/txt/jjj/' *.txt1

56、linux中创建随机数:
   第一种:
    root@anLA7856:/home/anla7856/shell/databases# echo $RANDOM
    32682
    root@anLA7856:/home/anla7856/shell/databases# echo $RANDOM
    7398
    root@anLA7856:/home/anla7856/shell/databases# echo $RANDOM|md5sum
    86d89c7b8fcf2287785b1d8416a52673  -
    root@anLA7856:/home/anla7856/shell/databases# echo $RANDOM|md5sum|cut -c 5-12
    5435e8b2
    root@anLA7856:/home/anla7856/shell/databases# su aaa
    aaa@anLA7856:/home/anla7856/shell/databases$ su root
   第二种:
    root@anLA7856:/home/anla7856/shell# openssl rand -base64 8
    GNPQgTd4X9g=
    root@anLA7856:/home/anla7856/shell# openssl rand -base64 80
    VBMwxB15+GBU8G+S67jSEx5AF9t9tFkhkwX8uVBpaNKbTe1FWHrPPN4ulPEJVaNU
    2fWThWUIPykREDfNI6aPG8yGOaRU1K2m4QfwhW1orOM=
   第三种:(时间戳)
    root@anLA7856:/home/anla7856/shell# echo `date +%s`
    1504191167
   第四种:
    root@anLA7856:/home/anla7856/shell# head /dev/urandom|cksum
    2869718548 2230
    root@anLA7856:/home/anla7856/shell# head /dev/urandom|cksum
    2819634142 1626
   第五种:
    root@anLA7856:/home/anla7856/shell# cat /proc/sys/kernel/random/uuid
    928c2afd-8087-4778-9087-5b0ed2bd191a
    root@anLA7856:/home/anla7856/shell# cat /proc/sys/kernel/random/uuid
    7c3e09c5-700f-46ed-a617-217b5f09775e
   可以利用前面的cut -c 5-12和md5sum来统一格式化。

57、select用法:
    root@anLA7856:/home/anla7856/shell# cat selectTest.sh
    #!/bin/bash
    select name in anla7856 aaa anla
    do
        echo $name
    done
    root@anLA7856:/home/anla7856/shell# bash selectTest.sh       #使用bash name.bash来启动
    1) anla7856
    2) aaa
    3) anla
    #? 1                                                        #问好后面输入一个1,即说明输出第一个,找不到就输出空
    anla7856
    #? 2
    aaa
    #? 3
    anla
    #? 4

    #?
    如果使用数组的话:
    #!/bin/bash
    array=(anla78 anla56 anla123)

    select name in "${array[@]}"
    do
        echo $name
    done
    或者这样,通过命令返回的结果:
    root@anLA7856:/home/anla7856/shell# cat selectTest.sh
    #!/bin/bash
    select name in `ls /home/anla7856/shell`
    do
        echo $name
    done
   select里面的PS3系统以及REPLY变量:
    root@anLA7856:/home/anla7856/shell# cat seletcTest2.sh
    #!/bin/bash
    PS2="please select a num from menu:"
    select name in anla78 anla56 anla123
    do
        echo -e "I guess you selected the menu is : \n $REPLY $name"
    done
    root@anLA7856:/home/anla7856/shell# bash seletcTest2.sh
    1) anla78
    2) anla56
    3) anla123
    #? 1
    I guess you selected the menu is :
     1 anla78
    #? 2
    I guess you selected the menu is :
     2 anla56
    #? 3
    I guess you selected the menu is :
     3 anla123
    #? ^C

58、循环结构:
    root@anLA7856:/home/anla7856/shell# cat until.sh
    #!/bin/sh
    for name in `ls /home/anla7856/shell`
    do
        echo "$name"
    done

    while true
        do
        cmd
        done

59、shell中数组结构:
    root@anLA7856:/home/anla7856/shell# array=( 1 2 3 )
    root@anLA7856:/home/anla7856/shell# echo ${array[1]}
    2
    root@anLA7856:/home/anla7856/shell# echo ${array[*]}
    1 2 3
    root@anLA7856:/home/anla7856/shell# array2={[1]=one [2]=two [4]=four}
    [2]=two:未找到命令
    root@anLA7856:/home/anla7856/shell# array2=([1]=one [2]=two [4]=four)
    root@anLA7856:/home/anla7856/shell# echo ${array[3]}

    root@anLA7856:/home/anla7856/shell# echo ${array[4]}

    root@anLA7856:/home/anla7856/shell# echo ${array1[1]}

    root@anLA7856:/home/anla7856/shell# echo ${array2[1]}
    one
    root@anLA7856:/home/anla7856/shell# echo ${array2[4]}
    four
    root@anLA7856:/home/anla7856/shell# echo ${array2[3]}

    root@anLA7856:/home/anla7856/shell# array3=`ls`
    root@anLA7856:/home/anla7856/shell# echo ${array3[*]}
    9 caseTest.sh checkUrl.sh colorTest.sh compareInstance.sh comput2.sh comput3.sh comput.sh consoleRead.sh databases ddos.sh eval.sh exec.sh expr1.sh expr2.sh forbideIP.sh functionTest.sh guessNum.sh ifTest.sh installService.sh lamp.sh lnmp.sh mail.rc modifyName monitorSite.sh nginxd.sh ninenine.sh n.sh paramTest.sh pid.sh read1.sh readFile.sh rsyncd1.sh rsyncd.sh selectTest.sh seletcTest2.sh send.sh testCat.sh tmp until.sh user vi waystoreadfile.sh while.sh whoami.sh yangHuiTriangle.sh
   
60、打印数组元素长度:
    root@anLA7856:/home/anla7856/shell# echo ${#array[@]}
    3
    root@anLA7856:/home/anla7856/shell# echo ${#array[*]}
    3
    root@anLA7856:/home/anla7856/shell# array[1]=21
    root@anLA7856:/home/anla7856/shell# echo ${array[1]}
    21
   数组的删除,使用unset去删除一个或者整个数组。
   数组的截取与替换:
    root@anLA7856:/home/anla7856/shell# array=($(echo {a..z}))
    root@anLA7856:/home/anla7856/shell# echo ${array[@]}
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    root@anLA7856:/home/anla7856/shell# echo ${array[@]:1:5}
    b c d e f
    root@anLA7856:/home/anla7856/shell# echo ${array[@]/1/b}
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    root@anLA7856:/home/anla7856/shell# echo ${array[@]/0/b}
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    root@anLA7856:/home/anla7856/shell# echo ${array[@]/a/b}
    b b c d e f g h i j k l m n o p q r s t u v w x y z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值