网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

shell脚本编程简介-创新互联

1.什么是shell

成都创新互联拥有一支富有激情的企业网站制作团队,在互联网网站建设行业深耕十载,专业且经验丰富。十载网站优化营销经验,我们已为上1000家中小企业提供了网站设计、网站制作解决方案,定制网站制作,设计满意,售后服务无忧。所有客户皆提供一年免费网站维护!

2. 还是hello world程序

3. shell中的变量

3.1 系统变量

3.2 用户定义变量

3.2.1 用户定义变量规则

3.2.3 shell如何使用变量

3.2.3 全局变量 vs 局部变量

4. shell编程中的控制结构

4.1 条件判定

4.1.1 简单条件判定

4.1.2 组合判定

4.2  if - else

4.3 for

4.4 while

4.5 case

5. shell中的函数

5.1 函数声明和定义

5.2 函数调用

6. shell脚本调试

6.1 万能的echo

6.2  两个命令

7. 参考资料及shell脚本下载

<1>. 什么是shell

shell扮演者操作系统内核和用户的中间人的角色,用户通过键入shell command,然后shell通过解析用户输入,然后将请求转发给操作系统的内核进行处理。

shell脚本编程简介 1. 一个系统可以存在多个shell,可以通过cat /etc/shells命令查看系统中安装的shell,不同的shell可能支持的命令语法是不相同的。

shell脚本编程简介 2. 可以通过echo $SHELL查看当前使用的shell

<2>. 还是hello world程序

首先使用vim编辑器(或者是linux下任意的文本编辑器)编写文件helloshell.sh(没有必要使用.sh后缀名):

 #!/bin/bash

echo "hello shell";

保存上面的文件,增加该文件的执行权限:

 xuqiang@ubuntu:~/shell$ sudo chmod +x ./helloshell.sh

运行该shell程序:

 xuqiang@ubuntu:~/shell$ ./helloshell.sh

hello shell

通过上面的程序没有什么实际的含义,但是通过第一个shell程序了解shell程序的执行过程。

<3>. shell中的变量

3.1 系统变量

linnux下的shell脚本中的变量分为“系统变量”和“用户自定义变量”,可以通过set命令查看那系统变量。

xuqiang@ubuntu:~/shell$ set

... 略去内容

xuqiang@ubuntu:~/shell$ echo $HOME

/home/xuqiang

3.2 用户定义变量

shell中用户可以自定义变量,shell中的变量是没有数据类型的,shell将根据当前的环境自动进行转化,例如:

msg="hello world"

上面的语句定义变量msg,并设置初始值是为hello world。

 shell脚本编程简介 Tip 1. 需要注意的是定义变量时,=两边是没有空格的

3.2.1  用户定义变量规则

变量必须是以字母开头,后跟字母或者是下划线,变量的命名是大小写敏感的,并且可以定义一个变量的值为NULL。

xuqiang@ubuntu:~/shell$ vech=

xuqiang@ubuntu:~/shell$ echo $vec

3.2.2  shell中如何使用变量

如果想要得到shell变量中存储的值的话,需要在变量名前增加$符号,例如:

xuqiang@ubuntu:~/shell$ vech="value"

xuqiang@ubuntu:~/shell$ echo $vech # this will print the value of vech

value

xuqiang@ubuntu:~/shell$ echo vech # this will print the string "vech"

vech

3.2.3 全局变量 vs 局部变量

默认在shell中编写的变量全部是局部变量,如果重新打开console的话,那么这些变量将全部丢失,全局的变量可以写在文件~/.bashrc文件。

<4>. 控制结构

4.1 条件判定

在shell中条件判断是通过test命令或者是[ ]实现, 判断条件如下:

数学运算:

a -eq b :a == b
a -ne b : a != b
a -lt b : a < b
a -le b : a <= b
a -gt b : a > b
a -ge b : a >= b

string比较:
string1 = string2
string1 != string2
string1 : string1 不是NULL,或者是没有定义

4.2 组合判定 

逻辑运算符:

! : not

exp1 -a exp2 : a && b

exp1 -o exp2 : exp1 || exp2

4.3 if-else结构:

shell脚本编程简介

