linux鍩虹11-bash缂栫▼(瀛楃涓叉祴璇?鍜?for寰幆)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux鍩虹11-bash缂栫▼(瀛楃涓叉祴璇?鍜?for寰幆)相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%ad%97%e7%ac%a6' title='瀛楃'>瀛楃
鍒楄〃 鐢ㄦ埛 shu dmi 鍒犻櫎鐢ㄦ埛 ++ 鐢熶骇 +=缁冧範锛?br />浼犻€掍竴涓敤鎴峰悕鍙傛暟缁欒剼鏈紝鍒ゆ柇姝ょ敤鎴风殑鐢ㄦ埛鍚嶈窡鍏跺熀鏈粍鐨勭粍鍚嶆槸鍚︿竴鑷达紝骞跺皢缁撴灉鏄剧ず鍑烘潵銆?br />(1)
瀛楃娴嬭瘯锛?br />==锛氭祴璇曟槸鍚︾浉绛夛紝鐩哥瓑涓虹湡锛屼笉绛変负鍋?br />!=: 娴嬭瘯鏄惁涓嶇瓑锛屼笉绛変负鐪燂紝绛変负鍋?br />>
<
-n string: 娴嬭瘯鎸囧畾瀛楃涓叉槸鍚︿负绌猴紝绌哄垯鐪燂紝涓嶇┖鍒欏亣
-z string: 娴嬭瘯鎸囧畾瀛楃涓叉槸鍚︿笉绌猴紝涓嶇┖涓虹湡锛岀┖鍒欎负鍋?/p>
缁冧範锛氬啓涓€涓剼鏈?br />浼犻€掍竴涓弬鏁?鍗曞瓧绗﹀氨琛?缁欒剼鏈紝濡傚弬鏁颁负q锛屽氨閫€鍑鸿剼鏈紱鍚﹀垯锛屽氨鏄剧ず鐢ㄦ埛鐨勫弬鏁帮紱
缁冧範锛氬啓涓€涓剼鏈?br />浼犻€掍竴涓弬鏁?鍗曞瓧绗﹀氨琛?缁欒剼鏈紝濡傚弬鏁颁负q銆丵銆乹uit鎴朡uit锛屽氨閫€鍑鸿剼鏈紱鍚﹀垯锛屽氨鏄剧ず鐢ㄦ埛鐨勫弬鏁帮紱
#!/bin/bash # if [ $1 = 鈥榪鈥?];then echo "Quiting..." exit 1 elif [ $1 = 鈥楺鈥?];then echo "Quiting..." exit 2 elif [ $1 = 鈥榪uit鈥?];then echo "Quiting..." exit 3 elif [ $1 = 鈥楺uit鈥?];then echo "Quiting..." exit 4 else echo $1 fi 渚嬶細 1锛?echo "scale=2;111/22;" | bc //淇濈暀2浣嶅皬鏁帮紝閫氳繃璁$畻鍣╞c璁$畻111/22鐨勫€?鏂规硶1)銆? 2, bc <<< "scale=2;111/22;" //淇濈暀2浣嶅皬鏁帮紝閫氳繃璁$畻鍣╞c璁$畻111/22鐨勫€?鏂规硶2)銆?
銆€銆€
缁冧範锛?br />浼犻€掍笁涓弬鏁扮粰鑴氭湰锛岀涓€涓负鏁存暟锛岀浜屼釜涓虹畻鏈繍绠楃锛岀涓変釜涓烘暣鏁帮紝灏嗚绠楃粨鏋滄樉绀哄嚭鏉ワ紝瑕佹眰淇濈暀涓や綅绮惧害銆傚舰濡傦細
./calc.sh 5 / 2
缁冧範锛?br />浼犻€?涓弬鏁扮粰鑴氭湰锛屽弬鏁板潎涓虹敤鎴峰悕銆傚皢姝や簺鐢ㄦ埛鐨勫笎鍙蜂俊鎭彁鍙栧嚭鏉ュ悗鏀剧疆浜?tmp/testusers.txt鏂囦欢涓紝骞惰姹傛瘡涓€琛岃棣栨湁琛屽彿銆?br />
鍐欎竴涓剼鏈細
鍒ゆ柇褰撳墠涓绘満鐨凜PU鐢熶骇鍟嗭紝鍏朵俊鎭湪/proc/cpuinfo鏂囦欢涓璿endor id涓€琛屼腑銆?br />濡傛灉鍏剁敓浜у晢涓篈uthenticAMD锛屽氨鏄剧ず鍏朵负AMD鍏徃锛?br />濡傛灉鍏剁敓浜у晢涓篏enuineIntel锛屽氨鏄剧ず鍏朵负Intel鍏徃锛?br />鍚﹀垯锛屽氨璇村叾涓洪潪涓绘祦鍏徃锛?/p>
(2)
寰幆锛氳繘鍏ユ潯浠讹紝閫€鍑烘潯浠?br />for
while
until
(3)
for 鍙橀噺 in 鍒楄〃; do
寰幆浣?br />done
渚嬶細
for I in 1 2 3 4 5 6 7 8 9 10; do
鍔犳硶杩愮畻
done
閬嶅巻瀹屾垚涔嬪悗锛岄€€鍑猴紱
(4) 濡備綍鐢熸垚鍒楄〃锛?br />{1..100} //鐢熸垚鏁存暟鍒楄〃鐨勬柟娉?
`seq [璧峰鏁?[姝ヨ繘闀垮害]] 缁撴潫鏁癭 //鐢熸垚鏁存暟鍒楄〃鐨勬柟娉?
(5)
bashshell涓嶆敮鎸佹诞鐐规暟杩愮畻锛屽悓鏃禸ash shell榛樿鐨勬暟鎹被鍨嬩负瀛楃鍨嬨€?/p>
(6)
bash shell澹版槑鏁村舰鐨勬柟娉曪細
declare -i SUM=0 //姝ゅ鐨?i涓?integer
鎴栵細
let sum=0
鍐欎竴涓剼鏈細
1銆佽瀹氬彉閲廎ILE鐨勫€间负/etc/passwd
2銆佷緷娆″悜/etc/passwd涓殑姣忎釜鐢ㄦ埛闂ソ锛屽苟鏄剧ず瀵规柟鐨剆hell锛屽舰濡傦細
Hello, root, your shell: /bin/bash
3銆佺粺璁′竴鍏辨湁澶氬皯涓敤鎴?/p>
for I in `seq 1 $LINES`; do echo "Hello, `head -n $I /etc/passwd | tail -1 | cut -d: -f1`"; done
鍙悜榛樿shell涓篵ash鐨勭敤鎴烽棶澹板ソ
鍐欎竴涓剼鏈細
1銆佹坊鍔?0涓敤鎴穟ser1鍒皍ser10锛屽瘑鐮佸悓鐢ㄦ埛鍚嶏紱浣嗚姹傚彧鏈夌敤鎴蜂笉瀛樺湪鐨勬儏鍐典笅鎵嶈兘娣诲姞锛?/p>
鎵╁睍锛?br />鎺ュ彈涓€涓弬鏁帮細
add: 娣诲姞鐢ㄦ埛user1..user10
del: 鍒犻櫎鐢ㄦ埛user1..user10
鍏跺畠锛氶€€鍑?br />adminusers user1,user2,user3,hello,hi
鍐欎竴涓剼鏈細
璁$畻100浠ュ唴鎵€鏈夎兘琚?鏁撮櫎鐨勬鏁存暟鐨勫拰锛?br />鍙栨ā锛屽彇浣?%
3%2=1
100%55=45
鍐欎竴涓剼鏈細
璁$畻100浠ュ唴鎵€鏈夊鏁扮殑鍜屼互鍙婃墍鏈夊伓鏁扮殑鍜岋紱鍒嗗埆鏄剧ず涔嬶紱
let I=$[$I+1] SUM=$[$SUM+$I] let SUM+=$I let I+=1 鐩稿綋浜?let I++ -= let I-=1 鐩稿綋浜?let I-- ++I, --I *= /= %=
銆€銆€
鍐欎竴涓剼鏈紝鍒嗗埆鏄剧ず褰撳墠绯荤粺涓婃墍鏈夐粯璁hell涓篵ash鐨勭敤鎴峰拰榛樿shell涓?sbin/nologin鐨勭敤鎴凤紝骞剁粺璁″悇绫籹hell涓嬬殑鐢ㄦ埛鎬绘暟銆傛樉绀虹粨鏋滃舰濡傦細
BASH锛?users锛宼hey are:
root,redhat,gentoo
NOLOGIN, 2users, they are:
bin,ftp
#!/bin/bash # NUMBASH=`grep "bash$" /etc/passwd | wc -l` BASHUSERS=`grep "bash$" /etc/passwd | cut -d: -f1` BASHUSERS=`echo $BASHUSERS | sed 鈥?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83f0c3">[email protected][[:space:]]@,@g鈥榒 echo "BASH, $NUMBASH users, they are:" echo "$BASHUSERS