添加示例

pull/11/head
Zhang Peng 2017-11-17 17:06:52 +08:00
parent b34204286c
commit f6054565c1
9 changed files with 118 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
for FILE in $HOME/; do
mv "$FILE" "./"
chmod +x "./${FILE}"
done

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
username="Zhang Peng" ### 声明变量
echo ${username} ### 输出变量的值
unset username ### 删除变量
echo ${username} ### 输出为空
export HELLO="Hello World" ### 输出环境变量