原文链接:

Shell编程  打算有时间简单了解shell编程

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权限

【转】Shell编程的更多相关文章

  1. Linux学习笔记(17) Shell编程之基础

    1. 正则表达式 (1) 正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配.grep.awk.sed等命令可以支持正则表达式:通配符用来匹配符合条件的文件名,通配符是完全匹配.ls.find ...

  2. shell编程:定义简单标准命令集

    shell是用户操作接口的意思,操作系统运行起来后都会给用户提供一个操作界面,这个界面就叫shell,用户可以通过shell来调用操作系统内部的复杂实现,而shell编程就是在shell层次上进行编程 ...

  3. Linux Shell编程入门

    从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...

  4. Shell编程菜鸟基础入门笔记

    Shell编程基础入门     1.shell格式:例 shell脚本开发习惯 1.指定解释器 #!/bin/bash 2.脚本开头加版权等信息如:#DATE:时间,#author(作者)#mail: ...

  5. Linux_10------Linux之shell编程------变量

    .-9 vim num.sh #! /bin/bash num1=$1 num2=$2 sum=$(($num1+$num2)) #变量sum是num1和num2的综合 echo $sum 执行 ./ ...

  6. 需要交互的shell编程——EOF(转载)

    在shell编程中,”EOF“通常与”<<“结合使用,“<<EOF“表示后续的输入作为子命令或子shell的输入,直到遇到”EOF“, 再次返回到主调shell,可将其理解为分 ...

  7. ****CodeIgniter使用cli模式运行,把php作为shell编程

    shell简介 在计算机科学中,Shell俗称壳(用来区别于核).而我们常说的shell简单理解就是一个命令行界面,它使得用户能与操作系统的内核进行交互操作. 常见的shell环境有:MS-DOS.B ...

  8. Shell 编程基础之变量和环境变量

    一.变量赋值和引用 Shell 编程中,使用变量无需事先声明,同时变量的命名不惜遵循如下规则: 首个字符必须为字母(a-z,A-Z)或者_ 变量名中间不能有空格,可以使用_连接 不能使用其他表达符号 ...

  9. Linux Shell编程基础

    在学习Linux BASH Shell编程的过程中,发现由于不经常用,所以很多东西很容易忘记,所以写篇文章来记录一下 ls   显示当前路径下的文件,常用的有 -l 显示长格式  -a 显示所有包括隐 ...

  10. centos 下建用户 shell编程

    useradd 用户名 passwd 用户名 cat /etc/passwd  查看用户信息 删除用户 userdel -r   加一个 -r 表示把用户及用户的主目录都删除 su  切换用户 sud ...

随机推荐

  1. PHP的环境搭建

    下载开发环境 wampserver 下载sublime text 2 sublime使用技巧 1:安装漂亮的编程字体http://pan.baidu.com/s/1xMex9 下载"程序编写 ...

  2. jQuery异步请求(如getJSON)跨域解决方案

    相信大家在使用jQuery异步请求非自己网站内相对资源(通过别人站点上的URL直接读取)使经常会遇到如下错误吧,实际上这些错误都是浏览器安全机制“搞的鬼”,才让我们开发路上遇到了拦路虎. 当你直接在浏 ...

  3. jQuery事件函数bind,live,delegate的区别

    DOM树 首先,可视化一个HMTL文档的DOM树是很有帮助的.一个简单的HTML页面看起来就像是这个样子: 事件冒泡(又称事件传播) 当我们点击一个链接时,其触发了链接元素的单击事件,该事件则引发任何 ...

  4. Delphi中的“委托”

    .NET中有委托(Delegate)的概念,其声明形式如下所示:     public delegate void MyDelegate(int aIntParam, string aStringPa ...

  5. Json.Net系列教程 3.Json.Net序列化和反序列化设置

    原文 Json.Net系列教程 3.Json.Net序列化和反序列化设置 上节补充 首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framewo ...

  6. TCP/IP笔记 三.运输层(3)——TCP超时重传算法

    TCP 每发送一个报文段,就对这个报文段设置一次计时器.只要计时器设置的重传时间到但还没有收到确认,就要重传这一报文段 1. 平均往返时延RTT 往返时延:一个报文段发出的时间,以及收到相应的确认报文 ...

  7. LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

    获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.L ...

  8. 腾讯webqq最新password加密算法,hash算法

    常常在做webqq机器人,可是最头痛的问题就是腾讯常常加一些验证串来防止robot,如今共享出最新的腾讯password加密算法和hash 算法 hash算法 def webqq_hash(i, a) ...

  9. Android实现视频录制

    安卓实现视频录制,有两种方法,一种是调用自带的视频功能,一种是使用MediaRecorder. 每种方法都有自己的优缺点.接下来,把两种方法的代码写出来. 先说第一种方法,也是最简单的方法,那就是直接 ...

  10. springAOP配置原理

    什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...