目录
1、数字循环对象
(1)常量范围
总结4种方式:表达循环1到10
双括号: ((a=1;a<=10;a++)) 遇到了awk嵌套的时候就是单括号 awk 'BEGIN{for(i=1; i<=10; i++) print i}'
大括号:{1..10}
seq反引号: `seq 1 10`
seq变量号: $(seq 1 10)
#!/bin/bash
for i in {1..10}
do
echo -n $i
done
echo -e
for ((i=1;i<11;i++))
do
echo -n $i
done
echo -e
for i in `seq 1 10`
do
echo -n $i
done
echo -e
for i in $(seq 1 10)
do
echo -n $i
done
echo -e
(2)含有变量的范围
总结3种方式:表达循环1到10
双括号:((a=1;a<=$w;a++))
seq反引号:`seq 1 $w`
seq变量号:$(seq 1 $w)
#!/bin/bash
w=10
for ((a=1;a<=$w;a++))
do
echo -n $a
done
echo -e
for c in `seq 1 $w`
do
echo -n $c
done
echo -e
for c in $(seq 1 $w)
do
echo -n $c
done
echo -e
#错误的使用{}大括号目前不知道杂用,一下是错误的使用
for b in {1..$w}
do
echo -n $b
done
echo -e
for d in {1..w}
do
echo -n $d
done
echo -e
for b1 in `{1..$w}`
do
echo -n $b1
done
echo -e
for b2 in $({1..$w})
do
echo -n $b2
done
2、字符串循环对象
(1)常量字符串
总结:引号+空格切割的,引号大于空格
#!/bin/bash
for i in "data0 data1 data data2";
do
echo $i is get
done
for y in f1 f2 f3
do
echo $y is get
done
for z in "f1" "f2" "f3"
do
echo $z is get
done
(2)字符串变量
总结:双引号加算整体,不加空格分隔
#!/bin/bash
#不加双引号怎么都是用空格分隔
list="data0 data data2"
for y in $list
do
echo $y is get
done
echo "******************************************"
for x in ${list[*]}
do
echo $x is get
done
echo "******************************************"
for a in ${list[@]}
do
echo $a is get
done
echo "******************************************"
#加了双引号怎么都算整体
for y in "$list"
do
echo $y is get
done
echo "******************************************"
for b in "${list[*]}"
do
echo $b is get
done
echo "******************************************"
for i in "${list[@]}"
do
var1="hello"
var2="world"
var3="jintian"
for z in $var1 $var2 $var3
do
echo $z is get
done
(3)$*和$@区别
区别:双引号加不加
$@加不加双引号没区别
$*加双引号就是把所有入参当做整体
范围:脚本以外获取的变量
#!/bin/bash
echo "------------------不穿衣服一样------------------------"
echo $*
echo $@
for i in $*
do
echo $i
done
for y in $@
do
echo $y
done
echo "------------------穿衣服不一样------------------------"
echo $*
echo $@
for i in "$*"
do
echo $i
done
for y in "$@"
do
echo $y
done
(4)路径循环
如何获取路径如下内容
#!/bin/bash
for file in /home/hanfang1/*;
do
echo $file is file path \! ;
done
for file in $(ls *.sh)
do
echo $file
done
for file in `ls *.sh`
do
echo $file
done