shell

作者: 突刺刺 | 来源:发表于2018-09-13 11:16 被阅读10次

shell常用判断文件的方法

  • -e filename,如果filename存在,则为真
  • -d filename,如果filename是目录,则为真
  • -f filename,如果filename为常规文件,则为真
  • -L filename,如果filename为符号链接,则为真
  • -h filename,如果filename软链接,则为真
  • -r filename,如果filename可读,则为真
  • -w filename,如果filename可写,则为真
  • -x filename,如果filename可执行,则为真
  • -s filename,如果filename长度不为0,则为真
  • 例子:判断文件夹存不存在
if [ ! -d "${filename}" ];then
echo "文件夹不存在"
else
echo "文件夹已经存在"
fi

shell截取字符串的方法

  • 例子link="Users/my/Desktop/test.ipa"
  • echo ${link#*/} = my/Desktop/test.ipa,截取从左边第一个/到结尾的字符
  • echo ${link##*/} = test.ipa,截取从左边最后一个/到结尾的字符
  • echo ${link%/*} = Users/my/Desktop,截取从右边第一个/到开头的字符
  • echo ${link%%/*} = Users,截取从右边最后一个/到开头的字符

echo转义符号

  • -e,打开转义替换
  • -E,禁止转义替换(默认就是禁止转义转换)
  • \\,反斜杠
  • \a,警报,响铃
  • \b, 退格,删除
  • \c,两个echo之间不换行
  • \f, 换页,将当前位置移到下页开头
  • \n, 换行
  • \r,回车
  • \t, 水平制表符(tab键)
  • \v, 垂直制表符
  • 例子
echo -e "第一行\n第二行"
输出: 第一行
     第二行

shell比较整型数大小

  • -eq 相等(equal)
  • -ne 不等(not equal)
  • -gt 大于(greater than)
  • -lt 小于(less than)
  • -ge 大于等于(greater than or equal)
  • -le 小于等于(less than or equal)

shell字符串判断

  • [ $str1 = $str2 ] 等于
  • [ $str1 != $str2 ] 不等于
  • [ -z $str ] 空字符串,长度:0;返回:true
  • [ -n $str ] / [ $str ] 非空字符串,长度:>0;返回:true
  • [[ $A == *$B* ]] 真为包含,假则不包含*是shell的通配符
  • [[ $strA =~ $strB ]] 真为包含,假则不包含

shell函数

  • shell函数的定义
    • function_name ()
    • function name()
  • shell函数的调用
    • function_name
    • name
  • shell函数的传参
      function name()
      {
            echo "第一个参数:$1"
            echo "第一个参数:$2"
            echo "第一个参数:$3"
      }
      name "tom" "lily" "jack"
    
  • shell函数的返回值
    • return返回整数 (只能返回0~255之间的数值)
       function num()
       {
       return 1
       }
      
    • echo 可以返回任意数值/字符串
      function num()
      {
      echo $1
      }
      result=$(num "Tim")
      echo "result = ${result}"
      打印:result = Tim
      

相关文章

  • Shell 学习

    shell 变量 shell 参数传递 shell 数组 shell 运算符 shell echo 命令 prin...

  • Shell 概述

    学习 Shell 主要包括的内容: Shell 脚本入门 Shell 变量 Shell 内置命令 Shell 运算...

  • Shell 教程

    Shell 变量 Shell 传递参数 Shell 数组 Shell 基本运算符 Shell echo 命令 Sh...

  • shell 第一天

    shell编程初识 1.1 shell编程初识 shell的定义 Shell 是命令解释器 Shell 也是...

  • shell 案例

    Shell编程一 Shell防范ARP攻击 Shell编程二 Shell防范DDos攻击 Shell编程三 ...

  • 【生物信息笔记】shell 脚本 (dry-2)

    shell 和 shell script(脚本)区别: shell 和 shell 脚本是两个不同概念,shell...

  • Linux Shell:基础知识和Shell变量

    摘要:Linux,Shell 整理Shell内容要点: Shell基础知识 Shell变量的类型 Shell变量赋...

  • Shell脚本语言一

    一、语法 格式 运行 Shell变量 Shell字符串 Shell数组 Shell注释 Shell传递参数 She...

  • 使用shell脚本

    使用方式 shell 变量 shell 字符串操作 shell 数组 shell 注释 shell 命令行参数 s...

  • vim学习 09——shell命令

    vim学习 09——shell命令 执行 shell 命令 :!shell命令 : 可以执行 shell 命令。 ...

网友评论

      本文标题:shell

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