linux for循环怎么理解

发布时间:2022-01-21 11:02:43 作者:柒染
来源:亿速云 阅读:193

这期内容当中小编将会给大家带来有关linux for循环怎么理解,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

for循环是Linux shell 中最常用的结构。for 循环有三种结构:一种结构是列表for循环;第二种结构是不带列表for循环;第三种结构是类C风格的for循环。

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'

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; ido
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 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

上述就是小编为大家分享的linux for循环怎么理解了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. linux清空某目录内文件的方法
  2. 如何在Linux中shell编程实现for循环

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux

上一篇:VMware虚拟机怎样安装Linux系统

下一篇:plsql可不可以连接mysql

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》