婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Shell中的for循環總結

Shell中的for循環總結

熱門標簽:奧維互動地圖標注參數 寧波智能外呼系統公司 聯通電話機器人怎么接 衛星地圖標注地名 申請公司400電話要注意什么 電銷機器人 劍魚 地圖標注輻射圖案 安裝外呼系統費用 曲阜400電話辦理

關于shell中的for循環用法很多,一直想總結一下,今天網上看到上一篇關于for循環用法的總結,感覺很全面,所以就轉過來研究研究,嘿嘿...

復制代碼 代碼如下:
for((i=1;i=10;i++));do echo $(expr $i \* 4);done

在shell中常用的是 for i in $(seq 10)
復制代碼 代碼如下:

for i in `ls`
for i in ${arr[@]}
for i in $* ; do
for File in /proc/sys/net/ipv4/confaccept_redirects:'
for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo $File
done
echo "直接指定循環內容"
for i in f1 f2 f3 ;do
echo $i
done
echo
echo "C 語法for 循環:"
for (( i=0; i10; i++)); do
echo $i
done

---------------------------------------------------------------------------------------------------------
shell中for循環用法

shell語法好麻煩的,一個循環都弄了一會 ,找了幾個不同的方法來實現輸出1-100間可以被3整除的數
1.用(())

復制代碼 代碼如下:

#!/bin/bash
clear
for((i=1;i100;i++))
for
do
if((i%3==0))
then
echo $i
continue
fi
done

2.使用`seq 100`
復制代碼 代碼如下:

#!/bin/bash
clear
for i in `seq 100`
do
if((i%3==0))
then
echo $i
continue
fi
done

3.使用while
復制代碼 代碼如下:

#!/bin/bash
clear
i=1
while(($i100))
do
if(($i%3==0))
then
echo $i
fi
i=$(($i+1))
done

--------------------------------------------------------------------------------------------------------
在shell用for循環做數字遞增的時候發現問題,特列出shell下for循環的幾種方法:
1.
復制代碼 代碼如下:

for i in `seq 1 1000000`;do
echo $i
done

用seq 1 10000000做遞增,之前用這種方法的時候沒遇到問題,因為之前的i根本就沒用到百萬(1000000),因為項目需要我這個數字遠大于百萬,發現用seq 數值到 1000000時轉換為1e+06,根本無法作為數字進行其他運算,或者將$i有效、正確的取用,遂求其他方法解決,如下
2.
復制代碼 代碼如下:

for((i=1;i10000000;i++));do
echo $i
done
3.
i=1
while(($i10000000));do
echo $i
i=`expr $i + 1`
done

因為本方法調用expr故運行速度會比第1,第2種慢不少不過可稍作改進,將i=`expr $i + 1`改為i=$(($i+1))即可稍作速度的提升,不過具體得看相應shell環境是否支持
4.
復制代碼 代碼如下:

for i in {1..10000000;do
echo $i
done

其實選用哪種方法具體還是得由相應的shell環境的支持,達到預期的效果,再考慮速度方面的問題。
示例:
復制代碼 代碼如下:

# !/bin/sh
i=1
function test_while(){
i=1
while [ $i ]
do
echo $i
i=`expr $i + 1`
if [ $i -ge 10 ]; then
break
fi
done
}
function test_for(){
i=1
for ((i=1; i=100; i++)); do
echo $i
if [ $i -ge 10 ]; then
break
fi
done
}
function test_continue(){
i=1
for i in $(seq 100); do
if (( i==0 )); then
echo $i
continue
fi
done
}
echo "test_while..."
test_while
echo "test_for..."
test_for
echo "test_continue..."
test_continue

運行結果:
復制代碼 代碼如下:

test_while...
1
2
3
4
5
6
7
8
9
test_for...
1
2
3
4
5
6
7
8
9
10
test_continue...
10
20
30
40
50
60
70
80
90
100

您可能感興趣的文章:
  • shell中的for循環用法詳解
  • linux shell循環:for、while、until用法詳解
  • linux shell常用循環與判斷語句(for,while,until,if)使用方法
  • Shell腳本用for循環遍歷參數的方法技巧
  • shell for循環、循環變量值付給其他shell腳本的方法
  • 基于Shell中for循環的幾個常用寫法分享
  • Shell中的for和while循環詳細總結
  • Shell中的循環語句for、while、until實例講解
  • Shell兩種for循環對應實現方法解析

標簽:三門峽 仙桃 大慶 上饒 安康 遵義 江西 大興安嶺

巨人網絡通訊聲明:本文標題《Shell中的for循環總結》,本文關鍵詞  Shell,中的,for,循環,總結,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Shell中的for循環總結》相關的同類信息!
  • 本頁收集關于Shell中的for循環總結的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 通江县| 莫力| 泗水县| 保德县| 崇文区| 灌云县| 河北省| 洛川县| 黎平县| 泗水县| 南安市| 龙川县| 普兰店市| 崇左市| 彭水| 丹棱县| 互助| 黄平县| 安塞县| 华蓥市| 阳西县| 建瓯市| 昌宁县| 手游| 汝南县| 敖汉旗| 阳春市| 阳原县| 晴隆县| 靖江市| 原阳县| 南木林县| 惠水县| 镇远县| 天祝| 贡山| 青龙| 永州市| 荆门市| 营口市| 贡嘎县|