首页 > 代码库 > linux bash 入门

linux bash 入门

  1 #!/bin/bash
  2 
  3 #shell使用的熟练成都反映用户对Unix/Linux使用的熟练程度
  4 #shell 有两种执行命令的方式:交互式和批处理
  5 #常见的shell脚本解释器有bash,sh,csh等等
  6 #chmod +x ./hello.sh  #使脚本具有执行权限
  7 #./hello.sh  #执行脚本
  8 
  9 #交互输入
 10 echo "whats your name?"
 11 read person
 12 echo "hello,$person"
 13 
 14 #变量 定义不加$,引用要加$
 15 myname="shiqing"
 16 echo ${myname}
 17 
 18 #特殊变量
 19 echo "filename:$0"
 20 echo "first parameter:$1"
 21 echo "second parameter:$2"
 22 echo "all parameter:$@"
 23 echo "all parameter:$*"
 24 echo "total number of parameters:$#"
 25 
 26 #命令替换 ~下 `command`
 27 USERS=`who | wc -l`
 28 echo "Logged in user are $USERS"
 29 
 30 #表达式,原生bash不支持简单的数学运算,expr 是一种表达式计算工具
 31 val=`expr 2 + 9`
 32 echo "value is $val"
 33 a=10
 34 b=20
 35 val=`expr $a \* $b`
 36 echo "a * b: $val"
 37 
 38 #字符串
 39 str="abcd"
 40 echo ${#str}
 41 echo ${str:1:4}
 42 
 43 #数组
 44 arr=(a,b,c,d)
 45 echo "first :${arr[0]}"
 46 echo "all:${arr[*]}" #获取所有元素
 47 echo "length:${#arr[*]}" #获取数组长度
 48 
 49 #if...elif...else...,条件表达式要放在方括号之间,并且要有空格
 50 a=10
 51 b=20
 52 
 53 if [ $a == $b ]
 54 then 
 55   echo "a is equal to b"
 56 elif [ $a -gt $b ]
 57 then 
 58   echo "a is g reater than b"
 59 else
 60   echo "a is less than b"
 61 fi
 62 
 63 #case ...easc...
 64 echo Input a number between 1 to 4
 65 echo Your number is:\c
 66 read aNum
 67 case $aNum in
 68     1)  echo You select 1
 69     ;;
 70     2)  echo You select 2
 71     ;;
 72     3)  echo You select 3
 73     ;;
 74     4)  echo You select 4
 75     ;;
 76     *)  echo You do not select a number between 1 to 4
 77     ;;
 78 esac
 79 
 80 #for...in...
 81 for loop in 1 2 3 4 5
 82 do
 83     echo "The value is: $loop"
 84 done
 85 
 86 #while...do...done...
 87 echo type <CTRL-D> to terminate
 88 i=0
 89 while read FILM
 90 do
 91     echo "Yeah! great film the $FILM"
 92     i=`expr $i + 1`
 93     b=3
 94     if [ $i-gt$b ] #expression 和方括号([ ])之间必须有空格,否则会有语法错误。
 95       then 
 96         break
 97     fi
 98 done
 99 
100 #until...do...done
101 a=0
102 until [ ! $a -lt 10 ]
103 do
104    echo $a
105    a=`expr $a + 1`
106    val=`expr $a + $b`
107 done
108 
109 
110 #函数
111 funWithParam(){
112     echo "The value of the first parameter is $1 !"
113     echo "The value of the second parameter is $2 !"
114     echo "The value of the tenth parameter is $10 !"
115     echo "The value of the tenth parameter is ${10} !"
116     echo "The value of the eleventh parameter is ${11} !"
117     echo "The amount of the parameters is $# !"  # 参数个数
118     echo "The string of the parameters is $* !"  # 传递给函数的所有参数
119 }
120 funWithParam 1 2 3 4 5 6 7 8 9 34 73
121 
122 funWithReturn(){
123     echo "The function is to get the sum of two numbers..."
124     echo -n "Input first number: "
125     read aNum
126     echo -n "Input another number: "
127     read anotherNum
128     echo "The two numbers are $aNum and $anotherNum !"
129     return $(($aNum+$anotherNum))
130 }
131 funWithReturn
132 ret=$? #函数返回值在执行函数后通过$?获得
133 echo "The sum of two numbers is $ret !"
134 
135 #输入输出重定向 >,>>(追加), <
136 who>users
137 wc -l<users

 

linux bash 入门