常用命令

date

获取当前时间

1
time=$(date "+%Y%m%d")

语法

例一:字符串拆分&&循环

1
2
3
4
5
6
7
8
9
#!/bin/bash

string="hello,shell,split,test"
array=(${string//,/ })

for var in ${array[@]}
do
echo $var
done

例二:自定义assert

高级shell编程笔记(第二十九章 调试)_bash assert-CSDN博客

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
#
assert()
{
E_PARAM_ERR=98
E_ASSERT_FAILED=99

if [ -z "$2" ];then
return $E_PARAM_ERR
fi

lineno=$2

if [ ! $1 ];then
echo "Assertion failed: \"$1\""
echo "File \"$0\", line $lineno"
exit $E_ASSERT_FAILED
fi
}

a=5
b=4
condition="$a -lt $b"

assert "$condition" $LINENO
#脚本以下的代码只有当"assert"成功时才会继续执行

#...
echo "This statement echoes only if the \"assert\" does not fail."
#...
exit 0

例三:条件判断

Linux Bash Shell编程(八):条件判断与示例_linux条件判断式 提交中-CSDN博客

1
2
3
4
5
6
7
8
if [ -z $1 ];then # -z为空返回真
echo $1"aaa"
else
echo $1"bbb"
fi

# 两个条件表达式进行操作
if [ "x$1" == "xall" ] || [ "x$1" == "x$repoName" ];then

例四:case 语法

shell中的case语句详解_shell case-CSDN博客

1
2
3
4
5
6
7
8
9
10
case $1 in
push)
echo $1
;;
stash)
echo $1
;;
*)
echo "error"
esac