美文网首页
shell 脚本初探

shell 脚本初探

作者: always_sun | 来源:发表于2017-09-25 23:14 被阅读0次

最近在工作中用到了shell 脚本,学会了一些简单的语法。分享如下。

  1. select 的实现

    • 指定要选择的var , 用户输入
    • echo出来一些选项, 用户通过输入数字来match 对应的选项。

    实现一:

      select var in a b c ;
      do 
       break
      done
    
      echo "$var"
    

    实现二:

    # choose env or prd update
    echo "-----------------------------"
    echo "please choose environment:"
    echo "(0) development(www_v4)"
    echo "(1) production(www_v3)"
    echo "(2) exit"
    echo "-----------------------------"
    read input
    case $input in 
    0) 
      dir='www_v5' ;;
    1) 
      dir='www_v3' ;;
    2)
      exit 1 ;;
    esac 
    
    1. if 判断多个条件
    # confirm env
    # 从控制台读取sure的值
    read -p "sure to continue? (Y/n) " sure
     # -z 表示为空, -n 不为空
     # 两个条件之间可直接用||相连
     # 表示只有输入Y时程序才会继续
    if [ -z "$sure" ] || [ "$sure" != "Y" ]; then
      echo 'quit'
      exit 1
    # fi表示if判断的结尾 
    fi 
    
    1. 异常处理
    git checkout master-a
    # 如果"$?"为0表示没有错误
    if [ "$?" = "0" ]; then
        echo '' checkout     success"
    else 
        # 0 表示标准输入
        # 1 表示标准输出
        # 2 表示标准错误输出
        # >  和 1> 意思一样, 标准输出重定向
        # 下面的1>&2 表示标准错误输出 重定向到 错误输出,程序如果有错误,显示错误并退出(exit 1)
        echo "git checkout branch error" 1>&2
        exit 1
    fi
    

参考文档

1. Shell重定向

相关文章

  • shell 脚本初探

    最近在工作中用到了shell 脚本,学会了一些简单的语法。分享如下。 select 的实现指定要选择的var , ...

  • Shell入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • 2018-09-26

    shell脚本 1.1、什么是shell脚本(shell script , ...

  • Shell script + crontab实现Mysql定时备

    一、Shell 脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。 业界所...

  • 嵌入式day12

    shell脚本的本质 shell脚本语言是解释型语言 shell脚本的本质:shell命令的有序集合 shell编...

  • shell脚本

    什么是shell脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所说...

  • Shell脚本语法

    1. Shell脚本简介Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所...

  • shell脚本

    什么是Shell脚本 Shell脚本(英语:Shell script),又称Shell命令稿、程序化脚本,是一种电...

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

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

  • chapter 11. 构建基本脚本

    创建shell脚本 shell脚本第一行为指定具体shell来运行该脚本,可以指定shell(待验证) echo ...

网友评论

      本文标题:shell 脚本初探

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