[转帖]  Shell编程_VMware, Unix及操作系统讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  VMware, Unix及操作系统讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3299 | 回复: 0   主题: [转帖]  Shell编程        下一篇 
522080330
注册用户
等级:少校
经验:1478
发帖:79
精华:20
注册:2013-1-18
状态:离线
发送短消息息给522080330 加好友    发送短消息息给522080330 发消息
发表于: IP:您无权察看 2013-1-23 15:19:11 | [全部帖] [楼主帖] 楼主

    1、shell结构

    一个简单的例子:

北京联动北方科技有限公司

[root@localhost shell]# vi example

#!/bin/sh
#This is show what a example looks like.
echo "my first shell example."
echo # this inserts an empty line in output.
echo "We are current in the following directory."
/bin/pwd
echo
~
~
~


北京联动北方科技有限公司

    1)#!  指定执行脚本的shell

    2)#  注释行

    3)命令和控制结构

    2、创建shell的步骤

    1)创建一个包含命令和控制结构的文件

    2)修改这个文件的权限使它可以执行,使用 chmod u+x

    3)执行 ./文件名 或者 sh 文件名

北京联动北方科技有限公司

[root@localhost shell]# ./example
my first shell example.

We are current in the following directory.
/shell

[root@localhost shell]#


北京联动北方科技有限公司

    3、shell变量

    1)变量:是shell传递数据的一种方法,用来代表每个取值的符号名

    2)shell有两类变量:临时变量和永久变量

    临时变量是shell程序内部定义的,其使用范围仅限于定义它的程序,对其他程序不可见。包括用户自定义变量、位置变量。

    永久变量是环境变量,其值不随shell脚本的执行结束而消失

    3)用户自定义变量

    由字母或下划线开头,由字母、数字或下划线组成,并且大小写字母意义不同。变量名长度没有限制。

    在使用变量值时,要在变量名前加上前缀 $

    4)设置和使用变量

    习惯上用大写字母来命名变量。变量名只能以字母表中的字符开头,不能用数字。

    变量赋值:赋值号"="两边应没有空格

    使用echo命令查看变量值,如 echo $NUM

    定义时赋值,如 NUM=100   

[root@localhost shell]# NUM=100
[root@localhost shell]#


