mirror of
https://github.com/dunwu/linux-tutorial.git
synced 2024-04-15 19:55:24 +08:00
update shell scripts
This commit is contained in:
parent
4fc170b647
commit
43f4c50461
55
codes/shell/array-demo.sh
Normal file
55
codes/shell/array-demo.sh
Normal file
@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# 创建数组
|
||||
nums=([2]=2 [0]=0 [1]=1)
|
||||
colors=(red yellow "dark blue")
|
||||
|
||||
# 访问数组的单个元素
|
||||
echo ${nums[1]}
|
||||
# Output: 1
|
||||
|
||||
# 访问数组的所有元素
|
||||
echo ${colors[*]}
|
||||
# Output: red yellow dark blue
|
||||
|
||||
echo ${colors[@]}
|
||||
# Output: red yellow dark blue
|
||||
|
||||
printf "+ %s\n" ${colors[*]}
|
||||
# Output:
|
||||
# + red
|
||||
# + yellow
|
||||
# + dark
|
||||
# + blue
|
||||
|
||||
printf "+ %s\n" "${colors[*]}"
|
||||
# Output:
|
||||
# + red yellow dark blue
|
||||
|
||||
printf "+ %s\n" "${colors[@]}"
|
||||
# Output:
|
||||
# + red
|
||||
# + yellow
|
||||
# + dark blue
|
||||
|
||||
# 访问数组的部分元素
|
||||
echo ${nums[@]:0:2}
|
||||
# Output:
|
||||
# 0 1
|
||||
|
||||
# 访问数组长度
|
||||
echo ${#nums[*]}
|
||||
# Output:
|
||||
# 3
|
||||
|
||||
# 向数组中添加元素
|
||||
colors=(white "${colors[@]}" green black)
|
||||
echo ${colors[@]}
|
||||
# Output:
|
||||
# white red yellow dark blue green black
|
||||
|
||||
# 从数组中删除元素
|
||||
unset nums[0]
|
||||
echo ${nums[@]}
|
||||
# Output:
|
||||
# 1 2
|
@ -1,71 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# 声明数组
|
||||
## 声明数组方式一
|
||||
animals[0]=Cat
|
||||
animals[1]=Dog
|
||||
animals[2]=Fish
|
||||
## 声明数组方式二
|
||||
colors=(Red Green Blue)
|
||||
|
||||
|
||||
# 根据下标获取数组中的元素
|
||||
echo "=========== 根据下标获取数组中的元素 ==========="
|
||||
echo "\${colors[1]} : " ${colors[1]}
|
||||
# 输出:
|
||||
# ${colors[1]} : Green
|
||||
|
||||
|
||||
# 数组切片
|
||||
echo "=========== 数组切片 ==========="
|
||||
colors[1]="Dark Green"
|
||||
|
||||
echo "\${colors[*]} : "
|
||||
printf "+ %s\n" ${colors[*]}
|
||||
# 输出:
|
||||
# + Red
|
||||
# + Dark
|
||||
# + Green
|
||||
# + Blue
|
||||
|
||||
echo "\"\${colors[*]}\" : "
|
||||
printf "+ %s\n" "${colors[*]}"
|
||||
# 输出:
|
||||
# + Red Dark Green Blue
|
||||
|
||||
echo "\"\${colors[@]}\" : "
|
||||
printf "+ %s\n" "${colors[@]}"
|
||||
# 输出:
|
||||
# + Red
|
||||
# + Dark
|
||||
# + Green
|
||||
# + Blue
|
||||
|
||||
echo "\"\${colors[@]:0:2}\" : " ${colors[@]:0:2} ### Red Dark Green
|
||||
|
||||
# 向数组中添加元素
|
||||
echo "=========== 向数组中添加元素 ==========="
|
||||
colors=(Yellow "${colors[@]}" Pink Black)
|
||||
echo "\"\${colors[@]}\" : " ${colors[@]}
|
||||
# 输出:
|
||||
# Yellow Red Dark Green Blue Pink Black
|
||||
|
||||
# 向数组中删除元素
|
||||
echo "=========== 向数组中删除元素 ==========="
|
||||
unset colors[0]
|
||||
echo "\"\${colors[@]}\" : " ${colors[@]}
|
||||
# 输出:
|
||||
# Red Dark Green Blue Pink Black
|
||||
|
||||
# 获取数组的长度
|
||||
echo "=========== 获取数组的长度 ==========="
|
||||
## 获取数组的长度方式一
|
||||
echo "\${#colors[*]} : ${#colors[*]}"
|
||||
## 获取数组的长度方式二
|
||||
echo "\${#colors[@]} : ${#colors[@]}"
|
||||
|
||||
|
||||
for (( i = 0; i < animals; i ++ )); do
|
||||
echo $i
|
||||
done
|
||||
|
19
codes/shell/comment-demo.sh
Normal file
19
codes/shell/comment-demo.sh
Normal file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
#--------------------------------------------
|
||||
# shell 注释示例
|
||||
# author:zp
|
||||
#--------------------------------------------
|
||||
|
||||
# echo '这是单行注释'
|
||||
|
||||
########## 这是分割线 ##########
|
||||
|
||||
:<<EOF
|
||||
echo '这是多行注释'
|
||||
echo '这是多行注释'
|
||||
echo '这是多行注释'
|
||||
EOF
|
||||
|
||||
# Execute: ./comment-demo.sh
|
||||
# Output: null
|
55
codes/shell/operator/operator-demo.sh
Normal file
55
codes/shell/operator/operator-demo.sh
Normal file
@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
x=10
|
||||
if [[ -n $1 ]]; then
|
||||
x=$1
|
||||
fi
|
||||
|
||||
y=20
|
||||
if [[ -n $2 ]]; then
|
||||
y=$2
|
||||
fi
|
||||
|
||||
echo "x=${x}, y=${y}"
|
||||
|
||||
val=`expr ${x} + ${y}`
|
||||
echo "${x} + ${y} = $val"
|
||||
|
||||
val=`expr ${x} - ${y}`
|
||||
echo "${x} - ${y} = $val"
|
||||
|
||||
val=`expr ${x} \* ${y}`
|
||||
echo "${x} * ${y} = $val"
|
||||
|
||||
val=`expr ${y} / ${x}`
|
||||
echo "${y} / ${x} = $val"
|
||||
|
||||
val=`expr ${y} % ${x}`
|
||||
echo "${y} % ${x} = $val"
|
||||
|
||||
if [[ ${x} == ${y} ]]; then
|
||||
echo "${x} = ${y}"
|
||||
fi
|
||||
if [[ ${x} != ${y} ]]; then
|
||||
echo "${x} != ${y}"
|
||||
fi
|
||||
|
||||
# Execute: ./operator-demo.sh
|
||||
# Output:
|
||||
# x=10, y=20
|
||||
# 10 + 20 = 30
|
||||
# 10 - 20 = -10
|
||||
# 10 * 20 = 200
|
||||
# 20 / 10 = 2
|
||||
# 20 % 10 = 0
|
||||
# 10 != 20
|
||||
|
||||
# Execute: ./operator-demo.sh 10 30
|
||||
# Output:
|
||||
# x=10, y=30
|
||||
# 10 + 30 = 40
|
||||
# 10 - 30 = -20
|
||||
# 10 * 30 = 300
|
||||
# 30 / 10 = 3
|
||||
# 30 % 10 = 0
|
||||
# 10 不等于 30
|
59
codes/shell/operator/operator-demo2.sh
Normal file
59
codes/shell/operator/operator-demo2.sh
Normal file
@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
x=10
|
||||
if [[ -n $1 ]]; then
|
||||
x=$1
|
||||
fi
|
||||
|
||||
y=20
|
||||
if [[ -n $2 ]]; then
|
||||
y=$2
|
||||
fi
|
||||
|
||||
echo "x=${x}, y=${y}"
|
||||
|
||||
if [[ ${x} -eq ${y} ]];then
|
||||
echo "${x} -eq ${y} : x 等于 y"
|
||||
else
|
||||
echo "${x} -eq ${y}: x 不等于 y"
|
||||
fi
|
||||
|
||||
if [[ ${x} -ne ${y} ]]; then
|
||||
echo "${x} -ne ${y}: x 不等于 y"
|
||||
else
|
||||
echo "${x} -ne ${y}: x 等于 y"
|
||||
fi
|
||||
|
||||
if [[ ${x} -gt ${y} ]]; then
|
||||
echo "${x} -gt ${y}: x 大于 y"
|
||||
else
|
||||
echo "${x} -gt ${y}: x 不大于 y"
|
||||
fi
|
||||
|
||||
if [[ ${x} -lt ${y} ]]; then
|
||||
echo "${x} -lt ${y}: x 小于 y"
|
||||
else
|
||||
echo "${x} -lt ${y}: x 不小于 y"
|
||||
fi
|
||||
|
||||
if [[ ${x} -ge ${y} ]]; then
|
||||
echo "${x} -ge ${y}: x 大于或等于 y"
|
||||
else
|
||||
echo "${x} -ge ${y}: x 小于 y"
|
||||
fi
|
||||
|
||||
if [[ ${x} -le ${y} ]]; then
|
||||
echo "${x} -le ${y}: x 小于或等于 y"
|
||||
else
|
||||
echo "${x} -le ${y}: x 大于 y"
|
||||
fi
|
||||
|
||||
# Execute: ./operator-demo2.sh
|
||||
# Output:
|
||||
# x=10, y=20
|
||||
# 10 -eq 20: x 不等于 y
|
||||
# 10 -ne 20: x 不等于 y
|
||||
# 10 -gt 20: x 不大于 y
|
||||
# 10 -lt 20: x 小于 y
|
||||
# 10 -ge 20: x 小于 y
|
||||
# 10 -le 20: x 小于或等于 y
|
45
codes/shell/operator/operator-demo3.sh
Normal file
45
codes/shell/operator/operator-demo3.sh
Normal file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
x=10
|
||||
if [[ -n $1 ]]; then
|
||||
x=$1
|
||||
fi
|
||||
|
||||
y=20
|
||||
if [[ -n $2 ]]; then
|
||||
y=$2
|
||||
fi
|
||||
|
||||
echo "x=${x}, y=${y}"
|
||||
|
||||
if [[ ${x} != ${y} ]];then
|
||||
echo "${x} != ${y} : x 不等于 y"
|
||||
else
|
||||
echo "${x} != ${y}: x 等于 y"
|
||||
fi
|
||||
|
||||
if [[ ${x} -lt 100 && ${y} -gt 15 ]];then
|
||||
echo "${x} 小于 100 且 ${y} 大于 15 : 返回 true"
|
||||
else
|
||||
echo "${x} 小于 100 且 ${y} 大于 15 : 返回 false"
|
||||
fi
|
||||
|
||||
if [[ ${x} -lt 100 || ${y} -gt 100 ]];then
|
||||
echo "${x} 小于 100 或 ${y} 大于 100 : 返回 true"
|
||||
else
|
||||
echo "${x} 小于 100 或 ${y} 大于 100 : 返回 false"
|
||||
fi
|
||||
|
||||
if [[ ${x} -lt 5 || ${y} -gt 100 ]];then
|
||||
echo "${x} 小于 5 或 ${y} 大于 100 : 返回 true"
|
||||
else
|
||||
echo "${x} 小于 5 或 ${y} 大于 100 : 返回 false"
|
||||
fi
|
||||
|
||||
# Execute: ./operator-demo3.sh
|
||||
# Output:
|
||||
# x=10, y=20
|
||||
# 10 != 20 : x 不等于 y
|
||||
# 10 小于 100 且 20 大于 15 : 返回 true
|
||||
# 10 小于 100 或 20 大于 100 : 返回 true
|
||||
# 10 小于 5 或 20 大于 100 : 返回 false
|
33
codes/shell/operator/operator-demo4.sh
Normal file
33
codes/shell/operator/operator-demo4.sh
Normal file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
x=10
|
||||
if [[ -n $1 ]]; then
|
||||
x=$1
|
||||
fi
|
||||
|
||||
y=20
|
||||
if [[ -n $2 ]]; then
|
||||
y=$2
|
||||
fi
|
||||
|
||||
echo "x=${x}, y=${y}"
|
||||
|
||||
if [[ ${x} -lt 100 && ${y} -gt 100 ]]
|
||||
then
|
||||
echo "${x} -lt 100 && ${y} -gt 100 返回 true"
|
||||
else
|
||||
echo "${x} -lt 100 && ${y} -gt 100 返回 false"
|
||||
fi
|
||||
|
||||
if [[ ${x} -lt 100 || ${y} -gt 100 ]]
|
||||
then
|
||||
echo "${x} -lt 100 || ${y} -gt 100 返回 true"
|
||||
else
|
||||
echo "${x} -lt 100 || ${y} -gt 100 返回 false"
|
||||
fi
|
||||
|
||||
# Execute: ./operator-demo4.sh
|
||||
# Output:
|
||||
# x=10, y=20
|
||||
# 10 -lt 100 && 20 -gt 100 返回 false
|
||||
# 10 -lt 100 || 20 -gt 100 返回 true
|
52
codes/shell/operator/operator-demo5.sh
Normal file
52
codes/shell/operator/operator-demo5.sh
Normal file
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
x="abc"
|
||||
if [[ -n $1 ]]; then
|
||||
x=$1
|
||||
fi
|
||||
|
||||
y="xyz"
|
||||
if [[ -n $2 ]]; then
|
||||
y=$2
|
||||
fi
|
||||
|
||||
echo "x=${x}, y=${y}"
|
||||
|
||||
if [[ ${x} = ${y} ]];then
|
||||
echo "${x} = ${y} : x 等于 y"
|
||||
else
|
||||
echo "${x} = ${y}: x 不等于 y"
|
||||
fi
|
||||
|
||||
if [[ ${x} != ${y} ]];then
|
||||
echo "${x} != ${y} : x 不等于 y"
|
||||
else
|
||||
echo "${x} != ${y}: x 等于 y"
|
||||
fi
|
||||
|
||||
if [[ -z ${x} ]];then
|
||||
echo "-z ${x} : 字符串长度为 0"
|
||||
else
|
||||
echo "-z ${x} : 字符串长度不为 0"
|
||||
fi
|
||||
|
||||
if [[ -n "${x}" ]];then
|
||||
echo "-n ${x} : 字符串长度不为 0"
|
||||
else
|
||||
echo "-n ${x} : 字符串长度为 0"
|
||||
fi
|
||||
|
||||
if [[ ${x} ]];then
|
||||
echo "${x} : 字符串不为空"
|
||||
else
|
||||
echo "${x} : 字符串为空"
|
||||
fi
|
||||
|
||||
# Execute: ./operator-demo5.sh
|
||||
# Output:
|
||||
# x=abc, y=xyz
|
||||
# abc = xyz: x 不等于 y
|
||||
# abc != xyz : x 不等于 y
|
||||
# -z abc : 字符串长度不为 0
|
||||
# -n abc : 字符串长度不为 0
|
||||
# abc : 字符串不为空
|
49
codes/shell/operator/operator-demo6.sh
Normal file
49
codes/shell/operator/operator-demo6.sh
Normal file
@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
file="/etc/hosts"
|
||||
|
||||
if [[ -r ${file} ]];then
|
||||
echo "${file} 文件可读"
|
||||
else
|
||||
echo "${file} 文件不可读"
|
||||
fi
|
||||
if [[ -w ${file} ]];then
|
||||
echo "${file} 文件可写"
|
||||
else
|
||||
echo "${file} 文件不可写"
|
||||
fi
|
||||
if [[ -x ${file} ]];then
|
||||
echo "${file} 文件可执行"
|
||||
else
|
||||
echo "${file} 文件不可执行"
|
||||
fi
|
||||
if [[ -f ${file} ]];then
|
||||
echo "${file} 文件为普通文件"
|
||||
else
|
||||
echo "${file} 文件为特殊文件"
|
||||
fi
|
||||
if [[ -d ${file} ]];then
|
||||
echo "${file} 文件是个目录"
|
||||
else
|
||||
echo "${file} 文件不是个目录"
|
||||
fi
|
||||
if [[ -s ${file} ]];then
|
||||
echo "${file} 文件不为空"
|
||||
else
|
||||
echo "${file} 文件为空"
|
||||
fi
|
||||
if [[ -e ${file} ]];then
|
||||
echo "${file} 文件存在"
|
||||
else
|
||||
echo "${file} 文件不存在"
|
||||
fi
|
||||
|
||||
# Execute: ./operator-demo6.sh
|
||||
# Output:(根据文件的实际情况,输出结果可能不同)
|
||||
# /etc/hosts 文件可读
|
||||
# /etc/hosts 文件可写
|
||||
# /etc/hosts 文件不可执行
|
||||
# /etc/hosts 文件为普通文件
|
||||
# /etc/hosts 文件不是个目录
|
||||
# /etc/hosts 文件不为空
|
||||
# /etc/hosts 文件存在
|
@ -1,30 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
a=10
|
||||
b=20
|
||||
|
||||
echo "a=$a, b=$b"
|
||||
|
||||
val=`expr $a + $b`
|
||||
echo "a + b : $val"
|
||||
|
||||
val=`expr $a - $b`
|
||||
echo "a - b : $val"
|
||||
|
||||
val=`expr $a \* $b`
|
||||
echo "a * b : $val"
|
||||
|
||||
val=`expr $b / $a`
|
||||
echo "b / a : $val"
|
||||
|
||||
val=`expr $b % $a`
|
||||
echo "b % a : $val"
|
||||
|
||||
if [ $a == $b ]
|
||||
then
|
||||
echo "a 等于 b"
|
||||
fi
|
||||
if [ $a != $b ]
|
||||
then
|
||||
echo "a 不等于 b"
|
||||
fi
|
@ -1,43 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
a=10
|
||||
b=20
|
||||
|
||||
echo "a=$a, b=$b"
|
||||
|
||||
if [ $a -eq $b ]
|
||||
then
|
||||
echo "$a -eq $b : a 等于 b"
|
||||
else
|
||||
echo "$a -eq $b: a 不等于 b"
|
||||
fi
|
||||
if [ $a -ne $b ]
|
||||
then
|
||||
echo "$a -ne $b: a 不等于 b"
|
||||
else
|
||||
echo "$a -ne $b : a 等于 b"
|
||||
fi
|
||||
if [ $a -gt $b ]
|
||||
then
|
||||
echo "$a -gt $b: a 大于 b"
|
||||
else
|
||||
echo "$a -gt $b: a 不大于 b"
|
||||
fi
|
||||
if [ $a -lt $b ]
|
||||
then
|
||||
echo "$a -lt $b: a 小于 b"
|
||||
else
|
||||
echo "$a -lt $b: a 不小于 b"
|
||||
fi
|
||||
if [ $a -ge $b ]
|
||||
then
|
||||
echo "$a -ge $b: a 大于或等于 b"
|
||||
else
|
||||
echo "$a -ge $b: a 小于 b"
|
||||
fi
|
||||
if [ $a -le $b ]
|
||||
then
|
||||
echo "$a -le $b: a 小于或等于 b"
|
||||
else
|
||||
echo "$a -le $b: a 大于 b"
|
||||
fi
|
37
codes/shell/string-demo.sh
Normal file
37
codes/shell/string-demo.sh
Normal file
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
################### 单引号和双引号 ###################
|
||||
################### 拼接字符串 ###################
|
||||
# 使用单引号拼接
|
||||
name1='white'
|
||||
str1='hello, '${name1}''
|
||||
str2='hello, ${name1}'
|
||||
echo ${str1}_${str2}
|
||||
# Output:
|
||||
# hello, white_hello, ${name1}
|
||||
|
||||
# 使用双引号拼接
|
||||
name2="black"
|
||||
str3="hello, "${name2}""
|
||||
str4="hello, ${name2}"
|
||||
echo ${str3}_${str4}
|
||||
# Output:
|
||||
# hello, black_hello, black
|
||||
|
||||
################### 获取字符串长度 ###################
|
||||
text="12345"
|
||||
echo ${#text}
|
||||
# Output:
|
||||
# 5
|
||||
|
||||
################### 获取字符串长度 ###################
|
||||
text="12345"
|
||||
echo ${text:2:2}
|
||||
# Output:
|
||||
# 34
|
||||
|
||||
################### 查找子字符串 ###################
|
||||
text="hello"
|
||||
echo `expr index "${text}" ll`
|
||||
# Output:
|
||||
# 3
|
22
codes/shell/variable-demo.sh
Normal file
22
codes/shell/variable-demo.sh
Normal file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
################### 声明变量 ###################
|
||||
word="hello"
|
||||
echo ${word}
|
||||
# Output: hello
|
||||
|
||||
################### 只读变量 ###################
|
||||
rword="hello"
|
||||
echo ${rword}
|
||||
# Output: hello
|
||||
readonly rword
|
||||
# rword="bye" # 如果放开注释,执行时会报错
|
||||
|
||||
################### 删除变量 ###################
|
||||
dword="hello" # 声明变量
|
||||
echo ${dword} # 输出变量值
|
||||
# Output: hello
|
||||
|
||||
unset dword # 删除变量
|
||||
echo ${dword}
|
||||
# Output: (空)
|
@ -1,18 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
### 如果变量为空,赋给他们默认值
|
||||
: ${VAR:='default'}
|
||||
: ${1:='first'}
|
||||
echo "\$1 : " $1
|
||||
: ${2:='second'}
|
||||
echo "\$2 : " $2
|
||||
|
||||
### 或者
|
||||
FOO=${FOO:-'default'}
|
||||
echo "FOO : " ${FOO}
|
||||
|
||||
# execute: ./positionalVariableDemo.sh big small
|
||||
# output:
|
||||
# $1 : big
|
||||
# $2 : small
|
||||
# FOO : default
|
@ -1,8 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
username="Zhang Peng" ### 声明变量
|
||||
echo ${username} ### 输出变量的值
|
||||
unset username ### 删除变量
|
||||
echo ${username} ### 输出为空
|
||||
|
||||
export HELLO="Hello World" ### 输出环境变量
|
Loading…
Reference in New Issue
Block a user