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: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 $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 2+2`
echo "val: $val"


a=10
b=20

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 1 2 3 4 5 
do
    echo "The value is : $loop"
done


for str in "this is a string "
do 
    echo $str
done


int=1
while(($int<=5))
do
    echo $int
    let "int++"
done
echo "##########"
:<<.
echo -n '输入你最喜欢的网站名:'
while read FILM
do
    echo "$FILM"
done
.
a=0
until [ ! $a -lt 10 ]
do
    echo $a
    a=`expr $a + 1`
done


# case 
echo "输入 1 到 4 之间的数字"
echo "你输入的数字为:"
read aNum
case $aNum in 
    1) echo '1';;
    2) echo '2';;
    3) echo '3';;
    4) echo '4';;
    *) echo '你没有输入 1 到 4 之间的数字';;
esac


funWithParam(){
    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 = gggggggg
myUrl = 
hello I know you are "wanghuixi" ! 

hello,wanghuixi! hello,wanghuixi !
hello, wanghuixi! hello, wanghuixiddd!
9
an
4
value0[0]
value0 value1 value2 value3
4
4
6
can shu: ./text.sh
can shu: 
can shu num : 0
proess ID is : 23470
can shu   
-- \S* --

-- $@ --
first :A
second: ccc
 A B ccc D
 A B ccc D
 4
 4
val: 2+2
a -b : -10
a + b : 30
a != b
ok! \n
ok Sun Apr 28 05:15:39 PDT 2019
姓名     性别   体重kg
郭靖     男      66.12
杨过     男      48.65
郭芙     女      47.99
The value is : 1
The value is : 2
The value is : 3
The value is : 4
The value is : 5
this is a string
1
2
3
4
5
##########
0
1
2
3
4
5
6
7
8
9
输入 14 之间的数字
你输入的数字为:
2
2
第一个参数为 1 !
第二个参数为 2 !
第十个参数为 10 !
第十个参数为 34 !
第十一个参数为 73 !
参数总数有 11 个!
作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !