【shell编程】for循环

for..do..done用法

 

1. 例子:

#!/bin/bash
 
for((i=1;i<=10;i++));
do 
    echo $(expr $i \* 3 + 1);
done

2. 例子:

1. 格式1

#!/bin/bash
 
for i in $(seq 1 10)
do 
    echo $(expr $i \* 3 + 1);
done

2. 格式2:

#!/bin/bash
 
for i in $(seq 1 10); do 
    echo $(expr $i \* 3 + 1);
done

 

3. 例子:

#!/bin/bash

# 遍历数字 1 到 10
for i in {1..10}; do
  echo "当前数字是 $i"

  # 当数字等于 5 时退出循环
  if [ $i -eq 5 ]; then
    echo "找到了 5,退出循环"
    break
  fi
done

echo "循环结束"

4. 例子:

#!/bin/bash
 
awk 'BEGIN{for(i=1; i<=10; i++) print i}'

5. 例子:

#!/bin/bash
 
for file in /proc/*;
do
    echo $file is file path \! ;
done

5. 例子:

#!/bin/bash
 
for file in $(ls *.sh)
do
    echo $file is file path \! ;
done

6. 例子:

#!/bin/bash
 
for i in f1 f2 f3 ;
do
    echo $i is appoint ;
done

7. 例子:

#!/bin/bash
 
for i in `ls`;
do 
    echo $i is file name\! ;
done

 

posted @ 2022-05-30 21:23  苏格拉底的落泪  阅读(51)  评论(0编辑  收藏  举报