shell

作者: 阿桃_28e7 | 来源:发表于2018-06-24 22:17 被阅读0次

    #!/bin/bash

    #1 输出

    function sayHello(){

    echo 'hello shell'

    }

    #sayHello

    #2 定义变量

    function defVar(){

    my_name='neo'

    echo ${my_name}

    }

    #defVar

    #3 循环输出

    function testFor(){

    for script in java php mysql js; do

    echo "i am good at ${script}-script"

    done

    }

    #testFor

    function testFor02(){

    arr=(java php mysql js)

    for skill in ${arr[*]}; do

    echo "i can use ${skill}-script"

    done

    }

    #testFor02

    #4 只读变量

    function testReadonly(){

    country_name='china'

    readonly country_name

    country_name='japan'

    }

    #testReadonly

    #5 删除变量

    function testDelVar(){

    test_del='test del'

    unset test_del

    echo ${test_del}

    }

    #testDelVar

    #6 单引号

    function test01(){

    my_name='neo'

    echo 'hello, i am ${my_name}'

    }

    #test01

    #7 双引号

    function test02(){

    my_name='neo'

    echo "hello, i am ${my_name}"

    }

    #test02

    #8 拼接字符串 https://blog.csdn.net/lixiaohuiok111/article/details/18313039

    function testStrConcat(){

    my_name='neo'

    hello="hello, i am "${my_name}" !\n"

    hello_2="hello, i am ${my_name} !"

    echo -e ${hello}${hello_2}

    }

    #testStrConcat

    #9 获取字符串长度

    function getStrLen(){

    str="abdc"

    echo ${#str}

    }

    #getStrLen

    #10 提取子字符串

    function subStr(){

    str="i am study shell"

    substr=${str:5:5} #从第6个字符开始截取5个字符【如果#接到}后面,变成字符串拼接】

    echo ${substr}

    }

    #subStr

    #函数传参

    function subStr02(){

    str="i am study shell"

    substr=${str:${1}:${2}}

    echo ${substr}

    }

    #subStr02 11 5

    #11 查找子字符串 https://blog.csdn.net/iamlaosong/article/details/54728393

    #【 以下脚本中 "`" 是反引号,而不是单引号 "'"】

    function testExpr(){

    str="runoob is a great site"

    echo `expr index "${str}" is` #查找字符"i或s"的位置

    echo $(expr index "${str}" is) #查找字符"i或s"的位置【推荐】

    }

    #testExpr

    #12 Shell数组

    function testArr(){

    arr=('西游' '水浒' '三国' '红楼梦')

    echo ${arr[0]}

    echo ${arr[@]} #遍历数组

    echo ${arr[*]} #遍历数组

    echo ${#arr[@]} #获取数组长度

    echo ${#arr[*]} #获取数组长度

    echo ${#arr[3]} #获取某个元素的长度

    }

    #testArr

    #13 Shell传递参数 。。。

    #14 if判断 注意判断条件[]中的空格

    # https://www.jb51.net/article/34332.htm

    # https://www.cnblogs.com/avivahe/p/5635911.html

    function testIf(){

    if [ ${1} -eq 1 ]; then

    echo 'num -eq 1'

    elif [ ${1} -eq 2 ]; then

    echo 'num -eq 2'

    else

    echo 'num -eq ?'

    fi

    }

    #testIf 2

    function testIf02(){

    if [ ${1} == 1 ]; then

    echo 'num == 1'

    elif [ ${1} == 2 ]; then

    echo 'num == 2'

    else

    echo 'num == ?'

    fi

    }

    #testIf02 3

    function test04(){

    if [ 1 == 1 ]; then

    echo  '1 == 1'

    else

    echo '1 != 1'

    fi

    if [ 2 -eq 2 ]; then

    echo 2 -eq 2

    else

    echo 2 -ne 2

    fi

    }

    #test04

    #15 算数运算符 【2 + 2之间必须有空格】

    #``好处是各个版本linux都可用, $()好处是直观

    #定义和使用函数 https://blog.csdn.net/zbw18297786698/article/details/77802037

    function arithmetic(){

    a=${1}

    b=${2}

    add_val=`expr ${a} + ${b}`

    echo "a + b -eq ${add_val}"

    sub_val=`expr ${a} - ${b}`

    echo "a - b -eq ${sub_val}"

    chu_val=`expr ${a} / ${b}`

    echo "a / b -eq ${chu_val}"

    yu_val=`expr ${a} % ${b}`

    echo "a % b -eq ${yu_val}"

    a=${b} #变量之间赋值

    echo "a -eq ${a}"

    }

    #arithmetic 20 10

    #16 字符串运算符function strExpr(){a=${1}b=${2}if [ ${a} = ${b} ]; thenecho "a 等于 b"elseecho "a 不等于 b"fiif [ ${a} != ${b} ]; thenecho "a 不等于 b"else echo "a 等于 b"fiif [ -z ${a} ]; thenecho "a 长度为 0"elseecho "a长度不为 0"fiif [ ${a} ]; thenecho "a不为空"else echo "a为空"fi}#strExpr "abc" "efg"#17 文件运算符function file(){file=${1}if [ -e ${file} ]; thenecho "文件存在"elseecho "文件不存在"fiif [ -s ${file} ]; thenecho "文件不为空"else echo "文件为空"fiif [ -f ${file} ]; thenecho "普通文件"elseecho "不是普通文件"fiif [[ ! -d ${file} ]]; thenecho "不是文件夹"fiif [ -r ${file} ]; thenecho "可读"elseecho "不可读"fiif [ -w ${file} ]; thenecho "可写"else echo "不可写"fiif [ -x ${file} ]; then echo "可执行"elseecho "不可执行"fi}#touch test.exe#echo "i am a file" > test.exe#chmod +x ./test.exe#file "test.exe"#18 echo命令function readInput(){read nameecho "${name} has been read"}#readInput# 换行输出function output(){echo -e "echo实现换行:\n 加上-e"}#output# 不换行输出function output02(){echo -e "echo实现不换行 \c"echo "test"}#output02# 输出至文件function output03(){echo "give you sth" > myfile}#output03#cat myfile# 显示命令执行结果,使用``[反引号]#echo `date`#echo $(date +%F%n%T)#echo date("Y-m-d H:i:s");#%n: 空格#%F:年月日#%T:时分秒function testPrintf(){printf "%-10s %-8s %-4s\n" 姓名 性别 体重printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234printf "%-10s %-8s %-4.2f\n" 杨过 男 48.8803printf "%-10s %-8s %-4.2f\n" 郭芙 女 45.55623}function comment(){%s %c %d %f都是格式替代符%-10s 指一个宽度为10个字符(-表示左对齐,没有则表示右对齐),任何字符都会被显示在10个字符宽的字符内,如果不足则自动以空格填充,超过也会将内容全部显示出来。%-4.2f 指格式化为小数,其中.2指保留2位小数。}#testPrintf#19 流程控制# let命令:http://www.runoob.com/linux/linux-comm-let.html#let 不需要空格隔开表达式的各个字符。#而 expr 后面的字符需要空格隔开各个字符。function testWhile(){i=1while (( ${i} <= 5 )) doecho ${i}let "i++"#i=`expr ${i} + 1`done}#testWhilefunction testWhile02(){echo "按下推出"

    echo -n "输入你最喜欢的网站:"

    while read site

    do

    echo "${site} is good"

    done

    }

    #testWhile02

    function testCase(){

    num=${1}

    case ${num} in

    1) echo '选1'

    ;;

    2) echo '选2'

    ;;

    3) echo '选3'

    ;;

    *) echo '没选'

    ;;

    esac #case反写作为结束

    }

    #testCase 5

    function testBreak(){

    num=${1}

    while :

    do

    case ${num} in

    1|2|3) echo "您的选择是:${num}"

    ;;

    *) echo "您没有在1-3之间选择,游戏结束"

      break

    ;;

    esac

    done

    }

    #testBreak

    #20 函数

    function add(){

    return $((${1}+${2}))

    }

    #add 1 2

    #echo "1 + 2 = ${?}" #使用${?}获取返回值

    function param(){

    echo "第一个参数:${1}"

    echo "参数总数:${#}"

    echo "所有参数:${*}"

    }

    #param 1 2 3 4 5 6 7 8

    #21 输入输出

    function testOutput(){

    touch users

    echo -e "hello file \c" > users

    echo "append txt" >> users

    cat users

    }

    #testOutput

    相关文章

      网友评论

          本文标题:shell

          本文链接:https://www.haomeiwen.com/subject/tutjyftx.html