美文网首页
Shell脚本编程常用知识点

Shell脚本编程常用知识点

作者: 夏雨後之光 | 来源:发表于2019-06-24 15:18 被阅读0次

1. 获取命令行选项和参数

使用命令getopts "options_string" variable

while getopts 'd:Dm:f:t:' OPT
do
case $OPT in
    d)
        DEL_DAYS="$OPTARG";;
    D) 
        DEL_ORIGINAL="yes";;
    f) 
        DIR_FROM="$OPTARG";;
    m) 
        MAILDIR_NAME="$OPTARG";;
    t)
        DIR_TO="$OPTARG";;
    ?)
        echo "Usage: `basename $0` [options] filename"
        exit 1 ;;
esac
done

shift $(($OPTIND - 1))

使用命令getopt "options_string" $*

args=`getopt abo: $*`

if [ $? != 0 ]
then
    echo "Usage: ..."
    exit 2
fi

set -- $args
for i
do
    case "$i" in
        -a|-b) echo flag $i set; 
            shift;;
        -o) echo oarg is "$2"; 
            shift 2;;
        --) shift; 
            break;;
    esac

done

2. 判断字符串为空或非空

判断str为空 if [ -z $str ]
判断str为非空 if [ ! -z $str ]

3. 算术运算

使用num加1 num = $(expr $num + 1)

4. 输入与输出

输出 echo [-n] string
格式化输出 printf format [arguments ...]
输入 read var # 输入内容赋予变量var

相关文章

  • shell编程-交互 脚本菜单

    shell编程交互 脚本菜单 shell脚本的交互最常用的方式是使用菜单,通常是echo打印菜单出来。 上面的脚本...

  • Shell脚本编程常用知识点

    1. 获取命令行选项和参数 使用命令getopts "options_string" variable 使用命令g...

  • 数据分析工作中shell脚本的使用

    这篇文章主要通过工作中shell脚本案例,介绍shell脚本中常用知识点 下面的这个脚本表示使用sqoop把生产数...

  • 78.shell编程

    shell编程 Shell脚本,是一种为shell编写的脚本程序。 Shell编程跟JavaScript、Pyth...

  • Shell编程、part1

    1.shell简介2. shell分类3. 查看shell4. 第一个shell脚本5. shell编程常用命令5...

  • shell编程

    Shell脚本,是一种为shell编写的脚本程序。 Shell编程跟JavaScript、Python编程一样,只...

  • shell脚本基础

    编写脚本 编程基础 shell脚本 创建shell脚本 变量 运算 测试 配置用户的环境

  • shell学习笔记

    shell是基于shell解释器的脚本编程语言,也是使用Linux的常用工具,连接着用户和系统内核。 查看操作系统...

  • Bash脚本编程之算术表达式

    算术符号 Bash shell 脚本编程中的算术表达式所常用的算术运算符号包括:+,-,* ,/,%,**。其使用...

  • mac终端下运行shell脚本

    From: 在mac下编写shell脚本并执行 一些资料 Shell教程-for 菜鸟教程 Shell脚本编程30...

网友评论

      本文标题:Shell脚本编程常用知识点

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