前言
编程语言的入门都有一些相通之处,这也是为什么精通一门编程语言的人,在接触一种新语言的时候,能够迅速上手。下面讲下shell入门的几个demo,帮助你迅速上手shell。
1 变量相关
#!/bin/bash name="helloworld" name="lilming" #readonly name="read only" #unset name myname='liming' var="my name is ${myname}" echo ${var} echo ${myname} echo ${name}
2 数组的使用
#!/bin/bash name="helloworld" name="liming" : ' #length is 15 name="this is"" my name" #name="this is my name" #echo ${var} #echo ${myname} # cut strings #echo ${name:1:4} echo ${name:2:10} echo ${#name} ' array_name=(li wang xiang zhang) array_para[0]="w"; array_para[3]="s" echo ${array_name[1]} echo ${array_para[3]} # echo array all elements echo ${array_name[@]} echo ${#array_name[@]} echo ${#array_name[0]} echo ${#array_para[1]}
3 打印相关
#!/bin/bash printf "hello\n" printf " %d, %f\n" 15 15.0000
4 运算相关
#!/bin/bash a=10 b="abc" c="10" d="20" # -eq -ne -gt -lt -ge -le if [ $a != 10 ] then echo " a is not 10 " else echo " a is 10 " fi if [ $c -eq 20 -a $d -eq 20 ] then echo " a ,b all right " else echo " a ,b all wrong " fi : ' if [ $c20 ] then echo " a ,b all right " else echo " a ,b all wrong " fi '