本博文面向零基础,一步一步学shell编程,入门!
#!/bin/bash
echo "前进程号:"$$
echo "start"
sleep 10
kill $$
sleep 900000000
echo "end"
[root@weekend110 shell]# cat for.sh
#!/bin/bash
for ((i=0;i<10;i++))
do
echo $i
done
[root@weekend110 shell]# for.sh
0
1
2
3
4
5
6
7
8
9
[root@weekend110 shell]#
[root@weekend110 shell]# more for1.sh
#!/bin/bash
for i in 0 1 2 3 4 5 6 7 8 9
do
echo $i
done
[root@weekend110 shell]# for1.sh
0
1
2
3
4
5
6
7
8
9
[root@weekend110 shell]#
[root@weekend110 shell]# more for2.sh
#!/bin/bash
for i in {0..9}
do
echo $i
done
[root@weekend110 shell]# for2.sh
0
1
2
3
4
5
6
7
8
9
[root@weekend110 shell]#
[root@weekend110 shell]# more while.sh
#!/bin/bash
while [ $1 -gt 2 ]
do
echo "true"
sleep 1
done
[root@weekend110 shell]#
[root@weekend110 shell]# more until.sh
#!/bin/bash
until [ $1 -gt 2 ]
do
echo "true"
sleep 1
done
[root@weekend110 shell]#
[root@weekend110 shell]# more if.sh
#!/bin/bash
if [ $1 -eq 1 ]
then
echo 'one'
else
echo 'none'
fi
[root@weekend110 shell]# more if1.sh
#!/bin/bash
if [ $1 -eq 1 ]
then
echo 'one'
elif [ $1 -eq 2 ]
then
echo 'two'
elif [ $1 -eq 3 ]
then
echo 'three'
else
echo 'none'
fi
[root@weekend110 shell]#
[root@weekend110 shell]# more case.sh
#!/bin/bash
case $1 in
1)
echo 'one'
;;
2)
echo 'two'
;;
3)
echo 'three'
;;
*)
echo 'none'
;;
esac
[root@weekend110 shell]# case.sh
none
[root@weekend110 shell]#
这里,我就在,/下新建shell目录,用来作为shell编程的入门。
[root@weekend110 /]# ls
bin data etc lib lost+found misc net proc sbin srv tmp var
boot dev home lib64 media mnt opt root selinux sys usr
[root@weekend110 /]# mkdir shell
[root@weekend110 /]# ls
bin data etc lib lost+found misc net proc sbin shell sys usr
boot dev home lib64 media mnt opt root selinux srv tmp var
[root@weekend110 /]# cd shell/
[root@weekend110 shell]# ll
total 0
[root@weekend110 shell]#
[root@weekend110 shell]# ls
[root@weekend110 shell]# vim break1.sh
[root@weekend110 shell]# ll
total 4
-rw-r--r--. 1 root root 79 Oct 22 09:15 break1.sh
[root@weekend110 shell]# chmod +x break1.sh
[root@weekend110 shell]# ll
total 4
-rwxr-xr-x. 1 root root 79 Oct 22 09:15 break1.sh
[root@weekend110 shell]# cat break1.sh //如果变量i达到2,就跳出循环
#!/bin/bash
for ((i=0;i<10;i++))
do
if [ $i -eq 2 ]
then
break
fi
echo $i
done
[root@weekend110 shell]# break1.sh
-bash: break1.sh: command not found
[root@weekend110 shell]# ./break1.sh 因为,此刻,还没加入到环境变量
0
1
[root@weekend110 shell]#
[root@weekend110 shell]# pwd
/shell
[root@weekend110 shell]# vim /etc/profile
export PATH=$PATH:/shell/
[root@weekend110 shell]# source /etc/profile
[root@weekend110 shell]# break1.sh
0
1
[root@weekend110 shell]#
这样,就达到了。
现在,写,while循环在外,for循环在内。
[root@weekend110 shell]# ls
break1.sh break2.sh
[root@weekend110 shell]# cat break2.sh
#!/bin/bash
while [ 1 -eq 1 ]
do
for ((i=0;i<10;i++))
do
if [ $i -eq 2 ]
then
break
fi
echo $i
done
echo 'yes'
sleep 1
done
[root@weekend110 shell]# break2.sh
0
1
yes
0
1
yes
0
1
yes
0
1
yes
^C
[root@weekend110 shell]#
[root@weekend110 shell]# cat break3.sh
#!/bin/bash
while [ 1 -eq 1 ]
do
for ((i=0;i<10;i++))
do
if [ $i -eq 2 ]
then
break 2 //在这里,默认是1,写个2,则说的是跳出2层循环。
fi
echo $i
done
echo 'yes'
sleep 1
done
[root@weekend110 shell]# break3.sh
0
1
[root@weekend110 shell]#
跳出单循环
for((i=0;i<10;i++))
do
echo $i
if [ $i -eq 2 ]
then
break
fi
done
跳出内循环
while [ 1 -eq 1 ]
do
echo 'yes'
sleep 1
for((i=0;i<10;i++))
do
echo $i
if [ $i -eq 2 ]
then
break
fi
done
done
跳出外循环
while [ 1 -eq 1 ]
do
echo 'yes'
sleep 1
for((i=0;i<10;i++))
do
echo $i
if [ $i -eq 2 ]
then
break 2
fi
done
done
[root@weekend110 shell]# cat continue.sh
#/bin/bash
for ((i=0;i<10;i++))
do
if [ $i -eq 2 ]
then
continue
fi
echo $i
done
[root@weekend110 shell]# continue.sh
0
1
3
4
5