Skip to content

Commit 5cf5fc7

Browse files
committed
update shell scripts
1 parent 43f4c50 commit 5cf5fc7

17 files changed

Lines changed: 174 additions & 83 deletions

codes/linux/ops/main.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ function checkOsVersion(){
2626
if(($1 == 1))
2727
then
2828
platform=`uname -i`
29-
if [[ ${platform} != "x86_64" ]];then
29+
if [[ ${platform} != "x86_64" ]]; then
3030
echo "this script is only for 64bit Operating System !"
3131
exit 1
3232
fi
3333
echo "the platform is ok"
3434
version=`lsb_release -r |awk '{print substr($2,1,1)}'`
35-
if [[ ${version} != 6 ]];then
35+
if [[ ${version} != 6 ]]; then
3636
echo "this script is only for CentOS 6 !"
3737
exit 1
3838
fi

codes/shell/operator/operator-demo2.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fi
1212

1313
echo "x=${x}, y=${y}"
1414

15-
if [[ ${x} -eq ${y} ]];then
15+
if [[ ${x} -eq ${y} ]]; then
1616
echo "${x} -eq ${y} : x 等于 y"
1717
else
1818
echo "${x} -eq ${y}: x 不等于 y"

codes/shell/operator/operator-demo3.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ fi
1212

1313
echo "x=${x}, y=${y}"
1414

15-
if [[ ${x} != ${y} ]];then
15+
if [[ ${x} != ${y} ]]; then
1616
echo "${x} != ${y} : x 不等于 y"
1717
else
1818
echo "${x} != ${y}: x 等于 y"
1919
fi
2020

21-
if [[ ${x} -lt 100 && ${y} -gt 15 ]];then
21+
if [[ ${x} -lt 100 && ${y} -gt 15 ]]; then
2222
echo "${x} 小于 100 且 ${y} 大于 15 : 返回 true"
2323
else
2424
echo "${x} 小于 100 且 ${y} 大于 15 : 返回 false"
2525
fi
2626

27-
if [[ ${x} -lt 100 || ${y} -gt 100 ]];then
27+
if [[ ${x} -lt 100 || ${y} -gt 100 ]]; then
2828
echo "${x} 小于 100 或 ${y} 大于 100 : 返回 true"
2929
else
3030
echo "${x} 小于 100 或 ${y} 大于 100 : 返回 false"
3131
fi
3232

33-
if [[ ${x} -lt 5 || ${y} -gt 100 ]];then
33+
if [[ ${x} -lt 5 || ${y} -gt 100 ]]; then
3434
echo "${x} 小于 5 或 ${y} 大于 100 : 返回 true"
3535
else
3636
echo "${x} 小于 5 或 ${y} 大于 100 : 返回 false"

codes/shell/operator/operator-demo5.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ fi
1212

1313
echo "x=${x}, y=${y}"
1414

15-
if [[ ${x} = ${y} ]];then
15+
if [[ ${x} = ${y} ]]; then
1616
echo "${x} = ${y} : x 等于 y"
1717
else
1818
echo "${x} = ${y}: x 不等于 y"
1919
fi
2020

21-
if [[ ${x} != ${y} ]];then
21+
if [[ ${x} != ${y} ]]; then
2222
echo "${x} != ${y} : x 不等于 y"
2323
else
2424
echo "${x} != ${y}: x 等于 y"
2525
fi
2626

27-
if [[ -z ${x} ]];then
27+
if [[ -z ${x} ]]; then
2828
echo "-z ${x} : 字符串长度为 0"
2929
else
3030
echo "-z ${x} : 字符串长度不为 0"
3131
fi
3232

33-
if [[ -n "${x}" ]];then
33+
if [[ -n "${x}" ]]; then
3434
echo "-n ${x} : 字符串长度不为 0"
3535
else
3636
echo "-n ${x} : 字符串长度为 0"
3737
fi
3838

39-
if [[ ${x} ]];then
39+
if [[ ${x} ]]; then
4040
echo "${x} : 字符串不为空"
4141
else
4242
echo "${x} : 字符串为空"

codes/shell/operator/operator-demo6.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22

33
file="/etc/hosts"
44

5-
if [[ -r ${file} ]];then
5+
if [[ -r ${file} ]]; then
66
echo "${file} 文件可读"
77
else
88
echo "${file} 文件不可读"
99
fi
10-
if [[ -w ${file} ]];then
10+
if [[ -w ${file} ]]; then
1111
echo "${file} 文件可写"
1212
else
1313
echo "${file} 文件不可写"
1414
fi
15-
if [[ -x ${file} ]];then
15+
if [[ -x ${file} ]]; then
1616
echo "${file} 文件可执行"
1717
else
1818
echo "${file} 文件不可执行"
1919
fi
20-
if [[ -f ${file} ]];then
20+
if [[ -f ${file} ]]; then
2121
echo "${file} 文件为普通文件"
2222
else
2323
echo "${file} 文件为特殊文件"
2424
fi
25-
if [[ -d ${file} ]];then
25+
if [[ -d ${file} ]]; then
2626
echo "${file} 文件是个目录"
2727
else
2828
echo "${file} 文件不是个目录"
2929
fi
30-
if [[ -s ${file} ]];then
30+
if [[ -s ${file} ]]; then
3131
echo "${file} 文件不为空"
3232
else
3333
echo "${file} 文件为空"
3434
fi
35-
if [[ -e ${file} ]];then
35+
if [[ -e ${file} ]]; then
3636
echo "${file} 文件存在"
3737
else
3838
echo "${file} 文件不存在"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
# 查找 10 以内第一个能整除 2 和 3 的正整数
4+
i=1
5+
while [[ ${i} -lt 10 ]]; do
6+
if [[ $((i % 3)) -eq 0 ]] && [[ $((i % 2)) -eq 0 ]]; then
7+
echo ${i}
8+
break;
9+
fi
10+
i=`expr ${i} + 1`
11+
done
12+
# Output: 6

codes/shell/statement/case-demo.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
echo "input param: " $1 $2 $3
4+
5+
x=0
6+
if [[ -n $1 ]]; then
7+
x=$1
8+
fi
9+
10+
y=0
11+
if [[ -n $2 ]]; then
12+
y=$2
13+
fi
14+
15+
oper=""
16+
if [[ -n $3 ]]; then
17+
oper=$3
18+
fi
19+
20+
exec
21+
case ${oper} in
22+
"+")
23+
val=`expr ${x} + ${y}`
24+
echo "${x} + ${y} = ${val}"
25+
;;
26+
"-")
27+
val=`expr ${x} - ${y}`
28+
echo "${x} - ${y} = ${val}"
29+
;;
30+
"*")
31+
val=`expr ${x} \* ${y}`
32+
echo "${x} * ${y} = ${val}"
33+
;;
34+
"/")
35+
val=`expr ${x} / ${y}`
36+
echo "${x} / ${y} = ${val}"
37+
;;
38+
*)
39+
echo "Unknown oper!"
40+
;;
41+
esac

