【linux/shell】shell中使用for循环读取数据

目录

一.for循环从列表中读取数据的几种形式

二.for循环从配置文件读取数据

三.for循环用通配符读取目录

四.for循环带有数字变量


一.for循环从列表中读取数据的几种形式

bash 复制代码
#!/bin/bash
listl="aa bb cc"
list2='aa bb cc'

for i in $list3
do
    echo $i
done

使用这种形式的列表时需要使用${list@}这样的形式访问变量list3=("aa" "bb" "cc")

bash 复制代码
#!/bin/bash
for i in ${list[@]}
do
    echo $i 
done

二.for循环从配置文件读取数据

从配置文件中读取数据:

bash 复制代码
#!/bin/bash
for moudle in $(cat PRE Config)
do
    echo $moudle
done

三.for循环用通配符读取目录

bash 复制代码
#!/bin/bash
for file in /home/rich/*
do
    if [ -d "$file" ]
    then
        echo "$file is a directory"
    elif [ -f "$file" ]
        echo "$file is a file"
    fi
done

四.for循环带有数字变量

第一种:使用C语言风格for循环

bash 复制代码
for(i=0;i<10;i++)

{
  printf("number is %d/n",i)
}

第二种:

bash 复制代码
for k in {1..14}
do
    n1='cat result1 |sed-n "$k"p | awk '{print $0}'
    n2='cat result2| sed -n "$k"p | awk '{print $0'}
    echo "scale=6; $n1/$n2"bc | awk '{printf "%.6f" $0}'>> result
done
相关推荐
AlfredZhao8 小时前
Docker 容器时区不对,`timedatectl` 不存在怎么办?
linux·timezone
zzzzzz3102 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode2 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
A小辣椒4 天前
TShark:Wireshark CLI 功能
linux
A小辣椒4 天前
TShark:基础知识
linux
AlfredZhao4 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao4 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334665 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪5 天前
linux 拷贝文件或目录到指定的位置
linux
摇滚侠5 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql