1
IFS=$'\n'

array.sh

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash

names=("Jason Chen" "Bell" "Alice")

# print all array members
echo "${names[@]}"

newNames=("${names[@]}" "Jason Guo")
echo "${newNames[@]}"


days=([0]=Sun [1]=Mon [2]=Tue [3]=Wed [4]=Thu [5]=Fri [6]=Sta)
echo "days: ${#days[@]}"
echo "value(index at 0): ${days[0]}"
echo "value's length at index 0: ${days[0]}"

## return indexes
for i in ${!days[@]}; do
echo "index: $i, value: ${days[$i]}"
done

## extract array members: ${array[@]:position:length}
echo ${days[@]:1:3}

## append to array
days+=("Unk" Unk"")
echo "${days[@]}"

## del a member from array
unset days[7]
days[8]=''
echo "after remove index[7,8]: ${days[@]}"

## empty array
unset days
echo "after empty array: ${days[@]}"


echo "your input will be added to array:"
read -a dice

for i in "${dice[@]}"; do
echo $i
done


declare -A colors
colors["red"]="#ff0000"

echo ${colors["red"]}

bashEnv.sh

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

function func1() {
## 打印函数调用栈 FUNCNAME 返回一个数组,0号元素是当前调用的函数
## 1 号元素是调用当前函数的函数,以此类推
echo -e "\nfunction call stack:"
echo "FUNCNAME[0]: ${FUNCNAME[0]}"
echo "FUNCNAME[1]: ${FUNCNAME[1]}"
}

echo "This is ${LINENO}th line in this script file"
func1

custom_env.sh

1
2
3
4
5
6
#!/usr/bin/env bash
export CUST_ENV4=guo
CUST_ENV5=peng
CUST_ENV6=hui

(echo $CUST_ENV4)

hello.sh

1
2
3
4
#!/bin/bash

hello="hello world"
echo "$hello, Tang!"

if.sh

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash

echo -n "plz input your answer:"
read ans

### String condition test
if [ -z "$ans" ]; then
echo "you input nothing!"
exit 1
fi

if [ "$ans" = "yes" ]; then
echo "Yeah!!!!!!!!"
elif [ "$ans" = "no" ]; then
echo "Sorry to hear that!!!!!!!"
elif [ "$ans" = "maybe" ]; then
echo "You have make up your mind .-."
else
echo "unknow answer"
fi

echo -n "plz input a num:"
read INT

### [[ ]] supports regex
if [[ "$INT" =~ ^-?[0-9]+$ ]]; then
echo "yeah, you input a number, good boy!"
else
echo "your input is not a number!"
exit 1
fi

### integer condition
if [ $INT -eq 0 ]; then
echo "zero"
elif [ $INT -gt 0 ]; then
echo "great than zero"
else
echo "less than zero"
fi

echo -n 'if ((3%2)) result='
if ((3%2)); then echo "true"; else echo "false"; fi

echo -n 'if ((3%3)) result='
if ((3%3)); then echo "true"; else echo "false"; fi

echo -n 'if ((3 > 2)) result='
if ((3 > 2)); then echo "true"; else echo "false"; fi

echo -n 'if ((3 < 2)) result='
if ((3 < 2)); then echo "true"; else echo "false"; fi

echo -n 'if ((3 == 3)) result='
if ((3 == 3)); then echo "true"; else echo "false"; fi

## command1 || command2; if command1 is false, then run command2
[ -d dir ] || mkdir dir

loop.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash

## loop test

number=1
while [ "$number" -le 10 ]; do
echo "$number hello"
number=$((number+1))
done

for i in *.sh; do
echo "shell file: $i"
done

## break continue
for (( i=0; i<5; i=i+1 )); do
echo $i
if [ $i -eq 3 ]; then
echo "break statement"
break
# continue
fi
done

month.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash

#echo "input month number:"
#read monthNum

monthNum=$1
if [[ "$monthNum" =~ ^(1[0-2]|0?[1-9])$ ]]; then
echo -n "you input $monthNum, and it is: "
case $monthNum in
1 | 01) echo "Jan" ;;
2 | 02) echo "Feb" ;;
3 | 03) echo "Mar" ;;
4 | 04) echo "Apr" ;;
5 | 05) echo "May" ;;
6 | 06) echo "Jun" ;;
7 | 07) echo "Jul" ;;
8 | 08) echo "Aug" ;;
9 | 09) echo "Sep" ;;
10) echo "Oct" ;;
11) echo "Nov" ;;
12) echo "Dec" ;;
esac
fi

parameter-parse.sh

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env bash
$(echo 'command')

echo $PWD

# 当前脚本中没有定义该变量,但如果调用脚本的父shell定义了该变量,并使用 source 执行,则可以打印
echo -e "\nvariable defined in parent shell: $CUST_ENV1\n"

echo 时间: $(date '+%F %T')
echo 带有option的参数列表: $@
echo 带有option的参数个数: $#


while getopts 'sn:' OPT; do
case "$OPT" in
s)
echo "silent output enabled"
;;

n)
echo "output number: $OPTARG"
;;
?)
echo "avaliable options: [-s] [-n number]" >&2 ## standard error
exit 1
;;
esac
done
shift "$(($OPTIND - 1))"

##
echo "option 选项处理完成,处理剩余主参数列表: $@"

## for 循环遍历参数列表
echo -e "\nfor 循环便利参数列表"
for i in $@; do
echo 参数:$i
done


echo -e "\nshift 遍历参数列表:$@"
## shift n 移除脚本当前n个参数, 没有指定默认为1;
while [ "$1" != "" ]; do
echo 参数个数:$#
echo 参数:$1
shift
done

readFile.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env bash

if [ $# == 0 ]; then
echo "请指定文件路径"
exit 2
fi

for i in "$@"; do
echo -e "\nfile: $i"
num=1
while read line; do
echo -e "$((num++))\t$line"
done < $i
done

readInput.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env bash
echo -n "输入您的用户名 > "
read user
read -s -p "请输入密码 > " passwd1
echo
read -s -p "请确认密码 > " passwd2
echo

if [ "$passwd1" == "$passwd2" ]; then
echo "注册成功, 用户名: '$user', 密码: '$passwd1'"
else
echo "密码输入不一致: '$passwd1', '$passwd2'"
fi

select.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

echo "select the seq number to choose(use ctrl+c to quit)"

select gender in male female other
do
case $gender in
"male") echo "Hi man" ;;
"female") echo "Hi girl" ;;
"other") echo "Hi there" ;;
*) echo "Unknow seq" ;;
esac
done

setShellDisallowNoUnset.sh

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

# set -o nounset: none set variable will case exit
set -u

echo $a
echo "hi"

setShellExitOnError.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash

# 遇到错误退出可以使用 command1 || exit 1, 如果 commands 1 执行失败,就执行 exit 1
# ls *.exe || exit 1

# 使用 -e 设置, set -o errexit
set -e

# 如果不存在 dll文件,不会终止,只要管道最后的命令执行成功
# 为了避免这种情况,可以使用 set -eo pipefail
ls *.dll | true

# 如果不存在 exe文件,错误终止
ls *.exe
echo "hi there"

# set +e 可以关闭该设置

setShellallowTrace.sh

1
2
3
4
5
6
#!/bin/bash

# set -o xtrace
set -x

echo "$PWD"

temp.sh

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

# 脚本退出时,删除创建的临时文件
trap 'rm -f "$tmpFile"' EXIT

tmpFile=$(mktemp) || exit 1
echo "temporary file created successfully: $tmpFile"