#!/bin/sh
echo "\n........字符串............"
my_name="lanwh6" #定义变量
#readonly my_name 设置只读不能重新赋值,不能unset
echo "hello world, ${my_name}"
echo "${my_name}的长度:\c"
echo ${my_name}
#echo ${my_name:1:2} #截取字符串
echo "${my_name}被unset(删除后的值):\c"
unset my_name
echo $my_name
echo "\n........数组............"
#array_name=(1,2,3,4) bash不兼容sh
array_name[0]="lan"
array_name[1]="w"
array_name[2]="h"
array_name[3]=666
echo ${array_name[1]}
echo ${array_name[@]} #打印所有
echo ${array_name[*]}
echo ${#array_name[@]} #打印数组长度
echo ${#array_name[*]}
echo ${#array_name[1]} #打印数组单个元素长度
echo ${#array_name[3]}
echo "\n........传参............"
echo "文件名:$0"
echo "参数1:$1"
echo "参数2:$2"
if [ $1 ]
then
echo "参数1有值"
else
echo "参数1为空"
fi
if [ $2 ]
then
echo "参数2有值"
else
echo "参数2为空"
fi
if[ "$1" != "" -o "$2" != "" ]
then
echo ' "$@"循环打印传入参数: '
for i in "$@" ; do
echo $i
done
echo ' "$*"循环打印传入参数: '
for i in "$*" ; do
echo $i
done
fi
echo "\n........运算符..........."
a=10
b=12
result=`expr $a + $b` # +、-、*、/、%
echo "${a}+${b}=${result}"
result=`expr $a - $b`
echo "${a}-${b}=${result}"
result=`expr $a \* $b`
echo "${a}*${b}=${result}"
result=`expr $a / $b`
echo "${a}/${b}=${result}"
result=`expr $a % $b`
echo "${a}%${b}=${result}"
echo "\n${a}+${b}=$(($a+$b))"
echo "\n${a}-${b}=$(($a-$b))"
echo "\n${a}*${b}=$(($a*$b))"
echo "\n${a}/${b}=$(($a/$b))"
echo "\n${a}%${b}=$(($a%$b))"
echo "\n........流程控制与test..........."
echo "文件判断:-e存在、-r可读、-x可执行、-d目录、-f文件、-s文件是否为空"
echo "字符串判断:!、-o或、-a与、=、!=、-z、-n、str"
echo "数字判断:-eq、-ne、-gt、-lt、-ge、-le"
echo "\n........函数.........."
test1(){
echo "test1函数"
}
test1 #调用test1函数
test2(){
echo "请输入数字1:"
read aa
echo "请输入数字2:"
read bb
return $(($aa+$bb))
}
test2
echo "函数test2的和是:$?"
echo "函数test3"
test3(){
echo $1
echo ${10}
}
test3 1 2 3 4 5 6 7 8 9 55
echo "\n........echo使用.........."
echo '单引号不会转义:$a'
echo ' `date`打印当前日期:\c'
echo `date`
echo "\n........导入外部文件test1.sh.........."
../test1.sh # source ./test1.sh也可以
echo "百度网址:${url}"