免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
shell編程知識(shí)

shell編程
shell是一種腳本語(yǔ)言
可以使用邏輯判斷、循環(huán)等語(yǔ)法
可以自定義函數(shù)
shell是系統(tǒng)命令的集合
shell腳本可以實(shí)現(xiàn)自動(dòng)化運(yùn)維,能大大增加我們的運(yùn)維效率

開(kāi)頭需要加#!/bin/bash
以#開(kāi)頭的行作為解釋說(shuō)明
腳本的名字以.sh結(jié)尾,用于區(qū)分這是一個(gè)shell腳本
執(zhí)行方法有兩種
chmod x 1.sh; ./1.sh
bash 1.sh
查看腳本執(zhí)行過(guò)程 bash -x 1.sh
查看腳本是否語(yǔ)法錯(cuò)誤 bash -n 1.sh

date時(shí)間命令
date?? %Y-%m-%d=date %F 年月日
date %y-%m-%d 年月日
date?? %H:%M:%S = date %T 時(shí)間
date %s??時(shí)間戳
date -d @1504620492
date -d " 1day" 一天后
date -d "-1 day" 一天前
date -d "-1 month" 一月前
date -d "-1 min" 一分鐘前
date %w 周幾
date %W 今年的第幾周

shell腳本中的變量
當(dāng)腳本中使用某個(gè)字符串較頻繁并且字符串長(zhǎng)度很長(zhǎng)時(shí)就應(yīng)該使用變量代替
使用條件語(yǔ)句時(shí),常使用變量? ? if [ $a -gt 1 ]; then ... ; fi
引用某個(gè)命令的結(jié)果時(shí),用變量替代? ?n=wc -l 1.txt
寫(xiě)和用戶交互的腳本時(shí),變量也是必不可少的??read -p "Input a number: " n; echo $n? ?如果沒(méi)寫(xiě)這個(gè)n,可以直接使用$REPLY
內(nèi)置變量 $0, $1, $2…? ? $0表示腳本本身,$1 第一個(gè)參數(shù),$2 第二個(gè) ....? ?? ? $#表示參數(shù)個(gè)數(shù)
數(shù)學(xué)運(yùn)算a=1;b=2; c=$(($a $b))或者$[$a $b]

shell腳本中的邏輯判斷

格式1:if 條件 ; then 語(yǔ)句; fi a=5 if [ $a -gt 3 ] ;then echo ok ; fi
格式2:if 條件; then 語(yǔ)句; else 語(yǔ)句; fi a=5 if [ $a -gt 3 ] ; then echo ok ; else not ok ; fi
格式3:if …; then … ;elif …; then …; else …; fi a=5 if [ $a -gt 1] ; then echo ok ;elif [ $a -lt 6 ] ; else nook ; fi
邏輯判斷表達(dá)式:
if [ $a -gt $b ];
if [ $a -lt 5 ];
if [ $b -eq 10 ]等
-gt (>); 大于
-lt(<); 小于
-ge(>=); 大于等于
-le(<=); 小于等于
-eq(==); 等于
-ne(!=) 不等于
注意到處都是空格
可以使用 && || 結(jié)合多個(gè)條件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then 且
if [ $b -gt 5 ] || [ $b -lt 3 ]; then 或

文件目錄屬性判斷
[ -f file ]判斷是否是普通文件,且存在 f="/tmp/ceshi" if [ -f $f ] ; then echo $f exist ; else touch $f ; fi
[ -d file ] 判斷是否是目錄,且存在 f="/tmp/ceshi" if [ -d $f ] ; then echo $f exist ; else touch $f ; fi
[ -e file ] 判斷文件或目錄是否存在 f="/tmp/ceshi" if [ -e $f ] ; then echo $f exist ; else touch $f ; fi
[ -r file ] 判斷文件是否可讀 f=''/tmp/ceshi'' if [ -r $f ] ; then echo $f is read; fi
[ -w file ] 判斷文件是否可寫(xiě) f=''/tmp/ceshi'' if [ -w $f ] ; then echo $f is write; fi
[ -x file ] 判斷文件是否可執(zhí)行 f=''/tmp/ceshi'' if [ -x $f ] ; then echo $f is exe fi

[ -f $f ] && rm -rf $f f=''/tmp/ceshi'' if [ -f $f ] ; then rm -rf $f ; fi
[ ! -f $f ] || touch $f f=''/tmp/ceshi'' if [ ! -f $f ] ; then touch $f ;fi

if判斷的一些特殊用法
if [ -z "$a" ]?? 這個(gè)表示當(dāng)變量a的值為空時(shí)會(huì)怎么樣
if [ -n "$a" ] 表示當(dāng)變量a的值不為空
if grep -q '123' 1.txt; then?? 表示如果1.txt中含有'123'的行時(shí)會(huì)怎么樣
if [ ! -e file ]; then 表示文件不存在時(shí)會(huì)怎么樣
if (($a<1)); then … 等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=這樣的符號(hào)

shell中case判斷
格式case變量名 in
value1)
command
;;
value2)
command
;;
*)
command
;;
在case中可以在條件中使用|,表示或的意思,比如 2|3) command ;;
read -p ""

shell腳本案例
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi

n1=echo $n|sed 's/[0-9]//g' //排查非數(shù)字情況
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number range is 0-100."
;;
esac

for循環(huán)
語(yǔ)法 for 變量名 in 條件;do ………… ;done

經(jīng)典案例1
#!/bin/bash
sum=0
for i in seq 1 100
do
sum=$[$sum $i]
echo $i
done

echo $sum

文件列表循環(huán)
#!/bin/bash
cd /etc/
for a in ls /etc/
do
? ? if [ -d $a ]
? ? then
? ?? ? ls -d $a
? ? fi
done

來(lái)源:https://www.icode9.com/content-3-394001.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
shell腳本的一些注意事項(xiàng) | Sina App Engine Blog
Linux Bash編程超詳細(xì)語(yǔ)法總結(jié)
執(zhí)行Shell腳本的4種方法及區(qū)別介紹
【shell腳本分享】100個(gè)Shell腳本經(jīng)典案例解析
我的一些簡(jiǎn)單的shell腳本實(shí)例
Shell多線程操作及線程數(shù)控制實(shí)例
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服