读CU论坛shell十三问后进行的几次小实验,备忘一下!
SHELL十三问地址:http://bbs.chinaunix.net/thread-218853-1-1.html
#!/bin/bash
function func_com()
{
local a=1
local b="2c3"
echo ${a}b
echo $ab
}
function func_array()
{
echo "##### func_array #####"
local PATH="/home/stone"
source $PATH/include/tt.ini
#echo $name
local srand=(1 3 5 2)
local srand_num=${#srand[*]}
for((i=0;i<$srand_num;i++));do
echo $((srand[i]+age))
done
echo "##### func_array #####"
}
function func_for()
{
echo "##### func_for #####"
local bin_path="/usr"
local array=(2 3 5 7 9)
local array_n=${#array[*]}
for dir in $(ls $bin_path);
do
echo $dir
done
for ((i=0;i<$array_n;i++));
do
echo ${array[i]}
done
echo "##### func_for #####"
}
function func_getlen()
{
echo "$#"
}
function func_param()
{
echo "##### func_len \$@ #####"
func_getlen "$@"
echo "##### func_len \$@ #####"
echo "##### func_len \$* #####"
func_getlen "$*"
echo "##### func_len \$* #####"
}
function func_calc()
{
echo "##### func_calc \$* #####"
local a=$RANDOM
local b=4
local c=8
d=$((a+b))
echo $d
e=`expr $a + 7`
echo $e
echo "##### func_calc \$* #####"
}
function func_exit()
{
local str1="abc"
local str2="def"
echo $str1
exit 0
echo $str2
}
function func_if()
{
local a=1
local b=3
local str="abc"
if [ ! $a=1 ];then
echo "equal"
elif [[ $str="abc" ]]
then
echo "str equal"
else
echo "else"
fi
}
function func_read()
{
op=""
while [[ $op != "q" ]]
do
read -p "input one word 'a,b,q'! " op
case $op in
a)
echo "the word is $op";;
b)
echo "b";;
q)
exit;;
*)
echo "input err!"
esac
done
}
function func_getopt()
{
ARGS="`getopt -u -o "ht:a:n:" -l "help,time:,age:,number:" -- "$@"`"
[ $? -ne 0 ] && usage
set -- ${ARGS}
while [ true ] ;
do
case $1 in
-h|--help)
usage
shift
;;
-t|--time)
echo "time is $2"
shift
;;
-a|--age)
echo "age is $2"
shift
;;
-n|--number)
echo "number is $2"
shift
;;
--)
shift
break
;;
*)
usage
;;
esac
shift
done
}
main()
{
func_com
#func_for
#func_array
#func_param 1 a 2 b
#func_calc
#func_exit
#func_if
#func_read
#case while getopt
#func_getopt -t 20150320 -a 25 --number 88
}
main