拓展阅读

linux Shell 命令行-00-intro 入门介绍

linux Shell 命令行-02-var 变量

linux Shell 命令行-03-array 数组

linux Shell 命令行-04-operator 操作符

linux Shell 命令行-05-test 验证是否符合条件

linux Shell 命令行-06-flow control 流程控制

linux Shell 命令行-07-func 函数

linux Shell 命令行-08-file include 文件包含

linux Shell 命令行-09-redirect 重定向

基本输出

echo

echo [-e] 字符串

printf

类似于 C 的 printf() 函数

printf  格式化字符串  [参数...]

test 测试条件是否成立

数字

  • test_num.sh
#!/bin/bash

num1=100
num2=100
if test $[num1] -eq $[num2]
then
    echo '两个数相等!'
else
    echo '两个数不相等!'
fi

运行

houbinbindeMacBook-Pro:shell houbinbin$ /bin/sh test_num.sh
两个数相等!

字符串

  • test_str.sh
#!/bin/bash

num1="runoob"
num2="runoob"
if test num1=num2
then
    echo '两个字符串相等!'
else
    echo '两个字符串不相等!'
fi

运行

houbinbindeMacBook-Pro:shell houbinbin$ /bin/sh test_str.sh
两个字符串相等!

文件

  • test_file.sh
#!/bin/bash

if test -e /bin/bash
then
    echo '文件已存在!'
else
    echo '文件不存在!'
fi

运行

houbinbindeMacBook-Pro:shell houbinbin$ /bin/sh test_file.sh
文件已存在!

本文由博客一文多发平台 OpenWrite 发布!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。