[root@localhost shell]# echo $NUM
100
[root@localhost shell]#


    将一个命令的执行结果赋给变量,如 TIME=`date` 注意`是数字键1旁边的那个符号,不是单引号

[root@localhost shell]# TIME=`date`
[root@localhost shell]# echo $TIME
2013年 01月 10日 星期四 22:57:14 CST
[root@localhost shell]#


[root@localhost shell]# TIME=$(date +%F)
[root@localhost shell]# echo $TIME
2013-01-10
[root@localhost shell]#



    将一个变量赋给另一个变量,如 DATE=$TIME

[root@localhost shell]# DATE=$TIME
[root@localhost shell]# echo $DATE
2013-01-10
[root@localhost shell]#


    列出所有变量 set

    包含多个字的变量,需要使用单引号或双引号扩起来

[root@localhost shell]# NAME="LI XUAN HUAN"
[root@localhost shell]# echo $NAME
LI XUAN HUAN
[root@localhost shell]#


    使用单引号和双引号的区别:单引号之间的内容原封不动的制定给了变量

北京联动北方科技有限公司

[root@localhost shell]# NOW="time is $DATE"
[root@localhost shell]# echo $NOW
time is 2013-01-10
[root@localhost shell]# NOW='time is $DATE'
[root@localhost shell]# echo $NOW
time is $DATE
[root@localhost shell]#


北京联动北方科技有限公司

    删除变量

[root@localhost shell]# unset NOW


    5)位置变量和特殊变量

    shell解释执行用户命令时,将命令行的第一个部分作为命令名,其他部分作为参数,由出现在命令行上的位置确定的参数称为位置参数。例如:

[root@localhost ~]# ls -l file1 file2 file3


    $0  这个程序的文件名 ls -l

    $n  这个程序的第n个参数值,n=1-9 $1就代表上面的file1

    例如脚本:test01.sh如下:

ls -l $1


    执行时我们传入一个值给位置变量$1

[root@localhost shell]# sh test01.sh /etc/inittab
-rw-r--r--. 1 root root 884 12月 5 04:50 /etc/inittab
[root@localhost shell]#



    6)特殊变量

    如:

[root@localhost shell]# ls /etc/inittab /home


    $* 这个程序的所有参数:在上面的命令就表示:/etc/inittab /home

$# 这个程序的参数个数:在上面的命令就表示:2 ,有两个参数


      $$ 这个程序的PID

[root@localhost shell]# ls -l /etc/inittab
-rw-r--r--. 1 root root 884 12月 5 04:50 /etc/inittab
[root@localhost shell]# echo $$
1591
[root@localhost shell]#


    $! 执行上一个后台命令的PID

    $? 执行上一个命令的返回值,如果上一个命令执行成功返回0,非0就是不成功

北京联动北方科技有限公司

[root@localhost shell]# ls -l /etc/inittab
-rw-r--r--. 1 root root 884 12月 5 04:50 /etc/inittab
[root@localhost shell]# echo $?
0
[root@localhost shell]# ls -l /etc/inittab4343
ls: 无法访问/etc/inittab4343: 没有那个文件或目录
[root@localhost shell]# echo $?
2
[root@localhost shell]#


北京联动北方科技有限公司

    4、shell命令

    1)read命令:从键盘读入数据,赋给变量。

北京联动北方科技有限公司

[root@localhost shell]# vi read.sh

#!/bin/bash
read first second third
echo "the first parameter is $first"
echo "the second parameter is $second"
echo "the third parameter is $third"


北京联动北方科技有限公司

[root@localhost shell]# sh read.sh
100 200 300
the first parameter is 100
the second parameter is 200
the third parameter is 300
[root@localhost shell]#


    sh +x 可以看到脚本执行的过程

北京联动北方科技有限公司

[root@localhost shell]# sh -x read.sh
+ read first second third
100 200 300
+ echo 'the first parameter is 100'
the first parameter is 100
+ echo 'the second parameter is 200'
the second parameter is 200
+ echo 'the third parameter is 300'
the third parameter is 300
[root@localhost shell]#


北京联动北方科技有限公司

    2)expr命令

    shell变量的算术运算

    expr命令:对整数型变量进行算术运算  

北京联动北方科技有限公司

[root@localhost shell]# expr 3 + 5
8
[root@localhost shell]# expr 10 - 5
5
[root@localhost shell]# expr 100 / 3
33
[root@localhost shell]# expr 2 \* 10
20
[root@localhost shell]#


北京联动北方科技有限公司

    注意两个参数和运算符之间要有空格

[root@localhost shell]# A=10
[root@localhost shell]# B=20
[root@localhost shell]# expr $A + $B
30
[root@localhost shell]#


    3)变量测试语句

    变量测试语句:用于测试变量是否相等、是否为空、文件类型等

    格式:test 测试条件

    测试范围:整数、字符串、文件

    变量测试语句一般不单独使用,一般作为if语句的测试条件,如:

    判断输入的参数是否是目录:

北京联动北方科技有限公司

[root@localhost shell]# vi test02.sh

#!/bin/bash
if test -d $1
then

echo "it is a directory."
else
echo "it is not a directory."
fi


北京联动北方科技有限公司

[root@localhost shell]# sh test02.sh /etc
it is a directory.
[root@localhost shell]#


    判断apache服务是否启动,如果没有启动就启动apache

北京联动北方科技有限公司

[root@localhost shell]# vi test03.sh

#!/bin/sh
# Detect apache service
WEB=`/usr/bin/pgrep httpd`
if [ "$WEB" != "" ]
then
echo "the Web service is running."
else
echo "the web service is NOT running."
/etc/rc.d/init.d/httpd start
fi


北京联动北方科技有限公司

北京联动北方科技有限公司

[root@localhost shell]# sh test03.sh
the Web service is running.

[root@localhost shell]# service httpd stop
停止 httpd:[确定]
[root@localhost shell]# sh test03.sh
the web service is NOT running.
正在启动 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[确定]
[root@localhost shell]#


北京联动北方科技有限公司

  5、流控制语句

    1)if

if 条件 then

  命令

fi


    2)if else    

北京联动北方科技有限公司

if 条件1 then

  命令1

else

  命令2
fi


北京联动北方科技有限公司

    3)if elif else  

北京联动北方科技有限公司

if 条件1 then
命令1
elif 条件2 then
命令2
else
命令3
fi


北京联动北方科技有限公司

    判断输入的两个参数的大小:

北京联动北方科技有限公司

[root@localhost ~]# vi test04.sh

#!/bin/bash
if [ $# -ne 2 ]; then
echo "Not enough parameters"
exit 0
fi
if [ $1 -eq $2 ]; then
echo "$1 equals $2"
elif [ $1 -lt $2 ]; then
echo "$1 littler than $2"
elif [ $1 -gt $2 ]; then
echo "$1 greater than $2"
fi
~


北京联动北方科技有限公司

北京联动北方科技有限公司

[root@localhost ~]# sh test04.sh 10
Not enough parameters
[root@localhost ~]# sh test04.sh 10 20
10 littler than 20
[root@localhost ~]# sh test04.sh 10 2
10 greater than 2
[root@localhost ~]# sh test04.sh 10 10
10 equals 10
[root@localhost ~]#


北京联动北方科技有限公司

    4)多个条件联合:

    -a 逻辑与,仅当两个条件都成立时,结果为真

     -o 逻辑或,两个条件只要有一个为真,结果为真

    5)exit 语句:退出程序执行,并返回一个返回码,返回码为0表示正常退出,非0表示非正常退出

    6)for done 语句

    格式:      

for 变量 in 名字表
do
命令列表
done


    如:

北京联动北方科技有限公司

[root@localhost ~]# vi test05.sh
#!/bin/bash

for DAY in Sunday Monday Tuesday wednesday Thursday Friday Saturday
do
echo "the day is: $DAY"
done


北京联动北方科技有限公司

北京联动北方科技有限公司

[root@localhost ~]# sh test05.sh
the day is: Sunday
the day is: Monday
the day is: Tuesday
the day is: wednesday
the day is: Thursday
the day is: Friday
the day is: Saturday
[root@localhost ~]#


北京联动北方科技有限公司

    7)awk命令的应用

    awk -F 域分隔符 ‘命令‘

    示例:

    7.1)检测系统中UID为0的用户 

[root@localhost ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash


    以:为分隔符,第三个位置参数($3)等于0(UID=0),然后把用户名(第一个位置参数$1)打印出来

[root@localhost ~]# awk -F: '$3==0 {print $1}' /etc/passwd
root
[root@localhost ~]#


    7.2)检测系统中密码位空的用户

[root@localhost ~]# awk -F: 'length($2)==0 {print $1}' /etc/shadow
luxh
[root@localhost ~]#


    8)踢出登录用户

    找出用户登录相关的PID,kill掉

北京联动北方科技有限公司

[root@localhost ~]# vi test06.sh

#!/bin/bash

# The script to kill logined user.

username="$1"

/bin/ps -ef | /bin/grep $username | /bin/awk '{print $2}' > /tmp/temp.pid

killid=`cat /tmp/temp.pid`

for PID in $killid
do
/bin/kill -9 $PID 2>/dev/null
done


北京联动北方科技有限公司

    比如,luxh01账号已登录系统,管理员要把luxh01踢出系统

[root@localhost ~]# sh test06.sh luxh01
已杀死
[root@localhost ~]#


    9)select in 语句  

select 变量 in 关键字
do
命令1
...
命令n
done


    select 把关键字中的每一项做成类似表单,以交互的方式执行do和done之间的命令

北京联动北方科技有限公司

[root@localhost shell]# vi test07.sh

#!/bin/bash

echo "What is your favourite OS?"

select var in "Linux" "Unix" "Windows" "Other"
do
break
done

echo "your have selected $var"


北京联动北方科技有限公司

北京联动北方科技有限公司

[root@localhost shell]# sh test07.sh
What is your favourite OS?
1) Linux
2) Unix
3) Windows
4) Other
#? 1
your have selected Linux
[root@localhost shell]#


北京联动北方科技有限公司

    10)case语句

case 变量 in
字符串1) 命令列表1
;;
字符串n) 命令列表n
esac



北京联动北方科技有限公司

Press ENTER or type command to continue
#!/bin/bash

echo "*************************************"
echo "Please select your operation:"
echo "Press "C" to Copy"
echo "Press "D" to Delete"
echo "Press "B" to Backup"
echo "*************************************"

read op
case $op in
C)
echo "your selection is Copy"
;;
D)
echo "your selection is Delete"
;;
B)
echo "your selection is Backup:"
;;
*)
echo "invalide selection"
esac


北京联动北方科技有限公司

北京联动北方科技有限公司

[root@localhost shell]# sh test08.sh
*************************************
Please select your operation:
Press C to Copy
Press D to Delete
Press B to Backup
*************************************
C
your selection is Copy
[root@localhost shell]#


北京联动北方科技有限公司

    11)while语句  

while 条件
do
命令
done


北京联动北方科技有限公司

[root@localhost shell]# vi test09.sh

#!/bin/bash

num=1
while [ $num -le 10 ]
do
SUM=`expr $num \* $num`
echo $SUM
num=`expr $num + 1`
done
~


北京联动北方科技有限公司

北京联动北方科技有限公司

[root@localhost shell]# sh test09.sh
1
4
9
16
25
36
49
64
81
100
[root@localhost shell]#


北京联动北方科技有限公司

    12)until语句

until 条件
do
命令
done


    类似于while,不同的是until是条件返回值为假时才继续执行

    13)跳出循环 break和continue

    break  跳出整个循环

    continue  跳过本次循环,进行下次循环

    14)shift指令

    参数左移,每执行一次,参数序列顺次左移一个位置,$#的值减1,用于分别处理每个参数,移出去的参数不再可用

北京联动北方科技有限公司

#!/bin/bash

if [ $# -le 0 ]
then
echo "Not enough parameters"
exit 0
fi
sum=0
while [ $# -gt 0 ]
do
sum=`expr $sum + $1`
shift
done
echo $sum


北京联动北方科技有限公司

[root@localhost shell]# sh test10.sh
Not enough parameters
[root@localhost shell]# sh test10.sh 1 2 3
6
[root@localhost shell]#


  6、函数应用

    1)函数的定义:      

函数名()
{
       命令序列
}


    2)函数的调用:不带()

    函数名 参数1 参数2 ...

    3)函数中的变量

    变量均为全局变量,没有局部变量

    函数中的参数:调用函数时,可以传递参数,在函数中用$1、$2...来引用

北京联动北方科技有限公司

#!/bin/bash

HELP()
{
       echo "Usage: sh function \$1 \$2 \$3"
}

if [ $# -ne 3 ]
then
HELP
else
echo "thank you for your input,the three arguments is 1 2 3"
fi


北京联动北方科技有限公司

    7、shell脚本调试

    1)sh -x script

    这将执行该脚本并显示所有变量的值

    2)sh -n script

    不执行脚本只是检查语法的模式,将返回所有语法错误

    8、如果让一个普通用户可以执行shell脚本:

    sh 脚本,条件:

    1)对脚本有r权限

    2)对脚本所在目录有rx权限

    直接写脚本全路径执行,条件:

    1)对脚本有rx权限

    2)对脚本所在目录有rx权限




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论