shell编程入门

简介:

  本博文面向零基础,一步一步学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

相关文章
|
2月前
|
Shell Linux
Linux下的Shell基础——Shell概述和入门(一)
Linux下的Shell基础——Shell概述和入门(一)
38 0
Linux下的Shell基础——Shell概述和入门(一)
|
2月前
|
Ubuntu Linux Shell
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
59 0
|
6天前
|
监控 Shell 开发工具
Shell编程
Shell编程
|
26天前
|
存储 Java Shell
bigdata-04-shell编程基础
bigdata-04-shell编程基础
12 0
|
28天前
|
Shell Linux C++
【Shell 编程设计】 编写自己的清理后台的Shell脚本
【Shell 编程设计】 编写自己的清理后台的Shell脚本
30 1
|
28天前
|
存储 Shell 数据安全/隐私保护
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
22 0
|
28天前
|
Shell C语言 C++
【Shell 编程指南】shell中的(),{}几种语法用法
【Shell 编程指南】shell中的(),{}几种语法用法
17 0
|
28天前
|
Shell 程序员 Linux
【Shell 编程指南】shell运算操作符之(())
【Shell 编程指南】shell运算操作符之(())
19 0
|
2月前
|
Linux Shell
Linux下的Shell基础——正则表达式入门(四)
Linux下的Shell基础——正则表达式入门(四)
25 1
Linux下的Shell基础——正则表达式入门(四)
|
2月前
|
Unix Shell Linux
【Shell】Shell脚本入门
【Shell】Shell脚本入门
58 0