美文网首页
shell 脚本学习

shell 脚本学习

作者: 当阳桥 | 来源:发表于2017-06-25 22:45 被阅读116次

    之前有一家面试时候、提到过一些关于iOS之外的一些问题、作为程序员软实力的一部分,会写一点基本的脚本还是很有必要的,由于Mac自带 shell 所以iOS程序员以shell来作为自己的学习并使用的脚本语言还是有些必要的,比如曾经一个面试官提问我,如何在一个工程中把每个文件前几行注释的内容全部删掉,我知道他想问一些脚本运用的能力,可惜当时我不会,啊,多么痛的领悟!!!等了这么久,后面会把这块慢慢补起来。

    数组

    • 定义一个数组、并取值
    array=(1 2 3 4 5 6 7)
    #获取第一个元素
    echo "first num is \"${array[0]}\""
    #获取所有的元素
    echo "num is \"${array[@]}\""
    #获取数组长度
    echo "length is ${#array[@]}"
    

    条件语句

    • if-else
    num1=30
    num2=20
    if ((num1 > num2))
    then
    echo "num1-num2 = ${num1}-${num2}"
    else
    echo "num2-num1 = ${num1}-${num2}"
    fi
    
    • if-elif-else
    num1=30
    num2=20
    if ((num1>num2))
    then
    echo "num1>num2 : num1-num2 = ${num1}-${num2}"
    elif ((num1==num2))
    then
    echo "num1=num2"
    else
    echo "num1<num2 : num1-num2 = ${num1}-${num2}"
    fi
    

    循环语句

    • for-for
    for ((i=0;i<10;i++))
    do
      echo  ${i}
    done
    
    • for-in
    for i in 1 2 3 4 5
    do
      echo $i
    done
    
    for x in {1..5}
    do
    echo $x
    done
    
    • while-done
    min=90
    max=100
    while [ $min -le $max ]
    do
    echo $min
    min=`expr $min + 1`
    done
    

    函数

    • 无参数、无返回值
    shell1(){
        echo "无参数、无返回值"
    }
    调用方式:
    shell1
    
    • 有参数、无返回值
    shell2(){
         echo "有参数、无返回值 参数个数:$# 参数列表:[$@]"
    }
    调用方式:
    shell2  1 2 3 4 "hello"
    
    • 有返回值、有参数
    shell3(){
       echo "有返回值、有参数"
       y=0
       for x in $@
       do
           let y=x+y
       done
       echo y=${y}
       echo "返回值"
    }
    调用并使用:
    返回值就是echo中的内容
    return_value=`shell3 10 20 30 40`
    echo return_value =${return_value}
    

    一些特殊的命令

    • let 进行运算
    num1=10
    num2=3
    # let 进行运算
    let num3=${num1}+${num2}
    let num4=${num1}-${num2}
    let num5=${num1}*${num2}
    let num6=${num1}/${num2}
    let num7=${num1}%${num2}
    echo num3=${num3} num4=${num4} num5=${num5} num6=${num6} num7=${num7}
    
    • expr expr 进行运算 `expr` + - * / % 左右两边必须有空格
    num4=`expr ${num1} + ${num2}`
    num5=`expr ${num1}+${num2}`
    echo num4=${num4} num5=${num5}
    
    • grep 查找
    rootpath=/Users/myDocuments/Desktop/shell_Test/
    grep xxx ${rootpath}shell.sh
    # 查看在第几行的位置
    grep -n xxx ${rootpath}shell.sh
    #用正则表达式筛选
    grep -E [0-9] ${rootpath}shell.sh
    
    • sort 排序
    #排序 并不改变原文件
    sort ${rootpath}haha.sh
    echo "---------------------倒序排列---------------------"
    #倒序排列
    sort -r ${rootpath}haha.sh
    echo "---------------------数字排序---------------------"
    sort -n ${rootpath}number.txt
    echo "---------------------排序输出---------------------"
    #将后面的文件排序后的结果输出到前面的文件中
    sort -o ${rootpath}haha.txt ${rootpath}haha.sh
    
    • wc 统计
    #wc统计行数 line
    wc -l ${rootpath}shell.sh
    echo "---------------------统计单词数---------------------"
    #wc统计  word
    wc -w ${rootpath}shell.sh
    echo "---------------------统计字节数  一个汉子3个字节---------------------"
    #wc统计字节个数:一个汉子3个字节、一个字母一个字节
    wc -c ${rootpath}shell.sh
    echo "---------------------统计字符数---------------------"
    #wc统计字符个数:一个汉子是一个字符、一个字母也是一个字符
    wc -m ${rootpath}shell.sh
    
    • find 查找文件
    find ${rootpath} -name "*.sh"
    
    • cat 查看文件内容
    cat  ${rootpath}shell.sh
    # 遍历查看每一行
    for line in `cat ${rootpath}shell.sh`
    do
         echo $line
    done
    
    • sed 文本替换
    echo "#删除文件的第一行"
    path1=${rootpath}shell_5.sh
    #删除文件的第一行
    echo "#删除文件的第一行"
    sed  "1d" ${rootpath}shell_5.sh
    #删除文件的最后一行
    echo "#删除文件的最后一行"
    sed '$d' ${rootpath}shell_5.sh
    echo "删除文件的第二行到末尾所有行"
    #删除文件的第二行到末尾所有行
    sed '2,$d' ${path1}
    #echo "整行范围替换"
    #s开头表示替换、---表示替换的内容+++ 表示替换后的内容 g表示整行范围内替换、不加g表示只替换行的第一个
    #sed 's/---/+++/g' ${path1}
    echo "在原文件替换"
    #注意 -i 后面有一个 '' 不然替换不了 -i 在原文件替换
    sed -i '' 's/---/+++/g' ${path1}
    #删除文件每一行的第一个字符
    sed -i '' 's/^.//' ${rootpath}shell_2.sh
    
    • rm 移除文件
    #移除文件  -i 提示用户是否要删除 y 删除 、n不删除
    rm -i ${rootpath}TableViewCell.m
    # 强制删除 -f 强制删除
    rm -f ${rootpath}TableViewCell.m
    
    • mv 移动文件 到指定路径
    mv ${rootpath}test_clang.c ${rootpath}shell
    
    • cp 拷贝文件 到指定路径下的文件
    cp ${rootpath}TableViewCell.m ${rootpath}shell
    
    • mkdir创建路径 、touch添加文件
    #到指定路径
    cd ${rootpath}
    #添加一个文件夹
    mkdir shell
    #进入文件夹
    cd shell
    #循环10次、创建10个txt文件
    for ((i=0;i<10;i++));do
       touch test_$i.txt
    done
    
    • > 、 >> 重定向
    # > 重定向输出覆盖
    # >> 重定向追加
    grep "echo" ${rootpath}shell.sh > ${rootpath}shell2.sh
    grep "sort" ${rootpath}shell.sh >>${rootpath}shell2.sh
    grep "wc" ${rootpath}shell.sh >>${rootpath}shell2.sh
    grep "rm" ${rootpath}shell.sh >>${rootpath}shell2.sh
    
    • tar
    # 创建一个压缩包 将后面的文件压缩到前面的.tar包中
    tar -cvf ${rootpath}shell/compresstest3.tar ${rootpath}shell.sh ${rootpath}haha.txt
    # 显示归档内容 并重定向到xx文件
    tar -tf ${path}compresstest2.tar > ${path}compresstest2.txt
    # 解压压缩包
    tar -xvf ${path}compresstest2.tar
    
    • 创建可执行文件
    #将shell脚本变为可执行文件
    # 1、让脚本具有可执行能力
    chmod +x xxx.sh
    # 2、gzexe
    gzexe xxx.sh
    #然后改一下名字、把后缀去掉
    

    未完待续...

    相关文章

      网友评论

          本文标题:shell 脚本学习

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