shell脚本学习(二)

shell脚本学习基础知识、接上一篇。

字符串

变量声明可以使用单引号''或者双引号"".

  1. 单引号的限制:
    . 按原字符输出,单引号中的变量是无效的。
    . 单引号中不能出现单引号(转义也不行)

  2. 双引号
    . 可以有变量
    . 可以出现转义

    对字符串的各种操作。

    # join the str
    a="hello"
    b=""$a"world"       ==   "${a}world"
    # get the str length 	
    echo "${#a}"                 // 5
    # split sub str
    echo "${a:1:2}"               // "el"
    # find  sub str  返回字符串中第一个字符第一次出现的位置
    echo `expr index "${a}" e`         // 1
    
数组

支持一维数组,不限定大小。

  1. 定义
    用括号定义,使用空格分割
    # define
    arr=(1 2 4 5)
    # single define & the index could not continue 
    # if the index is not exist ,the value is empty
     arr[0]=3
     arr[1]=4
     arr[2]=6
     arr[6]=9
    
  2. 获取
    # ouput arr
    echo "arr ${arr[2]}"       // 6
    # using * | @ output All
    echo "${arr[*]}"            // 3 4 6 9
    echo "${arr[5]}"           //
    # get the arr length using the char * | @
    echo "${#arr[*]}"              // 4
    # get the arr of the element length
    echo "${#arr[5]}"           // 0
    

数组下标不限,数组的长度为实际存在值的元素数量。

echo /printf

输出指令,打印字符串。

  1. 显示转义

    echo "\"hello wrold \""                     // "hello world"
    # 显示指定换行(默认换行)                         ** 测试无用、打印出了 \n
    echo "hello \n world"
    # 指定不换行                         ** 测试无用、打印出了 \c
    echo "hello \c"
    echo "world"
    
  2. 输出结果重定向到文件

    echo  "hello world" > log 
    

    > 覆盖文件内容 >> 追加内容,

  3. printf 打印字符、需要手动加换行符

    printf "hello \n"                          
    printf "world"                   
    # format string
    printf "%d %s" 4 "name"          
    
    // 输出
    hello
    world4 name    
    

    %d格式数值,默认 0 %s格式字符串 默认为空

test

检测某个条件是否成立,可进行数值、文件、字符三个方面。

  1. 数值测试,使用关系运算符
    关系运算符

    a=10
    b=24
    if test ${a} -eq ${b}
    then
    	echo 'a eq b
    else 
    	echo 'a not eq b'
    fi
    
  2. 字符串测试、
    在这里插入图片描述

    if test ${a} = ${b}
    then	
    	echo 'a = b'
    else
    	echo 'a!=b'
    fi
    
  3. 文件测试

    检测参数说明
    -e [filename]文件存在为 true
    -r [filename]文件存在且可读 true
    -w [filename]文件存在且可写 true
    -x [filename]文件存在可执行true
    -s [filename]文件存在且 至少有一个字符 true
    -d [filename]文件存在且为目录 true
    -f [filename]文件存在且 为普通文件 true
    -c [filename]文件存在且 为字符型特殊文件 true
    -b [filename]文件存在且 为块特殊文件 true
    if test -e ./008.sh
    then 
    	echo 'exit'
    fi
    

    提供 !(与) -a(非) -o(或)多个条件连接。

函数
  1. 函数定义

    # define
    function getName(){
    	echo 'hahahha'
    }
    # Invoke your function
    getName
    echo $?      // 0
    

    调用函数直接写函数名。
    使用$? 来获取函数调用执行的返回值。
    函数删除,使用unset命令,需加上.f选项。

    函数默认返回 0 ,表示调用成功。
    只能返回数值,不能返回字符串,会报错。

  2. 函数参数
    在调用函数时,参数以空格分隔匝函数名后。

    function getName(){
    	echo $1
    	echo $2
    }
    getName 2 3 4 5
    // 2 3
    

    使用 $n 来获取第几个参数。n>=10 书写为 ${10}

    变量说明
    $#参数个数
    $*显示所有传入的参数
    $@与$* 同
    $?函数的返回值
输入输出重定向

command > [file] 覆盖文件内容
command >> [file]追加到文件
command < [file]获取文件内容

感谢作者:shell教程

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

heroboyluck

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值