linux for退出循环命令,Linux命令For循环

本文通过示例展示了Bash脚本中的for循环不同用法,包括无限循环、从文件读取、参数传递以及数组操作。还提到了Bash版本对循环语法的影响,并探讨了如何跳出for循环。此外,文章还提供了Bash版本与循环语法兼容性的信息。
摘要由CSDN通过智能技术生成

[root@jfht ~]# ls

>ls.txt

[root@jfht ~]# for s in $(cat

ls.txt)

>

> do

>

> echo $s

>

> done

anaconda-ks.cfg

bak181

hlx

install.log

install.log.syslog

job.sh

job.txt

ls.txt

mbox

mini

setup

temp

vsftpd-2.0.5-16.el5.i386.rpm

vsftpd.conf

work191

[root@jfht ~]#

示例四

Bash代码  a4c26d1e5885305701be709a3d33442f.png

print_args()

{

for arg in "$@"

do

echo $arg

done

}

print_args 1234

print_args "this is a test"

"color: #000000;">print_args this is a test

[root@smsgw root]# print_args()

> {

> for arg in "$@"

> do

> echo $arg

> done

> }

[root@smsgw root]# print_args 1 2 3

4

1

2

3

4

[root@smsgw root]# print_args "this

is a test"

this is a test

[root@smsgw root]# print_args this is

a test

this

is

a

test

示例五

Bash代码  a4c26d1e5885305701be709a3d33442f.png

for ((i=0; i<10; ++i))

do

echo $i

done

[root@smsgw root]# for ((i=0;

i<10; ++i))

> do

> echo $i

> done

0

1

2

3

4

5

6

7

8

9

示例六 列表为数组

Bash代码  a4c26d1e5885305701be709a3d33442f.png

AREAS=(1901190219031904190519061907190819091910191119121913)

NAMES=(南京 无锡 徐州 常州 苏州 南通 连云港 淮安 盐城 扬州 镇江 泰州 宿迁)

NUM_OF_AREAS=13

area_name_of()

{

for ((I=0; I

do

if [ "$1"=="${AREAS[I]}"]; then

echo "${NAMES[I]}"

fi

done

}

echo $(area_name_of 1903)

for AREA in ${AREAS[*]};

do

echo $AREA $(area_name_of $AREA)

done

[root@smsgw root]# AREAS=(1901

1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913)

[root@smsgw root]# NAMES=(南京 无锡 徐州 常州

苏州 南通 连云港 淮安 盐城 扬州 镇江 泰州 宿迁)

[root@smsgw root]# NUM_OF_AREAS=13

[root@smsgw root]# area_name_of()

> {

> for ((I=0; I

++I))

> do

> if [ "$1" == "${AREAS[I]}" ];

then

> echo "${NAMES[I]}"

> fi

> done

> }

[root@smsgw root]# echo

$(area_name_of 1903)

徐州

[root@smsgw root]# for AREA in

${AREAS[*]};

> do

> echo $AREA $(area_name_of

$AREA)

> done

1901 南京

1902 无锡

1903 徐州

1904 常州

1905 苏州

1906 南通

1907 连云港

1908 淮安

1909 盐城

1910 扬州

1911 镇江

1912 泰州

1913 宿迁

[root@smsgw root]#

示例七 bash version 3.0+

Java代码  a4c26d1e5885305701be709a3d33442f.png

bash --version

fori in {1..5}

do

echo "Welcome $i times"

done

[root@smsgw root]# bash

--version

GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)

Copyright (C) 2002 Free Software Foundation, Inc.

[root@smsgw root]# for i in

{1..5}

> do

> echo "Welcome $i times"

> done

Welcome {1..5} times

[root@smsgw root]#

换个较高版本的Linux。

[root@jfht ~]# bash

--version

GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)

Copyright (C) 2005 Free Software Foundation, Inc.

[root@jfht ~]# for i in

{1..5}

> do

> echo "Welcome $i times"

> done

Welcome 1 times

Welcome 2 times

Welcome 3 times

Welcome 4 times

Welcome 5 times

[root@jfht ~]#

示例八 Bash v4.0+

Bash代码  a4c26d1e5885305701be709a3d33442f.png

echo"Bash version ${BASH_VERSION}..."

for i in {0..10..2}

do

echo "Welcome $i times"

done

[root@smsgw root]# echo "Bash version ${BASH_VERSION}..."

Bash version 2.05b.0(1)-release...

[root@smsgw root]# for i in {0..10..2}

> do

> echo "Welcome $i times"

> done

Welcome {0..10..2} times

[root@smsgw root]#

换个较高版本的Linux。

[root@jfht ~]# echo "Bash version ${BASH_VERSION}..."

Bash version 3.2.25(1)-release...

[root@jfht ~]# for i in {0..10..2}

> do

> echo "Welcome $i times"

> done

Welcome {0..10..2} times

[root@jfht ~]#

传说Bash4.0可以支持这种语法。

Bash version 4.0.33(0)-release...

Welcome 0 times

Welcome 2 times

Welcome 4 times

Welcome 6 times

Welcome 8 times

Welcome 10 times

问题思考

1. 怎么用for实现死循环(无限循环)?

2. 比较几种写法的不同:(1) for arg in $* (2) for arg in $@ (3) for arg in

"$*" (4) for arg in "$@"

3. 怎么跳出for循环?

4. for s; do echo $s; done 这个写法的执行结果是什么?

相关资料

【3】Bash新手指南 第9章 重复性任务

【4】Keep IT Simple and Stupid 在Bash的命令行使用For循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值