前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux for循环

linux for循环

作者头像
葫芦
发布2019-04-17 11:05:44
2.3K0
发布2019-04-17 11:05:44
举报
文章被收录于专栏:葫芦葫芦
代码语言:javascript
复制
for var in list
do
commands
done

#!/bin/bash
for test in alabama alaska arizona arkansas california colorado
do
echo the next state is $test
done
>
the next state is alabama
the next state is alaska
the next state is arizona
the next state is arkansas
the next state is california
the next state is colorado

#!/bin/bash
for test in alabama alaska arizona arkansas california colorado
do
echo "the next state is $test"
done
echo "The las state we visited was $test"
test=connecticut
echo "wait, now we're visiting $test"
>
the next state is alabama
the next state is alaska
the next state is arizona
the next state is arkansas
the next state is california
the next state is colorado
The las state we visited was colorado
wait, now we're visiting connecticut

#!/bin/bash
for test in I don't know if this'll work
do
echo "word:$test"
done
>
word:I
word:dont know if thisll
word:work

#!/bin/bash
for test in nevada "New Hampshire" "New Mexico" "New York"
do
echo "Now going to $test"
done
>
Now going to nevada
Now going to New Hampshire
Now going to New Mexico
Now going to New York

#!/bin/bash
list="Alabama ALaska Arizona Arkansas Colorado"
list=$list" Connecticut"
for state in $list
do
echo "Have you ever visited $state?"
done
>
Have you ever visited Alabama?
Have you ever visited ALaska?
Have you ever visited Arizona?
Have you ever visited Arkansas?
Have you ever visited Colorado?
Have you ever visited Connecticut?

#!/bin/bash
file="states"
for state in `cat $file`
do
echo "Visit beautiful  $state"
done
#cat states
>
Alabama
Alaska
dddfac
dfsase
cvcvss
dfqzcvc
ddddwq
bnnn
qqojfaso
>
Visit beautiful  Alabama
Visit beautiful  Alaska
Visit beautiful  dddfac
Visit beautiful  dfsase
Visit beautiful  cvcvss
Visit beautiful  dfqzcvc
Visit beautiful  ddddwq
Visit beautiful  bnnn
Visit beautiful  qqojfaso
 
#!/bin/bash
IFS=$'\n'
file="states"
for state in `cat $file`
do
echo "visit beautiful $state"
done
>
#cat states
>
A           labama
Alaska
dddfac
dfsase
cvcvss
dfqzcvc
ddddwq
bnnn
qqojfaso
>
visit beautiful A           labama
visit beautiful Alaska
visit beautiful dddfac
visit beautiful dfsase
visit beautiful cvcvss
visit beautiful dfqzcvc
visit beautiful ddddwq
visit beautiful bnnn
visit beautiful qqojfaso

在处理长脚本时,可能在一个地方需要修改IFS的值,然后忘掉它并在脚本中其他地方以为还是默认的值。一个可参考的简单实践:
IFS.OLD=$IFS
IFS=$'\n'
<use the new IFS value in code>
IFS=$IFS.OLD

#!/bin/bash
for file in /root/*
do
if  [ -d "$file" ]
then
echo " $file is a directory "
elif [ -f "$file" ]
then
echo "$file is a file"
fi
done

#!/bin/bash
for file in /root/.b* /home/rich/badtest
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
else
echo "$file doesn't exist"
fi
done

类C语言风格
#!/bin/bash
for (( i=1; i<=10; i++ ))
do
echo "The next nuber is $i"
done
>
The next nuber is 1
The next nuber is 2
The next nuber is 3
The next nuber is 4
The next nuber is 5
The next nuber is 6
The next nuber is 7
The next nuber is 8
The next nuber is 9
The next nuber is 10

#!/bin/bash
for (( a=1, b=10; a <= 12; a++, b-- ))
do
echo "$a - $b"
done
>
1 - 10
2 - 9
3 - 8
4 - 7
5 - 6
6 - 5
7 - 4
8 - 3
9 - 2
10 - 1
11 - 0
12 - -1
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013/09/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档