linux for循环怎么理解
发表于:2025-11-10 作者:千家信息网编辑
千家信息网最后更新 2025年11月10日,这期内容当中小编将会给大家带来有关linux for循环怎么理解,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。for循环是Linux shell 中最常用的结构。f
千家信息网最后更新 2025年11月10日linux for循环怎么理解
这期内容当中小编将会给大家带来有关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循环怎么理解了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。
循环
结构
内容
地方
风格
分析
专业
中小
内容丰富
就是
常用
文章
更多
知识
篇文章
脚本
行业
角度
语言
资讯
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
做好网络安全专项重点工作
沈阳新零售软件开发哪家好
最知名的网络安全培训
校园网络安全法规
gportal服务器卡
服务器访问具体流程
广东数据网络技术分类创新服务
北京求职软件开发
网络安全示范活动总结
软件开发制作公司
跨数据库join
市卫健委网络安全检查
网络安全保卫中队
软件开发部门绩效指标
手机软件开发需要哪些
数据库dump什么意思
网络安全宣传周培训
福田区光纤网络技术开发展示
地质勘探行业软件开发
网络安全什么方面好就业前景
银行网络安全意义
把数据库所有的时间改了
宿迁大型网络技术大概费用
规划数据库质量规范难统一
常见非关系型数据库的类型有哪些
如何控制数据库记录
网络安全的方案论证
东华科技 互联网金融
潍坊工具管理软件开发公司
成人计算机网络技术考试都考什么