!/bin/sh#
# see whether arguments is positive
#
if [ $# -ne 1 ]
then
        echo "$0 : you must give/supply one integers"
        exit 1
fi

if test $1 -gt 0
then
        echo "$1 number is postivie"
else
        echo "$1 number is negative"
fi

shell脚本编程简介

shell脚本编程简介

#!/bin/sh

osch=0

echo "1. unix(sun os)"
echo "2. linux(red hat)"
echo -n "select your os choice [1 or 2] ?"
read osch

if [ $osch -eq 1  ]
then
        echo "you pick up unix"
else
        #
        # nested if
        if [ $osch -eq 2 ]
        then
                echo "you pick up linux"
        else
                echo "what you donot like unix/linux"
        fi
fi

shell脚本编程简介

shell脚本编程简介

#!/bin/sh
#
# test the if .. elif .. else
#
if [ $1 -gt 0 ] ; then
        echo "$1 is positive"
elif [ $1 -lt 0 ] ; then
        echo "$1 is negative"
elif [ $1 -eq 0 ] ; then
        echo "$1 is zero"
else
        echo "$1 is not a number"
fi

shell脚本编程简介

 4.4 for

shell脚本编程简介

!/bin/sh

#
# test for
#

for i in 1 2 3 4 5
do
        echo "welcome $i times"
done

shell脚本编程简介

shell脚本编程简介

#!/bin/bash

for (( i = 0; i <= 5; i++ ))
do
        echo "welcome $i times"

done

shell脚本编程简介

shell脚本编程简介 Tip 1. 注意程序中使用的shell脚本的类型

shell脚本编程简介

#!/bin/bash

for(( i = 1; i <= 5; i++  ))
do
        for(( j = 1; j <= 5; ++j ))
        do
                echo -n "$i"
        done

# print a new line
        echo ""
done

shell脚本编程简介

4.5 while

shell脚本编程简介

#!/bin/bash

#
# test while loop
#

n=$1
i=0
while [ $i -le 10 ]
do
        echo "$n * $i = `expr $i \* $n`"
        i=`expr $i + 1`

done

shell脚本编程简介

4.6 case 

shell脚本编程简介

#!/bin/bash

#
# script to test case statement
#
action="update"
case $action in
        "update")
                echo "update the db"
                ;;
        "select")
                echo "select from db"
                ;;
        "delete")
                echo "delete from db"
                ;;
        *)
                echo "no action"
                ;;

esac

shell脚本编程简介

<5>. 函数

5.1 函数声明和定义

下面的程序中定义函数demo,向函数传递的所有参数表示为$*,第一个参数$1,第二个参数$2, 依次类推。

shell脚本编程简介

#!/bin/bash

function demo()
{
        echo "all function args : $*"
        echo "the first arg : $1"
        echo "the second arg : $2"
        echo "the third arg : $3"
}

# call the function

demo -f foo bar

shell脚本编程简介

5.2 函数调用(函数参数)

shell中向函数传递参数是通过直接在函数调用后面添加参数完成,在函数中,传递的参数通过$1得到。

<6>. 脚本调试

6.1 万能的echo

shell的脚本调试是比价恶心的,这里仅仅是提供一些常规性的调试方法,最简单的就是使用echo函数打印出变量的值从而达到调试目的。

6.2 两个命令

shell脚本执行可以通过./shell-filename.sh的形式执行,另外的一种形式是通过bash ./shell-filename.sh的形式执行,同时在执行脚本时,可以使用-v或者是-x参数打印程序执行信息。

-v:默认打印shell读取的内容

-x:将命令“展开” 之后打印

例如对于下面的程序:

!/bin/sh

# for debug shell script
#
tot=`expr $1 + $2`
echo $tot

如果使用-v选项,结果如下:

 xuqiang@ubuntu:~/shell$ sh -v ./debug.sh 2 5

#!/bin/sh

#

# for debug shell script

#

tot=`expr $1 + $2`

echo $tot

7

如果是使用-x选项的话,结果:

 xuqiang@ubuntu:~/shell$ sh -x ./debug.sh 2 5

+ expr 2 + 5

+ tot=7

+ echo 7

7

转载地址:http://www.cnblogs.com/xuqiang/archive/2011/04/27/2031034.html

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站栏目:shell脚本编程简介-创新互联
分享URL:http://bjjierui.cn/article/djgssh.html

其他资讯