Bash脚本编程:控制结构、变量与文件操作全解析
1. 循环结构
在Bash脚本中,循环结构是非常实用的工具,它可以帮助我们高效地处理多个文件或执行多次相同的操作。
- 遍历当前目录文件 :
#!/bin/bash
# example to go through all files in the current directory
for file in *
do
echo $file
done
执行上述脚本,会显示当前目录下的文件名,但以点(.)开头的文件名不会显示。若要显示所有文件,可使用以下代码:
for file in .* *
do
echo $file
done
- 类C语言的for循环 :Bash还支持类似C语言的for循环,可用于数值计算和数组索引。
#!/bin/bash
for (( i = 0 ; i < 9 ; i++ ))
do
echo $i
done
2. if命令
if结构用于条件执行命令,其简单形式如下:
if test-expr
then
commandlist1
else
command
超级会员免费看
订阅专栏 解锁全文
1159

被折叠的 条评论
为什么被折叠?



