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

以上是关于linux鍩虹11-bash缂栫▼(瀛楃涓叉祴璇?鍜?for寰幆)的主要内容,如果未能解决你的问题,请参考以下文章

浜岃繘鍒?amp;瀛楃缂栫爜_20210306

JavaWeb涓亣鍒扮殑瀛楃缂栫爜闂

瀛楃缂栫爜-ASCII锛孶nicode 鍜?UTF-8

6銆?0閬揓AVA鍩虹缂栫▼缁冧範棰樿窡绛旀

浠婃櫄8鐐癸紝鏂屽彅浜茶韩鏁欎綘缃戦〉缂栫▼鍟︼紒鍏嶈垂锛?宀佷互涓婇浂鍩虹閮藉彲浠ュ惉鎳傦紒

Java 瀛楃鐮併€佸瓧绗︽祦銆佸瓧鑺傛祦