shell script while Loop instance : calculation 1 reach 100 And
#!/bin/bash
# The last line means to declare this script Using shell name , Symbol #! Combination means declaration shell Symbol # It's for annotation
i=1
temp=101
sum=0
while [ $i -ne $temp ] be careful : Here we must note that there must be a space after the left bracket and before the bracket , Otherwise, it will report an error
do
sum=$(($sum + $i)) be careful : There must be two brackets here
i=$(($i+1))
echo $sum Output at terminal sum Value of
done
attach : If you need to output content to the terminal during the execution of the script, you can add statements to the script echo calculation 1 reach 100 And
If you need to execute the command , Add the command to execute directly in the script ,
If you need to do it every other second, add sleep 1 The command is delayed by one second ,
If you need to output content to a text file during execution, you can add
echo calculation 1 reach 100 And >>filename.txt ( To text file filename.txt Medium output “ calculation 1 reach 100 And ”)
Technology