海詩(shī)網(wǎng) 知識(shí)百科 shell腳本for循環(huán)(shell腳本for循環(huán) 計(jì)算1到100的和)

shell腳本for循環(huán)(shell腳本for循環(huán) 計(jì)算1到100的和)

shell腳本for循環(huán), shell腳本中的for循環(huán)是什么?讓我們一起來(lái)看看:

使用for循環(huán)在shell腳本中編寫(xiě)腳本通常用于確定輸入的用戶名是否存在。如果不存在,則創(chuàng)建用戶并設(shè)置密碼,否則程序會(huì)繼續(xù)提示用戶,即提示重新輸入新創(chuàng)建的用戶名。

for命令中for i的各種用法介紹如下:

For the I in File 1, File 2 and File 3

for i in /boot/*

for i in /etc/*.conf

For I, the unit is $(seq -w 10)-"01-10 of equal width.

For {1…10} years of I

for i in $( ls )

for I in $( file)

“$ @”中的For i使用所有位置參數(shù),可以縮寫(xiě)為for I。

shell腳本for循環(huán) 計(jì)算1到100的和

需要注意的是,bash shell支持C類(lèi)型for循環(huán)。

示例代碼如下:

#!/bin/bash

j=$1

for ((i=1; i=j; i++))

do

touch file$i echo file $i is ok

done

$ @:所有位置變量的內(nèi)容

$ #:位置變量的數(shù)量

$0:文件名

$ *:所有位置變量的內(nèi)容。

shell腳本for循環(huán) 計(jì)算1到100的和

for循環(huán)的一般代碼格式是:

對(duì)于列表中的變量名

do

command1

command2

.

commandN

done

參考示例:

示例1

輸入代碼:

for loop in 1 2 3 4 5

do

echo The value is: $loop

done

輸出結(jié)果是:

The value is: 1The value is: 2The value is: 3The value is: 4The value is: 5

示例2

如果您編寫(xiě)一個(gè)腳本來(lái)清除所有arp緩存記錄,示例代碼如下:

#!/bin/bash

for i in $(arp | tail -n +2|tr -s |cut -d -f1)

do

arp -d $i

done

shell腳本for循環(huán),以上就是本文為您收集整理的shell腳本for循環(huán)最新內(nèi)容,希望能幫到您!更多相關(guān)內(nèi)容歡迎關(guān)注。

本文來(lái)自網(wǎng)絡(luò),不代表海詩(shī)網(wǎng)立場(chǎng),轉(zhuǎn)載請(qǐng)注明出處:http://x91880.com/n/169514.html
      

ubuntu修改root密碼(ubuntu設(shè)置root密碼)

發(fā)表回復(fù)
聯(lián)系我們
聯(lián)系我們

在線咨詢: QQ交談

郵箱: 3587015498@qq.com

工作時(shí)間:周一至周五,9:00-17:30,節(jié)假日休息

關(guān)注微信
微信掃一掃關(guān)注我們
微信掃一掃關(guān)注我們
關(guān)注微博
返回頂部