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
43f4c50461
commit
5cf5fc7b40
@ -26,13 +26,13 @@ function checkOsVersion(){
|
||||
if(($1 == 1))
|
||||
then
|
||||
platform=`uname -i`
|
||||
if [[ ${platform} != "x86_64" ]];then
|
||||
if [[ ${platform} != "x86_64" ]]; then
|
||||
echo "this script is only for 64bit Operating System !"
|
||||
exit 1
|
||||
fi
|
||||
echo "the platform is ok"
|
||||
version=`lsb_release -r |awk '{print substr($2,1,1)}'`
|
||||
if [[ ${version} != 6 ]];then
|
||||
if [[ ${version} != 6 ]]; then
|
||||
echo "this script is only for CentOS 6 !"
|
||||
exit 1
|
||||
fi
|
||||
|
@ -12,7 +12,7 @@ fi
|
||||
|
||||
echo "x=${x}, y=${y}"
|
||||
|
||||
if [[ ${x} -eq ${y} ]];then
|
||||
if [[ ${x} -eq ${y} ]]; then
|
||||
echo "${x} -eq ${y} : x 等于 y"
|
||||
else
|
||||
echo "${x} -eq ${y}: x 不等于 y"
|
||||
|
@ -12,25 +12,25 @@ fi
|
||||
|
||||
echo "x=${x}, y=${y}"
|
||||
|
||||
if [[ ${x} != ${y} ]];then
|
||||
if [[ ${x} != ${y} ]]; then
|
||||
echo "${x} != ${y} : x 不等于 y"
|
||||
else
|
||||
echo "${x} != ${y}: x 等于 y"
|
||||
fi
|
||||
|
||||
if [[ ${x} -lt 100 && ${y} -gt 15 ]];then
|
||||
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
|
||||
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
|
||||
if [[ ${x} -lt 5 || ${y} -gt 100 ]]; then
|
||||
echo "${x} 小于 5 或 ${y} 大于 100 : 返回 true"
|
||||
else
|
||||
echo "${x} 小于 5 或 ${y} 大于 100 : 返回 false"
|
||||
|
@ -12,31 +12,31 @@ fi
|
||||
|
||||
echo "x=${x}, y=${y}"
|
||||
|
||||
if [[ ${x} = ${y} ]];then
|
||||
if [[ ${x} = ${y} ]]; then
|
||||
echo "${x} = ${y} : x 等于 y"
|
||||
else
|
||||
echo "${x} = ${y}: x 不等于 y"
|
||||
fi
|
||||
|
||||
if [[ ${x} != ${y} ]];then
|
||||
if [[ ${x} != ${y} ]]; then
|
||||
echo "${x} != ${y} : x 不等于 y"
|
||||
else
|
||||
echo "${x} != ${y}: x 等于 y"
|
||||
fi
|
||||
|
||||
if [[ -z ${x} ]];then
|
||||
if [[ -z ${x} ]]; then
|
||||
echo "-z ${x} : 字符串长度为 0"
|
||||
else
|
||||
echo "-z ${x} : 字符串长度不为 0"
|
||||
fi
|
||||
|
||||
if [[ -n "${x}" ]];then
|
||||
if [[ -n "${x}" ]]; then
|
||||
echo "-n ${x} : 字符串长度不为 0"
|
||||
else
|
||||
echo "-n ${x} : 字符串长度为 0"
|
||||
fi
|
||||
|
||||
if [[ ${x} ]];then
|
||||
if [[ ${x} ]]; then
|
||||
echo "${x} : 字符串不为空"
|
||||
else
|
||||
echo "${x} : 字符串为空"
|
||||
|
@ -2,37 +2,37 @@
|
||||
|
||||
file="/etc/hosts"
|
||||
|
||||
if [[ -r ${file} ]];then
|
||||
if [[ -r ${file} ]]; then
|
||||
echo "${file} 文件可读"
|
||||
else
|
||||
echo "${file} 文件不可读"
|
||||
fi
|
||||
if [[ -w ${file} ]];then
|
||||
if [[ -w ${file} ]]; then
|
||||
echo "${file} 文件可写"
|
||||
else
|
||||
echo "${file} 文件不可写"
|
||||
fi
|
||||
if [[ -x ${file} ]];then
|
||||
if [[ -x ${file} ]]; then
|
||||
echo "${file} 文件可执行"
|
||||
else
|
||||
echo "${file} 文件不可执行"
|
||||
fi
|
||||
if [[ -f ${file} ]];then
|
||||
if [[ -f ${file} ]]; then
|
||||
echo "${file} 文件为普通文件"
|
||||
else
|
||||
echo "${file} 文件为特殊文件"
|
||||
fi
|
||||
if [[ -d ${file} ]];then
|
||||
if [[ -d ${file} ]]; then
|
||||
echo "${file} 文件是个目录"
|
||||
else
|
||||
echo "${file} 文件不是个目录"
|
||||
fi
|
||||
if [[ -s ${file} ]];then
|
||||
if [[ -s ${file} ]]; then
|
||||
echo "${file} 文件不为空"
|
||||
else
|
||||
echo "${file} 文件为空"
|
||||
fi
|
||||
if [[ -e ${file} ]];then
|
||||
if [[ -e ${file} ]]; then
|
||||
echo "${file} 文件存在"
|
||||
else
|
||||
echo "${file} 文件不存在"
|
||||
|
12
codes/shell/statement/break-demo.sh
Normal file
12
codes/shell/statement/break-demo.sh
Normal file
@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# 查找 10 以内第一个能整除 2 和 3 的正整数
|
||||
i=1
|
||||
while [[ ${i} -lt 10 ]]; do
|
||||
if [[ $((i % 3)) -eq 0 ]] && [[ $((i % 2)) -eq 0 ]]; then
|
||||
echo ${i}
|
||||
break;
|
||||
fi
|
||||
i=`expr ${i} + 1`
|
||||
done
|
||||
# Output: 6
|
41
codes/shell/statement/case-demo.sh
Normal file
41
codes/shell/statement/case-demo.sh
Normal file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
echo "input param: " $1 $2 $3
|
||||
|
||||
x=0
|
||||
if [[ -n $1 ]]; then
|
||||
x=$1
|
||||
fi
|
||||
|
||||
y=0
|
||||
if [[ -n $2 ]]; then
|
||||
y=$2
|
||||
fi
|
||||
|
||||
oper=""
|
||||
if [[ -n $3 ]]; then
|
||||
oper=$3
|
||||
fi
|
||||
|
||||
exec
|
||||
case ${oper} in
|
||||
"+")
|
||||
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 ${x} / ${y}`
|
||||
echo "${x} / ${y} = ${val}"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown oper!"
|
||||
;;
|
||||
esac
|
@ -1,18 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
echo "input param: " $1
|
||||
|
||||
case $1 in
|
||||
"jpg" | "jpeg")
|
||||
echo "It's image with jpeg extension."
|
||||
;;
|
||||
"png")
|
||||
echo "It's image with png extension."
|
||||
;;
|
||||
"gif")
|
||||
echo "Oh, it's a giphy!"
|
||||
;;
|
||||
*)
|
||||
echo "Woops! It's not image!"
|
||||
;;
|
||||
esac
|
@ -7,3 +7,9 @@ for (( i = 0; i < 10; i ++ )); do
|
||||
fi
|
||||
echo ${i}
|
||||
done
|
||||
# Output:
|
||||
# 1
|
||||
# 3
|
||||
# 5
|
||||
# 7
|
||||
# 9
|
37
codes/shell/statement/for-demo.sh
Normal file
37
codes/shell/statement/for-demo.sh
Normal file
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
################### for 语句 ###################
|
||||
echo "print 0 to 9"
|
||||
for (( j = 0; j < 10; j ++ )); do
|
||||
echo ${j}
|
||||
done
|
||||
# Output:
|
||||
# print 0 to 9
|
||||
# 0
|
||||
# 1
|
||||
# 2
|
||||
# 3
|
||||
# 4
|
||||
# 5
|
||||
# 6
|
||||
# 7
|
||||
# 8
|
||||
# 9
|
||||
|
||||
################### for in 语句 ###################
|
||||
echo "print 1 to 5"
|
||||
for i in {1..5}; do echo ${i}; done
|
||||
# Output:
|
||||
# print 1 to 5
|
||||
# 1
|
||||
# 2
|
||||
# 3
|
||||
# 4
|
||||
# 5
|
||||
|
||||
################### for in 语句遍历文件 ###################
|
||||
DIR=/home/zp
|
||||
for FILE in ${DIR}/*.sh; do
|
||||
mv "$FILE" "${DIR}/scripts"
|
||||
done
|
||||
# 将 /home/zp 目录下所有 sh 文件拷贝到 /home/zp/scripts
|
@ -1,11 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
echo "print 1 to 5"
|
||||
for i in {1..5}; do
|
||||
echo $i;
|
||||
done
|
||||
|
||||
echo "print 0 to 9"
|
||||
for (( i = 0; i < 10; i ++ )); do
|
||||
echo $i
|
||||
done
|
@ -1,6 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
for FILE in $HOME/; do
|
||||
mv "$FILE" "./"
|
||||
chmod +x "./${FILE}"
|
||||
done
|
33
codes/shell/statement/if-demo.sh
Normal file
33
codes/shell/statement/if-demo.sh
Normal file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
################### if 语句 ###################
|
||||
# 写成一行
|
||||
if [[ 1 -eq 1 ]]; then echo "1 -eq 1 result is: true"; fi
|
||||
# Output: 1 -eq 1 result is: true
|
||||
|
||||
# 写成多行
|
||||
if [[ "abc" -eq "abc" ]]
|
||||
then
|
||||
echo ""abc" -eq "abc" result is: true"
|
||||
fi
|
||||
# Output: abc -eq abc result is: true
|
||||
|
||||
################### if else 语句 ###################
|
||||
if [[ 2 -ne 1 ]]; then
|
||||
echo "true"
|
||||
else
|
||||
echo "false"
|
||||
fi
|
||||
# Output: true
|
||||
|
||||
################### if elif else 语句 ###################
|
||||
x=10
|
||||
y=20
|
||||
if [[ ${x} > ${y} ]]; then
|
||||
echo "${x} > ${y}"
|
||||
elif [[ ${x} < ${y} ]]; then
|
||||
echo "${x} < ${y}"
|
||||
else
|
||||
echo "${x} = ${y}"
|
||||
fi
|
||||
# Output: 10 < 20
|
@ -1,27 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
if [[ -z $1 ]]; then
|
||||
echo "please input first param";
|
||||
exit
|
||||
fi
|
||||
|
||||
if [[ -z $2 ]]; then
|
||||
echo "please input second param";
|
||||
exit
|
||||
fi
|
||||
|
||||
if [[ $1 > $2 ]]; then
|
||||
echo "\$1 > \$2";
|
||||
elif [[ $1 < $2 ]]; then
|
||||
echo "\$1 < \$2";
|
||||
else
|
||||
echo "\$1 != \$2";
|
||||
fi
|
||||
|
||||
# execute: ./ifDemo.sh abc abc
|
||||
# output:
|
||||
# $1 == $2
|
||||
|
||||
# execute: ./ifDemo.sh abc ab
|
||||
# output:
|
||||
# $1 != $2
|
@ -10,5 +10,5 @@ case ${ITEM} in
|
||||
gem) gem install ${PACKAGE} ;;
|
||||
pip) pip install ${PACKAGE} ;;
|
||||
esac
|
||||
break ### 避免无限循环
|
||||
break # 避免无限循环
|
||||
done
|
13
codes/shell/statement/until-demo.sh
Normal file
13
codes/shell/statement/until-demo.sh
Normal file
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
x=0
|
||||
until [[ ${x} -ge 5 ]]; do
|
||||
echo ${x}
|
||||
x=`expr ${x} + 1`
|
||||
done
|
||||
# Output:
|
||||
# 0
|
||||
# 1
|
||||
# 2
|
||||
# 3
|
||||
# 4
|
@ -5,5 +5,16 @@ x=0
|
||||
### x小于10
|
||||
while [[ ${x} -lt 10 ]]; do
|
||||
echo $((x * x))
|
||||
x=$((x + 1)) ### x加1
|
||||
x=$((x + 1))
|
||||
done
|
||||
# Output:
|
||||
# 0
|
||||
# 1
|
||||
# 4
|
||||
# 9
|
||||
# 16
|
||||
# 25
|
||||
# 36
|
||||
# 49
|
||||
# 64
|
||||
# 81
|
Loading…
Reference in New Issue
Block a user