diff --git a/codes/linux/ops/main.sh b/codes/linux/ops/main.sh index 5f09006..d7c1f62 100644 --- a/codes/linux/ops/main.sh +++ b/codes/linux/ops/main.sh @@ -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 diff --git a/codes/shell/operator/operator-demo2.sh b/codes/shell/operator/operator-demo2.sh index 05cd0b2..1ebc849 100644 --- a/codes/shell/operator/operator-demo2.sh +++ b/codes/shell/operator/operator-demo2.sh @@ -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" diff --git a/codes/shell/operator/operator-demo3.sh b/codes/shell/operator/operator-demo3.sh index 68dc499..e127fa5 100644 --- a/codes/shell/operator/operator-demo3.sh +++ b/codes/shell/operator/operator-demo3.sh @@ -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" diff --git a/codes/shell/operator/operator-demo5.sh b/codes/shell/operator/operator-demo5.sh index 828803c..b4ee6bd 100644 --- a/codes/shell/operator/operator-demo5.sh +++ b/codes/shell/operator/operator-demo5.sh @@ -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} : 字符串为空" diff --git a/codes/shell/operator/operator-demo6.sh b/codes/shell/operator/operator-demo6.sh index 954d84e..9c810eb 100644 --- a/codes/shell/operator/operator-demo6.sh +++ b/codes/shell/operator/operator-demo6.sh @@ -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} 文件不存在" diff --git a/codes/shell/statement/break-demo.sh b/codes/shell/statement/break-demo.sh new file mode 100644 index 0000000..eba7c16 --- /dev/null +++ b/codes/shell/statement/break-demo.sh @@ -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 diff --git a/codes/shell/statement/case-demo.sh b/codes/shell/statement/case-demo.sh new file mode 100644 index 0000000..d8302ed --- /dev/null +++ b/codes/shell/statement/case-demo.sh @@ -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 diff --git a/codes/shell/statement/caseDemo.sh b/codes/shell/statement/caseDemo.sh deleted file mode 100644 index e74d89c..0000000 --- a/codes/shell/statement/caseDemo.sh +++ /dev/null @@ -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 diff --git a/codes/shell/statement/continueDemo.sh b/codes/shell/statement/continue-demo.sh similarity index 80% rename from codes/shell/statement/continueDemo.sh rename to codes/shell/statement/continue-demo.sh index 3b63c50..035d95c 100644 --- a/codes/shell/statement/continueDemo.sh +++ b/codes/shell/statement/continue-demo.sh @@ -7,3 +7,9 @@ for (( i = 0; i < 10; i ++ )); do fi echo ${i} done +# Output: +# 1 +# 3 +# 5 +# 7 +# 9 diff --git a/codes/shell/statement/for-demo.sh b/codes/shell/statement/for-demo.sh new file mode 100644 index 0000000..ba1aec9 --- /dev/null +++ b/codes/shell/statement/for-demo.sh @@ -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 diff --git a/codes/shell/statement/forDemo.sh b/codes/shell/statement/forDemo.sh deleted file mode 100644 index f1e70cc..0000000 --- a/codes/shell/statement/forDemo.sh +++ /dev/null @@ -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 diff --git a/codes/shell/statement/forDemo02.sh b/codes/shell/statement/forDemo02.sh deleted file mode 100644 index 7d7a4f2..0000000 --- a/codes/shell/statement/forDemo02.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash - -for FILE in $HOME/; do - mv "$FILE" "./" - chmod +x "./${FILE}" -done diff --git a/codes/shell/statement/if-demo.sh b/codes/shell/statement/if-demo.sh new file mode 100644 index 0000000..62b7031 --- /dev/null +++ b/codes/shell/statement/if-demo.sh @@ -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 diff --git a/codes/shell/statement/ifDemo.sh b/codes/shell/statement/ifDemo.sh deleted file mode 100644 index 33b417b..0000000 --- a/codes/shell/statement/ifDemo.sh +++ /dev/null @@ -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 diff --git a/codes/shell/statement/selectDemo.sh b/codes/shell/statement/select-demo.sh similarity index 91% rename from codes/shell/statement/selectDemo.sh rename to codes/shell/statement/select-demo.sh index e4e6d41..956ac8f 100644 --- a/codes/shell/statement/selectDemo.sh +++ b/codes/shell/statement/select-demo.sh @@ -10,5 +10,5 @@ case ${ITEM} in gem) gem install ${PACKAGE} ;; pip) pip install ${PACKAGE} ;; esac -break ### 避免无限循环 +break # 避免无限循环 done diff --git a/codes/shell/statement/until-demo.sh b/codes/shell/statement/until-demo.sh new file mode 100644 index 0000000..320b590 --- /dev/null +++ b/codes/shell/statement/until-demo.sh @@ -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 diff --git a/codes/shell/statement/whileDemo.sh b/codes/shell/statement/while-demo.sh similarity index 60% rename from codes/shell/statement/whileDemo.sh rename to codes/shell/statement/while-demo.sh index 089eab0..375bed9 100644 --- a/codes/shell/statement/whileDemo.sh +++ b/codes/shell/statement/while-demo.sh @@ -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