ubuntu的sh文件编程(三)

前端之家收集整理的这篇文章主要介绍了ubuntu的sh文件编程(三)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

中间运行有些需要加参数或者空格还有引号之类的,可能不能运行,下面是所有的测试代码

  1. #!/bin/bash
  2. #coding=utf-8
  3. echo "What is your name?"
  4. #read PERSON
  5. #echo "Hello,$PERSON"
  6.  
  7. your_name="mozhiyan"
  8. echo $your_name
  9. echo ${your_name}
  10.  
  11. for skill in ada caffe action java
  12. do
  13. echo "I am good at ${skill}script"
  14. done
  15.  
  16. myUrl="http://see.xidian.edu.cn/cpp/linux/"
  17. echo ${myUrl}
  18. #readonly myUrl
  19. myUrl="http://see.xidian.edu.cn/cpp/shell/"
  20. echo ${myUrl}
  21.  
  22. unset myUrl
  23. echo ${myUrl}
  24.  
  25. echo $$
  26. echo ${$}
  27.  
  28.  
  29. echo "File Name: $0"
  30. echo "First Parameter : $1"
  31. echo "Second Parameter : $2"
  32. echo "Quoted Values: $@"
  33. echo "Quoted Values: $*"
  34. echo "Total Number of Parameters : $#"
  35.  
  36. #"$* differ with $@ "
  37. echo "\$*=" $*
  38. echo "\"\$*\"=" "$*"
  39.  
  40. echo "\$@=" $@
  41. echo "\"\$@\"=" "$@"
  42.  
  43. echo "print each param from \$*"
  44. for var in $*
  45. do
  46. echo "$var"
  47. done
  48.  
  49. echo "print each param from \$@"
  50. for var in $@
  51. do
  52. echo "$var"
  53. done
  54.  
  55. echo "print each param from \"\$*\""
  56. for var in "$*"
  57. do
  58. echo "$var"
  59. done
  60.  
  61. echo "print each param from \"\$@\""
  62. for var in "$@"
  63. do
  64. echo "$var"
  65. done
  66.  
  67. echo $?
  68. a=10
  69. echo -e "Value of a is $a \n"
  70.  
  71. DATE=`date`
  72. echo "Date is $DATE"
  73.  
  74. USERS=`who | wc -l`
  75. echo "Logged in user are $USERS"
  76.  
  77. UP=`date ; uptime`
  78. echo "Uptime is $UP"
  79.  
  80. echo "+++++++++++++这段代码需要单独建立文件运行+++++++++++++++++++++"
  81. echo ${var:-"Variable is not set"}
  82. echo "1 - Value of var is ${var}"
  83.  
  84. echo ${var:="Variable is not set"}
  85. echo "2 - Value of var is ${var}"
  86.  
  87. unset var
  88. echo ${var:+"This is default value"}
  89. echo "3 - Value of var is $var"
  90.  
  91. var="Prefix"
  92. echo ${var:+"This is default value"}
  93. echo "4 - Value of var is $var"
  94.  
  95. echo ${var:?"Print this message"}
  96. echo "5 - Value of var is ${var}"
  97. echo "+++++++++++++以上这段代码需要单独建立文件运行+++++++++++++++++++++"
  98.  
  99. val1=`expr 2 + 2`
  100. echo "Total value : $val1"
  101.  
  102. a=10
  103. b=20
  104. val=`expr $a + $b`
  105. echo "a + b : $val"
  106.  
  107. val=`expr $a - $b`
  108. echo "a - b : $val"
  109.  
  110. val=`expr $a \* $b`
  111. echo "a * b : $val"
  112.  
  113. val=`expr $b / $a`
  114. echo "b / a : $val"
  115.  
  116. val=`expr $b % $a`
  117. echo "b % a : $val"
  118.  
  119. if [ $a == $b ]
  120. then
  121. echo "a is equal to b"
  122. fi
  123.  
  124. if [ $a != $b ]
  125. then
  126. echo "a is not equal to b"
  127. fi
  128.  
  129. a=10
  130. b=20
  131. if [ $a -eq $b ]
  132. then
  133. echo "$a -eq $b : a is equal to b"
  134. else
  135. echo "$a -eq $b: a is not equal to b"
  136. fi
  137.  
  138. if [ $a -ne $b ]
  139. then
  140. echo "$a -ne $b: a is not equal to b"
  141. else
  142. echo "$a -ne $b : a is equal to b"
  143. fi
  144.  
  145. if [ $a -gt $b ]
  146. then
  147. echo "$a -gt $b: a is greater than b"
  148. else
  149. echo "$a -gt $b: a is not greater than b"
  150. fi
  151.  
  152. if [ $a -lt $b ]
  153. then
  154. echo "$a -lt $b: a is less than b"
  155. else
  156. echo "$a -lt $b: a is not less than b"
  157. fi
  158.  
  159. if [ $a -ge $b ]
  160. then
  161. echo "$a -ge $b: a is greater or equal to b"
  162. else
  163. echo "$a -ge $b: a is not greater or equal to b"
  164. fi
  165.  
  166. if [ $a -le $b ]
  167. then
  168. echo "$a -le $b: a is less or equal to b"
  169. else
  170. echo "$a -le $b: a is not less or equal to b"
  171. fi
  172.  
  173. a=10
  174. b=20
  175.  
  176. if [ $a != $b ]
  177. then
  178. echo "$a != $b : a is not equal to b"
  179. else
  180. echo "$a != $b: a is equal to b"
  181. fi
  182.  
  183. if [ $a -lt 100 -a $b -gt 15 ]
  184. then
  185. echo "$a -lt 100 -a $b -gt 15 : returns true"
  186. else
  187. echo "$a -lt 100 -a $b -gt 15 : returns false"
  188. fi
  189.  
  190. if [ $a -lt 100 -o $b -gt 100 ]
  191. then
  192. echo "$a -lt 100 -o $b -gt 100 : returns true"
  193. else
  194. echo "$a -lt 100 -o $b -gt 100 : returns false"
  195. fi
  196.  
  197. if [ $a -lt 5 -o $b -gt 100 ]
  198. then
  199. echo "$a -lt 100 -o $b -gt 100 : returns true"
  200. else
  201. echo "$a -lt 100 -o $b -gt 100 : returns false"
  202. fi
  203.  
  204. a="abc"
  205. b="efg"
  206.  
  207. if [ $a = $b ]
  208. then
  209. echo "$a = $b : a is equal to b"
  210. else
  211. echo "$a = $b: a is not equal to b"
  212. fi
  213.  
  214. if [ $a != $b ]
  215. then
  216. echo "$a != $b : a is not equal to b"
  217. else
  218. echo "$a != $b: a is equal to b"
  219. fi
  220.  
  221. if [ -z $a ]
  222. then
  223. echo "-z $a : string length is zero"
  224. else
  225. echo "-z $a : string length is not zero"
  226. fi
  227.  
  228. if [ -n $a ]
  229. then
  230. echo "-n $a : string length is not zero"
  231. else
  232. echo "-n $a : string length is zero"
  233. fi
  234.  
  235. if [ $a ]
  236. then
  237. echo "$a : string is not empty"
  238. else
  239. echo "$a : string is empty"
  240. fi
  241.  
  242.  
  243. file="/home/x/git/sh_project/test.sh"
  244.  
  245. if [ -r $file ]
  246. then
  247. echo "File has read access"
  248. else
  249. echo "File does not have read access"
  250. fi
  251.  
  252. if [ -w $file ]
  253. then
  254. echo "File has write permission"
  255. else
  256. echo "File does not have write permission"
  257. fi
  258.  
  259. if [ -x $file ]
  260. then
  261. echo "File has execute permission"
  262. else
  263. echo "File does not have execute permission"
  264. fi
  265.  
  266. if [ -f $file ]
  267. then
  268. echo "File is an ordinary file"
  269. else
  270. echo "This is sepcial file"
  271. fi
  272.  
  273. if [ -d $file ]
  274. then
  275. echo "File is a directory"
  276. else
  277. echo "This is not a directory"
  278. fi
  279.  
  280. if [ -s $file ]
  281. then
  282. echo "File size is zero"
  283. else
  284. echo "File size is not zero"
  285. fi
  286.  
  287. if [ -e $file ]
  288. then
  289. echo "File exists"
  290. else
  291. echo "File does not exist"
  292. fi
  293.  
  294.  
  295.  
  296. your_name='qinjx'
  297. str="Hello,I know your are \"$your_name\"! \n"
  298.  
  299. echo -e $str
  300.  
  301.  
  302. NAME[0]="Zara"
  303. NAME[1]="Qadir"
  304. NAME[2]="Mahnaz"
  305. NAME[3]="Ayan"
  306. NAME[4]="Daisy"
  307. echo "First Index: ${NAME[0]}"
  308. echo "Second Index: ${NAME[1]}"
  309.  
  310. NAME[0]="Zara"
  311. NAME[1]="Qadir"
  312. NAME[2]="Mahnaz"
  313. NAME[3]="Ayan"
  314. NAME[4]="Daisy"
  315. echo "First Method: ${NAME[*]}"
  316. echo "Second Method: ${NAME[@]}"
  317.  
  318. length=${#NAME[*]}
  319. echo "$length"
  320.  
  321. echo "\"It is a test\""
  322.  
  323. mouth=8
  324. echo "${mouth}-1-2009"
  325.  
  326. echo -e "OK!\n"
  327. echo "It is a test"
  328.  
  329. echo -e "OK!\c"
  330. echo "It is a test"
  331. echo '$name\"'
  332. echo `date`
  333.  
  334. printf "%d %s\n" 1 "abc"
  335. printf '%d %s\n' 1 "abc"
  336. printf "%s\n" abc def
  337. printf "%s %s %s\n" a b c d e f g h i j
  338. printf "%s and %d \n"
  339. printf "The first program always prints'%s,%d\n'" Hello Shell
  340.  
  341. num1=$[2*3]
  342. num2=$[1+5]
  343. if test $[num1] -eq $[num2]
  344. then
  345. echo 'The two numbers are equal!'
  346. else
  347. echo 'The two numbers are not equal!'
  348. fi
  349.  
  350. echo 'Input a number between 1 to 4'
  351. echo -e 'Your number is:\c'
  352. read aNum
  353. case $aNum in
  354. 1) echo 'You select 1'
  355. ;;
  356. 2) echo 'You select 2'
  357. ;;
  358. 3) echo 'You select 3'
  359. ;;
  360. 4) echo 'You select 4'
  361. ;;
  362. *) echo 'You do not select a number between 1 to 4'
  363. ;;
  364. esac
  365.  
  366. option="${1}"
  367. case ${option} in
  368. -f) FILE="${2}"
  369. echo "File name is $FILE"
  370. ;;
  371. -d) DIR="${2}"
  372. echo "Dir name is $DIR"
  373. ;;
  374. *)
  375. echo "`basename ${0}`:usage: [-f file] | [-d directory]"
  376. exit 1 # Command to come out of the program with status 1
  377. ;;
  378. esac
  379.  
  380.  
  381. for loop in 1 2 3 4 5
  382. do
  383. echo "The value is: $loop"
  384. done
  385.  
  386.  
  387.  
  388. for str in 'This is a string'
  389. do
  390. echo $str
  391. done
  392.  
  393.  
  394. for FILE in $HOME/.bash*
  395. do
  396. echo $FILE
  397. done
  398.  
  399. COUNTER=0
  400. while [ $COUNTER -lt 5 ]
  401. do
  402. COUNTER=`expr $COUNTER + 1`
  403. echo $COUNTER
  404. done
  405.  
  406. #echo 'type <CTRL-D> to terminate'
  407. #echo -n 'enter your most liked film: '
  408. #while read FILM
  409. #do
  410. # echo "Yeah! great film the $FILM"
  411. #done
  412.  
  413. while :
  414. do
  415. echo -n "Input a number between 1 to 5: "
  416. read aNum
  417. case $aNum in
  418. 1|2|3|4|5) echo "Your number is $aNum!"
  419. ;;
  420. *) echo "You do not select a number between 1 to 5,game is over!"
  421. break
  422. ;;
  423. esac
  424. done
  425.  
  426.  
  427.  
  428. for var1 in 1 2 3
  429. do
  430. for var2 in 0 5
  431. do
  432. if [ $var1 -eq 2 -a $var2 -eq 0 ]
  433. then
  434. echo "find the variables:$var1 $var2"
  435. break 2
  436. else
  437. echo "$var1 $var2"
  438. fi
  439. done
  440. done
  441.  
  442. NUMS="1 2 3 4 5 6 7"
  443.  
  444. for NUM in $NUMS
  445. do
  446. Q=`expr $NUM % 2`
  447. if [ $Q -eq 0 ]
  448. then
  449. echo "Number is an even number!!"
  450. continue
  451. fi
  452. echo "Found odd number"
  453. done
  454.  
  455.  
  456.  
  457. Hello () {
  458. echo "Url is http://see.xidian.edu.cn/cpp/shell/"
  459. }
  460. Hello
  461.  
  462.  
  463.  
  464. funWithReturn(){
  465. echo "The function is to get the sum of two numbers..."
  466. echo -n "Input first number: "
  467. read aNum
  468. echo -n "Input another number: "
  469. read anotherNum
  470. echo "The two numbers are $aNum and $anotherNum !"
  471. return $(($aNum+$anotherNum))
  472. }
  473. funWithReturn
  474. # Capture value returnd by last command
  475. ret=$?
  476. echo "The sum of two numbers is $ret !"
  477.  
  478.  
  479. number_one () {
  480. echo "Url_1 is http://see.xidian.edu.cn/cpp/shell/"
  481. number_two
  482. }
  483.  
  484. number_two () {
  485. echo "Url_2 is http://see.xidian.edu.cn/cpp/u/xitong/"
  486. }
  487. number_one
  488.  
  489.  
  490. funWithParam(){
  491. echo "The value of the first parameter is $1 !"
  492. echo "The value of the second parameter is $2 !"
  493. echo "The value of the tenth parameter is $10 !"
  494. echo "The value of the tenth parameter is ${10} !"
  495. echo "The value of the eleventh parameter is ${11} !"
  496. echo "The amount of the parameters is $# !" # 参数个数
  497. echo "The string of the parameters is $* !" # 传递给函数的所有参数
  498. }
  499. funWithParam 1 2 3 4 5 6 7 8 9 34 73
有问题请留言。

猜你在找的Ubuntu相关文章