歡迎訪問我的博客html
本地變量:手動定義的,在當前系統的某個環境下才能生效,做用範圍小shell
普通變量:bash
➜ shell name='tom' ➜ shell echo $name tom
➜ shell word="I am $name" ➜ shell echo $word I am tom
➜ shell age=33 ➜ shell echo $age 33
命令變量:變量值是命令的變量,會將命令的執行結果賦值給變量測試
➜ shell files=`ll` ➜ shell echo $files total 8 -rwxr-xr-x 1 zhouyajun staff 228B Mar 26 21:58 itcast.sh drwx-wx--x 3 zhouyajun staff 96B Oct 11 22:15 test
➜ shell woshishui=$(whoami) ➜ shell echo $woshishui zhouyajun
腳本文件ui
$0 獲取當前執行的shell腳本文件名命令行
#! /bin/bash # yy複製,p粘貼 echo "獲取當前文件名:get_name" echo "獲取當前文件名:$0" ➜ shell /bin/bash get_name.sh 獲取當前文件名:get_name 獲取當前文件名:get_name.sh
$n 獲取當前執行的shell腳本的第n個參數值,n=1..9,當n爲0的時候表示腳本的文件名,若是n大於9就要用大括號括起來${10}code
#! /bin/bash echo "獲取參數" echo "獲取命令行參數:$1" ➜ shell /bin/bash get_args.sh 1 2 3 獲取參數 獲取命令行參數:1
#! /bin/bash echo "獲取參數" echo "獲取命令行參數:$#" ➜ shell /bin/bash get_args.sh 1 2 3 d gf 獲取參數 獲取命令行參數:5
$? 獲取執行上一個指令的返回值(0爲成功,非0爲失敗)htm
➜ shell echo $? 這是上面指令執行結果 0 ➜ shell asdfas 這是一條錯誤指令 zsh: command not found: asdfas ➜ shell echo $? 127
精確獲取 ${變量名:其實位置:截取長度},get
➜ shell file=abcdefghijklmnopkrstuvwxyz ➜ shell echo $file abcdefghijklmnopkrstuvwxyz ➜ shell echo ${file:0:5} abcde ➜ shell echo ${file:5:5} fghij ➜ shell echo ${file:0-6:5} uvwxy
默認值博客
#! /bin/bash a="$1" # 獲取命令行第一個參數 echo "參數的值是: ${a:-1}" # 設置默認值1 ➜ shell /bin/bash default_value.sh 55 參數的值是: 55 ➜ shell /bin/bash default_value.sh 參數的值是: 1
歡迎你們去 個人博客 瞅瞅,裏面有更多關於測試實戰的內容哦!!