```cpp
#!/bin/bash
echo "Hello World !"
echo "This is a test"
# variable: no blank-space
myName="Wite_Chen"
echo $myName
# print the length
echo ${#myName}
# concat string
echo "hello $myName, nice to meet you"
#----array
color=(red block white)
length=${#color[*]} # store the length of array
echo "length of array: $length"
# print the data
for var in ${color[@]};do
echo $var
done
# multi-line comment
:<<EOF
echo "comment"
EOF
# delete character
:<<EOF
#、## 表示从左边开始删除。一个 # 表示从左边删除到第一个指定的字符;两个 # 表示从左边删除到最后一个指定的字符。
%、%% 表示从右边开始删除。一个 % 表示从右边删除到第一个指定的字符;两个 % 表示从左边删除到最后一个指定的字符。
删除包括了指定的字符本身
EOF
var="http://www.runoob.com/linux/linux-shell-variable.html"
s1=${var%%t*}
s2=${var%t*}
s3=${var%%.*}
s4=${var#*/}
s5=${var##*/}
echo $s1 $s2 $s3 $s4 $s5
# get the paras for scripts
echo "Shell 传递参数实例, 参数个数: $#"
echo "执行的文件名:$0"
echo "第一个参数为:$1"
# $@和$*都能获取到参数信息,$*是保存到一个字符串,$@分开保存
nPara=0
for para in "$@";do
echo "para[$nPara]: $para"
let nPara++
done
# expr 是一款表达式计算工具,使用它能完成表达式的求值操作。
sum=`expr 1 + 1` # blank-space between in parameters
echo "1 + 1 = $sum"
num1=100
num2=200
#if [ $num1 != $num2 ];then
if [ $num1 -eq $num2 ];then
echo "num1[$num1] equle to num2[$num2].";
elif [ $num1 -lt $num2 ];then
echo "num1[$num1] less than num2[$num2].";
else
echo "num1[$num1] greater than num2[$num2].";
fi
# while
cond=5;
while(( $cond>=0 ));do
echo $cond;
let cond--;
done;
# case: 其间所有命令开始执行直至 ;;
echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
choice=5;
#read choice
case $choice in
1) echo '你选择了 1'
;;
2) echo '你选择了 2'
;;
3) echo '你选择了 3'
;;
4) echo '你选择了 4'
;;
5|6) echo "你选择了 5|6, $choice"
;;
*) echo '你没有输入 1 到 4 之间的数字'
;;
esac
# -e: 开启转义字符
echo -e "Hello \nWorld!";
# -p 输入提示文字
# -n 输入字符长度限制(达到6位,自动结束)
# -t 输入限时
# -s 隐藏输入内容
#read -p "请输入密码: " -n 6 -t 5 -s password
#echo -e "\npassword is $password"
#echo $password>password.txt # clear the data and write
# append
#echo $password>>password.txt
# print
age=27;
name=wite;
printf "name[%s], age[%d]\n" $name $age
# create file/directory
curr_dir=$(pwd)
echo $curr_dir
dir="$curr_dir/data"
if [ -d $dir ];then
echo "Directory[$dir] exist. ";
create_ret=1;
#`mkdir $dir` || let create_ret=0;
mkdir $dir;
echo "create directory result: $?";
rm -r $dir;
else
echo "Directory[$dir] not exist. ";
creat_dir=`mkdir $dir`;
echo "create directory result: $creat_dir";
fi
# function
PrintData()
{
echo "参数个数:$#";
echo "This is function test";
echo "参数1 $1"
echo "参数2 $2"
# 参数>=10, ${n}来获取参数
#echo "参数10: ${10}";
}
PrintData 1 2 3; # invoke function
# include source file
. ./data.sh # or source ./data.sh
echo "File declared variable: $data"