shell编程的简单代码 一些基础代码 直接上代码
#!/bin/bashmyUrl="gggggggg"# 只读变量设置# readonly myUrlecho "myUrl =" ${myUrl}unset myUrlecho 'myUrl = ' ${myUrli}# string spliceyour_name='wanghuixi'str="hello I know you are \"$your_name\" ! \n"echo -e $strgreeting="hello,"$your_name"!"greeting1="hello,${your_name} !"echo $greeting $greeting1greeting2='hello, '$your_name'!'greeting3='hello, '${your_name}'ddd!'echo $greeting2 $greeting3echo ${#your_name}echo ${your_name:1:2}string="runoob is a great site"echo `expr index "$string" io`array_name=(value0 value1 value2 value3)echo $array_name[0]echo ${array_name[@]}#获取数组的长度echo ${#array_name[@]}echo ${#array_name[*]}# 取得数组单个元素的长度echo ${#array_name[2]}:<<.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 $idoneecho "-- \$@ --"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 2+2`echo "val: $val"a=10b=20val=`expr $a - $b`echo "a -b : $val" val=`expr $a + $b`echo "a + b : $val"if [ $a == $b ]then echo " a = b "fiif [ $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 lineecho -e "ok \c"echo "it is a test " > myfileecho `date`printf "%-10s %-8s %-4s\n" 姓名 性别 体重kgprintf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876for loop in 1 2 3 4 5 do echo "The value is : $loop"donefor str in "this is a string "do echo $strdoneint=1while(($int<=5))do echo $int let "int++"doneecho "##########":<<.echo -n '输入你最喜欢的网站名:'while read FILMdo echo "$FILM"done.a=0until [ ! $a -lt 10 ]do echo $a a=`expr $a + 1`done# case echo "输入 1 到 4 之间的数字"echo "你输入的数字为:"read aNumcase $aNum in 1) echo '1';; 2) echo '2';; 3) echo '3';; 4) echo '4';; *) echo '你没有输入 1 到 4 之间的数字';;esacfunWithParam(){ echo "第一个参数为 $1 !" echo "第二个参数为 $2 !" echo "第十个参数为 $10 !" echo "第十个参数为 ${10} !" echo "第十一个参数为 ${11} !" echo "参数总数有 $# 个!" echo "作为一个字符串输出所有参数 $* !"}funWithParam 1 2 3 4 5 6 7 8 9 34 73
运行结果:
myUrl = ggggggggmyUrl = hello I know you are "wanghuixi" ! hello,wanghuixi! hello,wanghuixi !hello, wanghuixi! hello, wanghuixiddd!9an4value0[0]value0 value1 value2 value3446can shu: ./text.shcan shu: can shu num : 0proess ID is : 23470can shu -- \S* ---- $@ --first :Asecond: ccc A B ccc D A B ccc D 4 4val: 2+2a -b : -10a + b : 30a != bok! \nok Sun Apr 28 05:15:39 PDT 2019姓名 性别 体重kg郭靖 男 66.12杨过 男 48.65郭芙 女 47.99The value is : 1The value is : 2The value is : 3The value is : 4The value is : 5this is a string12345##########0123456789输入 1 到 4 之间的数字你输入的数字为:22第一个参数为 1 !第二个参数为 2 !第十个参数为 10 !第十个参数为 34 !第十一个参数为 73 !参数总数有 11 个!作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !