【linux】学习6
鳥哥13章的東西
shell script速度較慢,適合用于系統管理,但不適合處理大量數值運算
?
var=$((運算內容)) 可以用來做變量的加減乘除求余運算
total=$(($firstnum*$secnu))
declare -i total="$firstnum*$secnu"
上面兩句功能一樣,建議用第一種
echo -e "\nYour full name is: $firstname $lastname"
-e表示后面 \表示轉義,例子表示了在echo中顯示兩個變量的方法
file1=${filename}${date1}
一個變量是另兩個變量的連接的寫法
?
執行script
sh scriptname 在子進程中執行,變量不會出現在父進程
source scriptname 在父進程中執行,變量會出現在父進程
?
test :測試
test -e /test && echo "exist" || echo "Not exist" ?測試文件名/test是否存在 目錄文件都可以
test -f sh03.sh && echo "exist" || echo "Not exist" 測試文件是否存在 必須是文件
test -d abc && echo "exist" || echo "Not exist" 測試目錄是否存在 必須是目錄
下面代碼測試文件是否存在 以及文件的執行權限
read -p "Please input a filename: " filename test -z $filename && echo "You MUST input a filename." && exit 0 test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0 test -f $filename && filetype="regulare file" test -d $filename && filetype="directory" test -r $filename && perm="readable" test -w $filename && perm="$perm writable" test -x $filename && perm="$perm executable" echo "The filename: $filename is a $filetype" echo "And the permissions are: $perm"[]: 表判斷,但是挨著括號的兩端必須都是空格
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, CONTINUE" && exit 0 判斷yn等于Y或y,任意一個都返回true
?
腳本后面帶參數:
$0 代碼文件名 $1代碼后面第一個參數 ?$2代碼后面第二個參數....
$@ 代表除文件名之外的所有參數 ?$# 表后面接的參數個數
下面例子輸入少于兩個參數會退出 會顯示所有參數和第一第二個參數
echo "The script name is --> $0" echo "Total parameter number is --> $#" [ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." && exit 0 echo "Your whole parameter is --> '$@'" echo "The 1st parameter --> $1" echo "The 2nd parameter --> $2"?
shift num: 移除后面num個變量
?
條件判斷式
if [] ; then
elif []; then
else
fi
if [ "$1" == "hello" ]; thenecho "Hello, how are you?" elif [ "$1" == "" ]; thenecho "You Must input parameters, ex> {$0 someword}" elseecho "The only parameter is 'hello', ex> {$0 hello}" fi?
netatat -tuln 獲得目前主機啟動的服務
80:www
22:ssh
21:ftp
25:mail
檢測常見端口
echo "Now, I will detect your linux server's services!" echo -e "The www, ftp, ssh and mail will be detect!\n"testing=$(netstat -tuln | grep ":80 ") if [ "$testing" != "" ]; thenecho "WWW is running in your system." fitesting=$(netstat -tuln | grep ":22 ") if [ "$testing" != "" ]; thenecho "SSH is running in your system." fitesting=$(netstat -tuln | grep ":21 ") if [ "$testing" != "" ]; thenecho "FTP is running in your system." fitesting=$(netstat -tuln | grep ":25 ") if [ "$testing" != "" ]; thenecho "MAIL is running in your system." fi?
case 條件判斷
case $變量名稱 in"第一個變量內容“)程序段 ;;"第二個變量內容“)程序段 ;; *)其他變量內容的程序段exit 1;; esac?
函數:
function fname(){程序段 } ? ?后面接內置參數和shell的內置參數方法一樣 也是$1 $2 ...
?
循環:
買足條件就循環
while [ condition ] do程序段 done滿足條件就結束循環
until [ condition ] do程序段 done?
for循環
for var in con1 con2 con3 ... do程序段 done for ((初始值;限制值;執行步長)) do程序段 done?
?
調試script
sh [-nvx] scriptname.sh
-n 不執行,僅檢查語法
-v 執行前把script內容輸出到屏幕
-x 將使用到的script內容輸出到屏幕 ? debug很有用
總結
以上是生活随笔為你收集整理的【linux】学习6的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 去掉ExpandableListView
- 下一篇: 网络安全技术之端口隔离