for语句的用法

时间:2021-11-11 16:00:38

#!/bin/bash
for i in 1 2 3 4 5 6
do
echo $i
done

看文件

#!/bin/bash
dir=$(ls /etc)

for i in $dir
do
echo $i
done

判断

#!/bin/bash
read -p "please input a dirname:" -t 30 filename
# 如果字符串为空,报错跳出
if [ -z $filename ];then
echo "please input !!"
exit 1
fi

# 如果文件或目录不存在,报错跳出
if [ ! -e $filename ];then
echo "$filename no found"
exit 2
fi

# 如果不是目上报错跳出
if [ ! -d $filename ];then
echo "$filename is not dir"
exit 3
fi

file = $(ls $filename)

for d in $file
do
echo $d
done

累加for

#!/bin/bash

s=0
for ((i=1;i<=100;i=i+1))
do
s=$(($s+i))
done
echo $s