1. For loop
1. Digital field form
Copy the codeThe code is as follows:
for i in {1..10}
do
echo $i
done
2. List in detail (characters and few items)
Copy the codeThe code is as follows:
for File in 1 2 3 4 5
do
echo $File
done
3. Loop on existing files
Copy the codeThe code is as follows:
for shname in `ls *.sh`
do
name=`echo "$shname" | awk -F. '{print $1}'`
echo $name
done
4. Search loop (This method can also be used when the ls data volume is too large)
Copy the codeThe code is as follows:
for shname in `find . -type f -name "*.sh"`
do
name=`echo "$shname" | awk -F/ '{print $2}'`
echo $name
done
5.((grammar loop--a bit like C syntax, but remember double brackets
Copy the codeThe code is as follows:
for((i=1;i<100;i++))
do
if((i%3==0))
then
echo $i
continue
fi
done
Form Start from 1
Copy the codeThe code is as follows:
for i in `seq 100`
do
if((i%3==0))
then
echo $i
continue
fi
done
2. While loop
Pay attention to square brackets[] and pay attention to spaces
Copy the codeThe code is as follows:
min=1
max=100
while [ $min -le $max ]
do
echo $min
min=`expr $min + 1`
done
2. Double bracket form, the internal structure is a bit like the syntax of C. Pay attention to the assignment: i=$(($i+1))
Copy the codeThe code is as follows:
i=1
while(($i<100))
do
if(($i%4==0))
then
echo $i
fi
i=$(($i+1))
done
3. Read from the configuration file and can control the number of processes
Copy the codeThe code is as follows:
MAX_RUN_NUM=8
cat cfg/res_card_partition.cfg |grep -v '^$'|grep -v "#" | grep -v grep |while read partition
do
nohup sh inv_res_card_process.sh $partition >log/resCard$ 2>&1 &
while [ 1 -eq 1 ]
do
psNum=`ps -ef | grep "inv_res_card_process" | grep -v "grep" | wc -l`
if [ $psNum -ge $MAX_RUN_NUM ]
then
sleep 5
else
break
fi
done
done
3. Loop control statement
Copy the codeThe code is as follows:
The #break command does not execute the statement below break in the current loop body and exits from the current loop.
The # continue command is the following statement that the program ignores in the loop body and executes from the loop header.