From f6054565c1e7643bb946fd2026775f9ec23ee596 Mon Sep 17 00:00:00 2001 From: Zhang Peng Date: Fri, 17 Nov 2017 17:06:52 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/bash/statement/caseDemo.sh | 18 +++++++++++++ codes/bash/statement/continueDemo.sh | 9 +++++++ codes/bash/statement/forDemo.sh | 11 ++++++++ codes/bash/statement/forDemo02.sh | 6 +++++ codes/bash/statement/ifDemo.sh | 25 +++++++++++++++++++ codes/bash/statement/selectDemo.sh | 14 +++++++++++ codes/bash/statement/whileDemo.sh | 9 +++++++ codes/bash/variable/positionalVariableDemo.sh | 18 +++++++++++++ codes/bash/variable/variableDemo.sh | 8 ++++++ 9 files changed, 118 insertions(+) create mode 100644 codes/bash/statement/caseDemo.sh create mode 100644 codes/bash/statement/continueDemo.sh create mode 100644 codes/bash/statement/forDemo.sh create mode 100644 codes/bash/statement/forDemo02.sh create mode 100644 codes/bash/statement/ifDemo.sh create mode 100644 codes/bash/statement/selectDemo.sh create mode 100644 codes/bash/statement/whileDemo.sh create mode 100644 codes/bash/variable/positionalVariableDemo.sh create mode 100644 codes/bash/variable/variableDemo.sh diff --git a/codes/bash/statement/caseDemo.sh b/codes/bash/statement/caseDemo.sh new file mode 100644 index 0000000..e74d89c --- /dev/null +++ b/codes/bash/statement/caseDemo.sh @@ -0,0 +1,18 @@ +#!/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/bash/statement/continueDemo.sh b/codes/bash/statement/continueDemo.sh new file mode 100644 index 0000000..3b63c50 --- /dev/null +++ b/codes/bash/statement/continueDemo.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +# 打印10以内的奇数 +for (( i = 0; i < 10; i ++ )); do + if [[ $((i % 2)) -eq 0 ]]; then + continue; + fi + echo ${i} +done diff --git a/codes/bash/statement/forDemo.sh b/codes/bash/statement/forDemo.sh new file mode 100644 index 0000000..f1e70cc --- /dev/null +++ b/codes/bash/statement/forDemo.sh @@ -0,0 +1,11 @@ +#!/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/bash/statement/forDemo02.sh b/codes/bash/statement/forDemo02.sh new file mode 100644 index 0000000..7d7a4f2 --- /dev/null +++ b/codes/bash/statement/forDemo02.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +for FILE in $HOME/; do + mv "$FILE" "./" + chmod +x "./${FILE}" +done diff --git a/codes/bash/statement/ifDemo.sh b/codes/bash/statement/ifDemo.sh new file mode 100644 index 0000000..5047438 --- /dev/null +++ b/codes/bash/statement/ifDemo.sh @@ -0,0 +1,25 @@ +#!/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"; +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/bash/statement/selectDemo.sh b/codes/bash/statement/selectDemo.sh new file mode 100644 index 0000000..e4e6d41 --- /dev/null +++ b/codes/bash/statement/selectDemo.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +PS3="Choose the package manager: " +select ITEM in bower npm gem pip +do +echo -n "Enter the package name: " && read PACKAGE +case ${ITEM} in + bower) bower install ${PACKAGE} ;; + npm) npm install ${PACKAGE} ;; + gem) gem install ${PACKAGE} ;; + pip) pip install ${PACKAGE} ;; +esac +break ### 避免无限循环 +done diff --git a/codes/bash/statement/whileDemo.sh b/codes/bash/statement/whileDemo.sh new file mode 100644 index 0000000..089eab0 --- /dev/null +++ b/codes/bash/statement/whileDemo.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +### 0到9之间每个数的平方 +x=0 +### x小于10 +while [[ ${x} -lt 10 ]]; do + echo $((x * x)) + x=$((x + 1)) ### x加1 +done diff --git a/codes/bash/variable/positionalVariableDemo.sh b/codes/bash/variable/positionalVariableDemo.sh new file mode 100644 index 0000000..41a0b68 --- /dev/null +++ b/codes/bash/variable/positionalVariableDemo.sh @@ -0,0 +1,18 @@ +#!/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 diff --git a/codes/bash/variable/variableDemo.sh b/codes/bash/variable/variableDemo.sh new file mode 100644 index 0000000..6384e85 --- /dev/null +++ b/codes/bash/variable/variableDemo.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +username="Zhang Peng" ### 声明变量 +echo ${username} ### 输出变量的值 +unset username ### 删除变量 +echo ${username} ### 输出为空 + +export HELLO="Hello World" ### 输出环境变量