shell编程入门

  本博文面向零基础,一步一步学shell编程,入门!

 

shell编程入门

shell编程入门

shell编程入门

shell编程入门

shell编程入门

shell编程入门

 

 shell编程入门

shell编程入门

shell编程入门

shell编程入门

shell编程入门

shell编程入门

#!/bin/bash

echo "前进程号:"$$

echo "start"

sleep 10

kill  $$

sleep 900000000

echo "end"

 

shell编程入门

shell编程入门

shell编程入门

[[email protected] shell]# cat for.sh 
#!/bin/bash
for ((i=0;i<10;i++))
do
echo $i
done
[[email protected] shell]# for.sh 
0
1
2
3
4
5
6
7
8
9
[[email protected] shell]#

 

 

shell编程入门

[[email protected] shell]# more for1.sh 
#!/bin/bash
for i in 0 1 2 3 4 5 6 7 8 9
do
echo $i
done
[[email protected] shell]# for1.sh 
0
1
2
3
4
5
6
7
8
9
[[email protected] shell]#

 

 

 

 

shell编程入门

[[email protected] shell]# more for2.sh 
#!/bin/bash
for i in {0..9}
do
echo $i
done
[[email protected] shell]# for2.sh 
0
1
2
3
4
5
6
7
8
9
[[email protected] shell]#

 

 

 

 

 

 

shell编程入门

shell编程入门

shell编程入门

shell编程入门

[[email protected] shell]# more while.sh 
#!/bin/bash
while [ $1 -gt 2 ]
do

echo "true"
sleep 1
done
[[email protected] shell]#

 

 

 

shell编程入门

[[email protected] shell]# more until.sh 
#!/bin/bash
until [ $1 -gt 2 ]
do

echo "true"
sleep 1
done

[[email protected] shell]#

 

 

 

 

 

shell编程入门

shell编程入门

[[email protected] shell]# more if.sh 
#!/bin/bash
if [ $1 -eq 1 ] 
then
echo 'one'
else
echo 'none'
fi
[[email protected] 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
[[email protected] shell]#

 

 

 

 

 

 

 

 

shell编程入门

shell编程入门

[[email protected] shell]# more case.sh 
#!/bin/bash
case $1 in
1)
echo 'one'
;;
2)
echo 'two'
;;
3)
echo 'three'
;;
*)
echo 'none'
;;
esac
[[email protected] shell]# case.sh
none
[[email protected] shell]#

 

 

 

 

 

 

 

 

shell编程入门

shell编程入门

这里,我就在,/下新建shell目录,用来作为shell编程的入门。

[[email protected] /]# 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

[[email protected] /]# mkdir shell

[[email protected] /]# 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

[[email protected] /]# cd shell/

[[email protected] shell]# ll

total 0

[[email protected] shell]#

 

shell编程入门

[[email protected] shell]# ls

[[email protected] shell]# vim break1.sh

 

shell编程入门

[[email protected] shell]# ll

total 4

-rw-r--r--. 1 root root 79 Oct 22 09:15 break1.sh

[[email protected] shell]# chmod +x break1.sh

[[email protected] shell]# ll

total 4

-rwxr-xr-x. 1 root root 79 Oct 22 09:15 break1.sh

[[email protected] 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

[[email protected] shell]# break1.sh

-bash: break1.sh: command not found

[[email protected] shell]# ./break1.sh                  因为,此刻,还没加入到环境变量

0

1

[[email protected] shell]#

 

shell编程入门

[[email protected] shell]# pwd

/shell

[[email protected] shell]# vim /etc/profile

export PATH=$PATH:/shell/

 

shell编程入门

shell编程入门

[[email protected] shell]# source /etc/profile

[[email protected] shell]# break1.sh

0

1

[[email protected] shell]#

这样,就达到了。

 

shell编程入门

 

现在,写,while循环在外,for循环在内。

shell编程入门

[[email protected] shell]# ls

break1.sh  break2.sh

[[email protected] 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

[[email protected] shell]# break2.sh

0

1

yes

0

1

yes

0

1

yes

0

1

yes

^C

[[email protected] shell]#

 

shell编程入门

shell编程入门

[[email protected] 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

[[email protected] shell]# break3.sh

0

1

[[email protected] 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

 

shell编程入门

shell编程入门

[[email protected] shell]# cat continue.sh

#/bin/bash

for ((i=0;i<10;i++))

do

if [ $i -eq 2 ]

then

continue

fi

echo $i

done

[[email protected] shell]# continue.sh

0

1

3

4

5