shell while循环

一时失言乱红尘 2022-09-20 05:56 309阅读 0赞

#!/bin/bash

x=0

# =

if [ “$x” -eq 0 ];

then

  1. x=1;
  2. echo "$x"

fi

while [ “$x” -lt 10 ];

do

  1. y="$x"
  2. echo "$x"
  3. x=\`expr $x + 1\`

done

#使用while 读取文件解析每行数据并赋予变量

test.sh

#!/bin/bash
while read id name age
do
echo “id:${id} name:${name} age:${age}“
done < ./data

#数据文件如下:

1 zhangsan 50
2 lishi 30

执行sh test.sh 输出如下:

id:1 name:zhangsan age:50
id:2 name:lishi age:30

发表评论

表情:
评论列表 (有 0 条评论,309人围观)

还没有评论,来说两句吧...

相关阅读

    相关 shell学习二十四--while循环

    一、当型循环和直到型循环 当型循环结构:在每次执行循环体前,对条件进行判断,当条件满足时,执行循环体,否则终止循环。 直到型循环结构:在执行了一次循环体后,对条件进行判