首页 > 代码库 > shell基础

shell基础

  1 第一个shell脚本  2 #!/bin/bash  3 echo "Hello World"  4   5 使用read从标准输入获取输入并赋值给变量,然后输出  6 #!/bin/bash  7   8 # Author : zjw  9 # Copyright @ www.litsoft.com 10 # Script follow here: 11  12 echo "what is your name?" 13 read PERSON 14 echo "Hello, $PERSON" 15  16 使用变量,并使用了for循环 17 #!/bin/bash 18  19 for skill in Ada Coffe Action Java 20 do 21     echo "I am good at ${skill} Script." 22 done 23  24 echo "Over" 25  26 重定义变量 27 #!/bin/bash 28  29 myUrl="Http://www.litsoft.com" 30 echo ${myUrl} 31  32 myUrl="Http://www.myblog.com" 33 echo ${myUrl} 34  35 echo "Over" 36  37  38 只读变量 39 #!/bin/bash 40  41 myUrl="Http://www.litsoft.com" 42 echo ${myUrl} 43  44 readonly myUrl 45  46 myUrl="Http://www.myblog.com" 47 echo ${myUrl} 48  49 echo "Over" 50  51  52 删除变量 53 #!/bin/bash 54  55 myUrl="Http://www.litsoft.com" 56 echo ${myUrl} 57  58 unset myUrl 59  60 #myUrl="Http://www.myblog.com" 61 echo ${myUrl} 62  63 echo "Over" 64  65  66 特殊变量,命令行参数 67 #!/bin/bash 68  69 echo "File Name : $0" 70 echo "First Parameter : $1" 71 echo "Second Parameter : $2" 72 echo "Quoted Values : $@" 73 echo "Quoted Values : $*" 74 echo "Total Number of Parameters : $#" 75  76  77 变量替换 78 #!/bin/bash 79  80 a=10 81 echo -e "Value of a is $a \n" 82 echo "Over" 83  84  85 命令替换 86 #!/bin/bash 87 DATE=date 88 echo "Date is $DATE" 89  90 USERS=who | wc -l 91 echo "Logged in user are $USERS" 92  93 UP=date; uptime 94 echo "Uptime is &UP" 95  96  97 变量替换 98 #!/bin/bash 99 100 echo ${var:-"Variable is not set"}101 echo "1 - Value of var is ${var}"102 103 echo ${var:="Variable is not set"}104 echo "2 - Value of var is ${var}"105 106 unset var107 echo ${var:+"This is default value"}108 echo "3 - Value of var is $var"109 110 var="Prefix"111 echo ${var:+"This is default value"}112 echo "4 - Value of var is $var"113 114 echo ${var:?"Print this message"}115 echo "5 - Value of var is ${var}"116 117 118 ===========================shell运算符============================================119 算术运算符120 #!/bin/bash121 122 val= expr 2 + 2123 124 echo "Total value : $val"125 126 #!/bin/bash127 128 a=10129 b=20130 val= expr $a + $b131 echo "a + b : $val"132 133 val= expr $a - $b134 echo "a - b : $val"135 136 val= expr $a \* $b137 echo "a * b : $val"138 139 val= expr $b / $a140 echo "b / a : $val"141 142 val= expr $b % $a143 echo "b % a : $val"144 145 if [ $a == $b ]146 then147     echo "a is equal to b"148 fi149 150 if [ $a != $b ]151 then152     echo "a is not equal to b"153 fi154 155 156 关系运算符157 #!/bin/sh158 159 a=10160 b=20161 if [ $a -eq $b ]162 then163     echo "$a -eq $b : a is equal to b"164 else165     echo "$a -eq $b : a is not equal to b"166 fi167 168 if [ $a -ne $b ]169 then170     echo "$a -ne $b : a is not equal to b"171 else172     echo "$a -ne $b : a is equal to b"173 fi174 175 if [ $a -gt $b ]176 then177     echo "$a -gt $b : a is greater than b"178 else179     echo "$a -gt $b : a is not greater than b"180 fi181 182 if [ $a -lt $b ]183 then184     echo "$a -lt $b : a is less than b"185 else186     echo "$a -lt $b : a is not less than b"187 fi188 189 if [ $a -ge $b ]190 then191     echo "$a -ge $b : a is greater or equal to b"192 else193     echo "$a -ge $b : a is not greater or equal to b"194 fi195 196 if [ $a -le $b ]197 then198     echo "$a -le $b : a is less or equal to b"199 else200     echo "$a -le $b : a is no less or equal to b"201 fi202 203 204 布尔运算符205 #!/bin/bash206 207 a=10208 b=20209 210 if [ $a != $b ]211 then212     echo "$a != $b : a is not equal to b"213 else214     echo "$a != $b : a is equal to b"215 fi216 217 if [ $a -lt 100 -a $b -gt 15 ]218 then219     echo "$a -lt 100 -a $b -gt 15 : return true"220 else221     echo "$a -lt 100 -a $b -gt 15 : return false"222 fi223 224 if [ $a -lt 100 -o $b -gt 100 ]225 then226     echo "$a -lt 100 -o $b -gt 100 : return true"227 else228     echo "$a -lt 100 -o $b -gt 100 : return false"229 fi230 231 if [ $a -lt 5 -o $b -gt 100 ]232 then233     echo "$a -lt 100 -o $b -gt 100 : return true"234 else235     echo "$a -lt 100 -o $b -gt 100 : return false"236 fi237 238 239 字符串运算符240 #!/bin/bash241 242 a="abc"243 b="efg"244 245 if [ $a = $b ]246 then247     echo "$a = $b : a is equal to b"248 else249     echo "$a = $b : a is not equal to b"250 fi251 252 if [ $a != $b ]253 then254     echo "$a != $b : a is not equal to b"255 else256     echo "$a != $b : a is equal to b"257 fi258 259 if [ -z $a ]260 then261     echo "-z $a : string length is zero"262 else263     echo "-z $a : string length is not zero"264 fi265 266 if [ -n $a ]267 then268     echo "-n $a : string length is not zero"269 else270     echo "-n $a : string length is zero"271 fi272 273 if [ $a ]274 then275     echo "$a : string is not empty"276 else277     echo "$a : string is empty"278 fi279 280 281 文件测试运算符282 #!/bin/bash283 284 file="/mnt/hgfs/work/mycode/shell/1.sh"285 286 if [ -r $file ]287 then288     echo "File has read access"289 else290     echo "File does not have read access"291 fi292 293 if [ -w $file ]294 then295     echo "File has write permission"296 else297     echo "File does not have write permission"298 fi299 300 if [ -x $file ]301 then302     echo "File has execute permission"303 else304     echo "File does not has execute permission"305 fi306 307 if [ -f $file ]308 then309     echo "File is an ordinary file"310 else311     echo "This is a spcial file"312 fi313 314 if [ -d $file ]315 then316     echo "File is a directory file"317 else318     echo "This is not a directory file"319 fi320 321 if [ -s $file ]322 then323     echo "File size is zero"324 else325     echo "File size is not zero"326 fi327 328 if [ -e $file ]329 then330     echo "File exists"331 else332     echo "File does not exist"333 fi

 

shell基础