欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

shell-while-for

程序员文章站 2022-04-26 22:10:21
...
while 循环

1、基本用法:
#!/bin/bash
source /etc/profile

while read dataline
do 
  echo "$dataline"
done



2、循环读入一个文件。
方式1:
#!/bin/bash
source /etc/profile

cat $*|
while read dataline
do 
  echo "$dataline"
done


方式2:
#!/bin/bash
source /etc/profile

filename=$1
if [ ! -r "$filename" ];then
   echo "can not read $filename"
   exit 1
fi

while read dataline
do 
  echo "$dataline"
done < $filename



3、while 循环输出内容到一个文件。
#!/bin/bash
source /etc/profile

filename=$1
if [ ! -r "$filename" ];then
   echo "can not read $filename"
   exit 1
fi

while read dataline
do 
  echo "$dataline"
done < $filename >output.data


4、命令行执行for
for i in `cat num2.txt` ;do echo $i; done |awk 'BEGIN {a=0} {a+=$1} END {print a}' 
相关标签: shell while

推荐阅读