codes/shell/statement/caseDemo.sh

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ for (( i = 0; i < 10; i ++ )); do
77
fi
88
echo ${i}
99
done
10+
# Output:
11+
# 1
12+
# 3
13+
# 5
14+
# 7
15+
# 9

codes/shell/statement/for-demo.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
################### for 语句 ###################
4+
echo "print 0 to 9"
5+
for (( j = 0; j < 10; j ++ )); do
6+
echo ${j}
7+
done
8+
# Output:
9+
# print 0 to 9
10+
# 0
11+
# 1
12+
# 2
13+
# 3
14+
# 4
15+
# 5
16+
# 6
17+
# 7
18+
# 8
19+
# 9
20+
21+
################### for in 语句 ###################
22+
echo "print 1 to 5"
23+
for i in {1..5}; do echo ${i}; done
24+
# Output:
25+
# print 1 to 5
26+
# 1
27+
# 2
28+
# 3
29+
# 4
30+
# 5
31+
32+
################### for in 语句遍历文件 ###################
33+
DIR=/home/zp
34+
for FILE in ${DIR}/*.sh; do
35+
mv "$FILE" "${DIR}/scripts"
36+
done
37+
# 将 /home/zp 目录下所有 sh 文件拷贝到 /home/zp/scripts

0 commit comments

Comments
 (0)