shell 编程初级

shell编程的简单代码 一些基础代码 直接上代码

#!/bin/bash
myUrl="gggggggg"
# 只读变量设置
# readonly myUrl
echo "myUrl =" ${myUrl}
unset myUrl
echo 'myUrl = ' ${myUrli} # string splice
your_name='wanghuixi'
str="hello I know you are \"$your_name\" ! \n"
echo -e $str greeting="hello,"$your_name"!"
greeting1="hello,${your_name} !"
echo $greeting $greeting1 greeting2='hello, '$your_name'!'
greeting3='hello, '${your_name}'ddd!'
echo $greeting2 $greeting3 echo ${#your_name} echo ${your_name::} string="runoob is a great site"
echo `expr index "$string" io` array_name=(value0 value1 value2 value3)
echo $array_name[]
echo ${array_name[@]} #获取数组的长度
echo ${#array_name[@]} echo ${#array_name[*]}
# 取得数组单个元素的长度
echo ${#array_name[]} :<<.
zhu shi
.
echo "can shu: $0"
echo "can shu: $1"
echo "can shu num : $#" echo "proess ID is : $$"
echo "can shu $* " echo "-- \S* --"
for i in "$*"; do
echo $i
done echo "-- \$@ --"
for i in "$@";
do
echo $@
done :<<.
array
write
readd
. my_array=(A B "ccc" D)
echo "first :${my_array[0]}"
echo "second: ${my_array[2]}" echo " ${my_array[*]}"
echo " ${my_array[@]}" echo " ${#my_array[*]}"
echo " ${#my_array[@]}"
:<<.
ji ben yun suan fu .
val=`expr +`
echo "val: $val" a=
b= val=`expr $a - $b`
echo "a -b : $val" val=`expr $a + $b`
echo "a + b : $val" if [ $a == $b ]
then
echo " a = b "
fi if [ $a != $b ]
then
echo "a != b"
fi
:<<.
file yun suan
文件测试运算符
.
# echo
# read name
# echo "$name It is a test" echo "ok! \n" # -e 开启转义 \c no line
echo -e "ok \c" echo "it is a test " > myfile echo `date` printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234
printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543
printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876 for loop in
do
echo "The value is : $loop"
done for str in "this is a string "
do
echo $str
done int=
while(($int<=))
do
echo $int
let "int++"
done
echo "##########"
:<<.
echo -n '输入你最喜欢的网站名:'
while read FILM
do
echo "$FILM"
done
.
a=
until [ ! $a -lt ]
do
echo $a
a=`expr $a + `
done # case
echo "输入 1 到 4 之间的数字"
echo "你输入的数字为:"
read aNum
case $aNum in
) echo '';;
) echo '';;
) echo '';;
) echo '';;
*) echo '你没有输入 1 到 4 之间的数字';;
esac funWithParam(){
echo "第一个参数为 $1 !"
echo "第二个参数为 $2 !"
echo "第十个参数为 $10 !"
echo "第十个参数为 ${10} !"
echo "第十一个参数为 ${11} !"
echo "参数总数有 $# 个!"
echo "作为一个字符串输出所有参数 $* !"
}
funWithParam

运行结果:

myUrl = gggggggg
myUrl =
hello I know you are "wanghuixi" ! hello,wanghuixi! hello,wanghuixi !
hello, wanghuixi! hello, wanghuixiddd! an value0[]
value0 value1 value2 value3 can shu: ./text.sh
can shu:
can shu num :
proess ID is :
can shu
-- \S* -- -- $@ --
first :A
second: ccc
A B ccc D
A B ccc D val: +
a -b : -
a + b :
a != b
ok! \n
ok Sun Apr :: PDT
姓名 性别 体重kg
郭靖 男 66.12
杨过 男 48.65
郭芙 女 47.99
The value is :
The value is :
The value is :
The value is :
The value is :
this is a string ########## 输入 到 之间的数字
你输入的数字为: 第一个参数为 !
第二个参数为 !
第十个参数为 !
第十个参数为 !
第十一个参数为 !
参数总数有 个!
作为一个字符串输出所有参数 !
上一篇:cocos2d-x中处理touch事件


下一篇:慎用preg_replace危险的/e修饰符(一句话后门常用)