您的位置:首页 > 运维架构 > Shell

Shell脚本--刘晓涛

2015-07-01 10:49 597 查看
csh

bash kernel

other programs

The X Window System

Shell 脚本

如果你有一系列经常使用的Linux命令,你可以把他们存储在一个文件中。Shell可以读取这个文件并执行其中的命令。这样的文件被称为脚本文件。

执行shell脚本

bash magic / ./magic 执行脚本magic

*********************************************************

echo命令

用于在屏幕上显示消息

echo "this is an example of the echo command"

#符号

用于在shell脚本中可以包含注解入口

变量--内存空间--抽屉

创建变量 <variable name>=<value>

引用变量 variable=${variable2}

读入值给变量 read fname

//shell允许用户从键盘输入一个值给变量。

局部变量--只有当前shell能都知道变量的存在

全局变量--shell中创建的默认为局部变量,export特别之处则为全局的

环境变量--通过改变变量值,用户能够定制此环境

//例如:HOME,PATH,PS1,PS2,LOGNAME,SHLVL,SHELL

<1>HOME变量

Linux系统中的每个用户都有一个相关的称作HOME的目录。

当一个用户登录后,进入相应的HOME的目录

$echo $HOME

<2>PATH变量

包含一系列用冒号定界的目录的路径名字,便于可执行程序的搜索

<2>PS1变量

Prompt String 1变量包含了shell提示符,$符号

PS1-“HELLO>” 回车

HELLO> New prompt

<3>PS2

第二个提示符设置值的环境变量

<4>LOGNAME变量

包含用户的注册名字

echo "${echo}" 显示登录用户的登录名字

<5>SHLVL变量

该变量包含你当前工作的shell level

<6>SHELL变量

shell的类型

<7>env命令

查看所有环境变量和它们各自的值

*********************************************************

命令替换

echo "The date is ‘具体时间’"

expr 4+5 -- 9

$((expression))

*********************************************************

Example1

计算呼叫中心未应答的询问的数量。

echo "Please enter the total number of queries reproted

today."

read totalqueries

echo "Please enter the number of queries answered."

read answered

pending=$((totalqueries-answered))

echo "Number of calls pending=$pending"

chmod +x call pengding

*********************************************************

条件执行

test 和 []命令

求值表达式,并返回true (0)或false

if构造

算术测试

结合if构造,用于测试变量的数字值

串测试

文件测试

exit命令

用于终止shell脚本的执行并返回到$提示符下

test测试命令

<1>数值测试

-eq

-ne

-gt

-ge

-lt

-le

<2>字符串测试

= 等则为真

!= 不能

-z 字符串长度为零为真

-n 字符串长度部位零

<3>文件测试

-e文件名: 存在

-r文件名: 存在且可读

-w文件名: 存在且可写

-x文件名: 才能在且可执行

-s文件名: 存在且至少有一个字符

-d文件名: 存在且为目录

-f文件名: 存在且为普通文件

-c文件名: 存在且为字符型文件

-b文件名: 存在且为块文件

-a 并且

-o 或者

! 非

*********************************************************

Example2

#!/bin/bash

echo "Enter the percentage of calls answered same day."

read actual

if[ $actual -le 80 ]

then

echo "You Grade is Average."

elif[$acutal -gt 80 -a $actual -le 90]

then

echo "Your Grade is Good."

else

echo "Your Grade is Outstanding."

fi

*********************************************************

case...esac 构造

Example3

#!/bin/bash

echo ""

echo "1)"

echo "2)"

echo "3)"

echo "4)"

echo "5)"

echo -n ""

read choice

case $choice in

1)echo "";;

2)echo "";;

3)echo "";;

4)echo "";;

5)echo "";;

*)echo "";;

esac

*********************************************************

迭代

while构造

while <条件>

do

<命令(s)>

done

until构造

循环执行到求值条件为真时,停止。

for构造

for variable_name in <list_of_values>

do

...

done

break 终止循环。跳出循环。服务器循环。

continue 强迫一个新的重复。

Example4

#!/bin/bash

ecode=1000

while[ $ecode -le 1008 ]

do

echo "Enter data for the emplyee with Employee Code

=$ecode"

echo -n "Employee Name:"

read name

echo -n "Email Address:"

read email

echo -n "Telephone Number:"

read telno

echo "$ecode:$name:$telno">>??employee

((ecode=$ecode+1))

done // -n 表示不换行

*********************************************************

请求后台处理

$ wc tempfile &

// wc--计算字符

[1] 2082

$ vi newfile

检查后台进程

Ps 为每个当前活动的每个进程产生一行入口。

终止后台进程

kill 278

查看完成一个命令所花的时间

time find /etc -name "passwd" 2> /dev/null

管道

前面的命令的输出作为之后命令的输入,发送

ls -l | more

*********************************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: