LinuxCommandLine -- [脚本 if]

2018-04-30  本文已影响0人  liaozb1996

状态码

# 正常退出
$ ls
$ echo $?
0

# 异常
$ ls non-exist
ls: cannot access non-exist: No such file or directory
$ echo $?
2

if 语法

if [ condition ]; then
    commands
elif [ condition ]; then
    commands
else
    commands
fi

比较符

字符串

String

整数

integer.PNG

文件

test_file_1
test_file_2

test

# is_num.sh
#------------
#!/bin/bash

read -p "Enter something: " num

if [[ "$num" =~ ^-?[[:digit:]]+$ ]]; then
    echo "$num is a number"
else
    echo "$num is not a number"
fi

INT=-5

比较:变量名前不用加 $
(( INT == 0 ))

# 计算
(( ((INT % 2)) == 0))
上一篇 下一篇

猜你喜欢

热点阅读