Unix Shell Scripting之'扫盲篇'

俗话说:"工欲善其事,必先利其器"。在Unix/Linux上做开发,这里的’器’也同样包括Unix Shell Script,遗憾亚,虽然自己在Unix上开发已经快2年了,但是对Unix Shell Script可以说是’Script盲’一个,很多稍微复杂些的Script自己根本都看不懂。其实这也是自己栽下的’苦果’,因为以前我一直’歧视’Script language,认为那不是真正程序员该精通的,所以也就没有认真钻研过。目前认识到了解一定的Script技术,可以很大程度提高自己的工作效率,有些小工具用Script实现方便极了。这里’扫盲’一是给自己’扫盲’,二是把这些基础的Script技术和那些对和我一样对Script不熟悉的人一起分享。这篇中的所涉及的Shell语法均为符合POSIX标准的语法,可能很多Shell不能与该语法完全相吻合。自己感觉Bash与POSIX Shell语法最为接近。

首先我们要知道Script Language属于’解释型’语言,其性能要逊于像C、C++这类的编译型语言。但是Script Language由于站在更高的层次,它编写相对简单,编写效率要高于编译型语言,用它来做一些系统管理和辅助性的工作绰绰有余。还有一点值得注意的是Shell script已经被标准化,这就意味着其可移植性相对较好,基本上是’Write Once, Run Everywhere’!

当你打开一个后缀为.sh的文件时,在文件的第一行一般有这样格式的字符串"#!usr/bin/xx",这种script文件叫’Self-Contained Script’,也就是说它可以像可执行程序那样被执行而不用你输入额外的字符,比如有这样的一个简单的’Self-Contained Script’:
/* helloworld.sh */
#! /usr/bin/bash
echo "hello world"
我们编辑完后,再赋予helloworld.sh可执行权限属性,这样我们就可以在命令行上敲入’helloworld’,’hello world’就输出到屏幕上了,更准确的说是标准输出上^_^。

Shell Script实际上是一组’命令’序列,它支持三种基本的命令:
内置命令 — 像cd、read、echo、printf等shell自己使用的一些命令;
Shell函数 — 用Shell Language写的函数,调用方式与外部命令相同;
外部命令 — Shell通过创建一个新的进程来运行这个命令,典型的’spawn’模式 — ‘fork + exec’.

既然Shell Script被称为一门语言,那么它也自然也不例外的拥有变量和相关的控制语句,我们逐一来学习一下。

[Shell变量]
1、定义变量:例如YOUR_VAR1=this_is_my_first_shell_var、YOUR_VAR2="this is my first shell var",对于变量值中间含有空格的,就按照YOUR_VAR2的定义方式,用双引号括上即可。注意在’='两边不允许有空格,否则会解释出错。
2、引用变量:例如echo $YOUR_VAR1、printf "${YOUR_VAR1} ${YOUR_VAR2}\n"、MY_VAR1=$YOUR_VAR1。在引用变量时最好用’{}’将你的变量括起来,否则当Shell解释器遇到这样的语句echo $YOUR_VAR1is,它就不认识了,修改成echo ${YOUR_VAR1}is后,则一切正常了。

[Shell环境变量]
每种Shell都有自己的环境变量,这些变量被该Shell下执行的程序所继承和共享,那我们如何定义环境变量呢?使用export命令。
export varname=value
或者
varname=value
export varname

例如:
/* 在环境变量文件中, Bash中为.bashrc,C Shell中为.cshrc */
hours_per_day=24
seconds_per_hour=3600
export hours_per_day
export seconds_per_hour

这样我们在命令行上敲入echo $hours_per_day后,我们就可以看到24输出在标准输出上了。

[条件分支判断]
Shell条件判断的语句格式:
if condition1
then
 statement1
 statement2
 ……….
elif condition2
then
 statement3
 statement4
 ……..   
else
 statement5
 ……..
fi

Shell Script支持多种判断condition的方法,如果在condition处是一个command, 那么如果该command执行成功,其退出状态(exit status)为0,否则为不等于0的值。当你既要获取command退出状态,而又不想要该command的输出时,有一个常用的技巧,那就是使用’/dev/null’文件设备。熟悉Unix编程的人都知道’/dev/null’文件的作用,这里不多说。看下面的例子:
#! /usr/bin/bash
if ls -l|grep myfile > /dev/null
then
        echo "myfile exists"
else
        echo "myfile doesn’t exist"
fi

在condition处我们大多数情况下都会利用test命令来判断condition,test命令有两种使用方式:
test operand1 operator operand2
或者是
[ operand1 operator operand2 ](省略test)

我们举例说明:

X=3
Y=6
if [ ${X} -lt ${Y} ]
then
        echo "${X} < ${Y}"
else
        echo "${X} > ${Y}"
fi

if test ${X} -lt ${Y}
then
        echo "${X} < ${Y}"
else
        echo "${X} > ${Y}"
fi

test命令提供的operator很是丰富,其中一目operator包括:
-n — operand non zero length
-z — operand has zero length
-d — there exists a directory whose name is operand
-f — there exists a file whose name is operand
二目operator包括:
-eq — the operands are integers and they are equal
-neq — the opposite of -eq
= — the operands are equal (as strings)
!= — opposite of =
-lt — operand1 is strictly less than operand2 (both operands should be integers)
-gt — operand1 is strictly greater than operand2 (both operands should be integers)
-ge — operand1 is greater than or equal to operand2 (both operands should be integers)
-le — operand1 is less than or equal to operand2 (both operands should be integers)

[循环语句]
和C语言类似,Shell也支持for、do..while loop,而且还支持break、continue这样的跳转语句。有了上面的基础理解这两组语句就不难了,看看例子一切都明白了。
e.g.-1
X=0
while [ $X -le 20 ]
do
        echo $X
        X=$((X+1))
done

e.g.-2
colour1="red"
colour2="blue"
colour3="green"
for X in "$colour1" "$colour2" "$colour3"
do
        echo $X
done

e.g.-3
colour4="red blue green"
for Y in $colour4
do
        echo $Y
done

这里e.g.-2和e.g.-3的输出是一样的。

[Shell函数]
Shell函数的定义与C语言也类似,格式如下:
[function] name ()
{
 command-list;
}
Shell Function的使用和外部command一样,不同的是Shell Fucntion运行在Shell进程上下文中,Shell在执行Shell Function时不另外启动一个新的进程。在Shell Function内部,Shell Fucntion参数屏蔽了其Parent Script的参数,见下面的例子:
/* test.sh */
#! /usr/bin/bash
printarglist ()
{
        echo ${1}
        echo ${2}
}
printarglist ${1} upload

执行:test.sh go download
输出:go upload

以上是Shell Script的基本语法结构,权当给自己’扫盲’了,当然Shell Script编程又很多经验和技巧,这需要在以后不断的实践中慢慢摸索了。

口语学习笔记-'电话沟通'

电话沟通是现在日常生活和商业活动中的一种重要的沟通方式,今天我要学习的就是如何使用英语进行有效的电话沟通。下面是’口语8000句’之’电话’的听写笔记。

[打电话]
This is Dennis Smith.
Hello, John?
Is this Mr. Dennis Smith?
Is this the finance department?
Is this Dr. Jim Baker’s office?
Do you mind if I use your phone?
May I speak to Mr Smith?
Is Mark there?
I’m sorry for calling you this late.
I hope I’m not disturbing you.
I hope I didn’t wake you up.
It is urgent I talk to Mr Smith now.
I’m calling about tomorrow’s meeting.
I’m returning your call.

[接电话]
Speaking!
It is me.
ABC Business College, May I help you?
Who is calling please?
Who in particular would you like to talk to?
He is been expecting your call.
Which Smith do you want to talk to ?
There are three Smiths here.
Would you mind calling back later?
Extension 103, please.
I’ll connect you to extension 103.
Hold on, please.
I’ll put him on.
I’ll transfer your call.
I’ll get your party for you. (here ‘party’ means the person who is called for)
I’m transferring your call to the sales department.
You have a call from Mr. Smith of ABC.
Your party is on the line.

[无法接电话时]
Her line is busy now.
I’m sorry, she is tied up at the moment.
Would you like to hold?
He is away from his desk.
He is in but he is not at his desk right now.
I’m sorry, he is not in right now.
When is he coming back?
He should be back in 10 minutes.
He should be back in the office next week.
He is on vacation until next week.
He called in sick today.
He is out of town now.
He is out to lunch now.
He is in a meeting now.
He is off today.

[留言]
Could you call back later?
Please call me back in ten minutes.
May I take a message?
I’ll try again later.
Can I leave a message?
I called but your line was busy.
Would you tell him that Tom called?
Please tell him to call me.
How can he get a hold of you? 如何联系你
Your number, please.
My number is …
Your can reach me at 12341234 until six o’clock.
Let me repeat the number, that is 12341234.
OK, I’ll tell him that you called.
How do you spell your name?
Mr. Tom called you during the meeting.
I’ll have him call you back.
Shall I have him call you back?

[挂断电话]
Thanks for calling.
Please call again anytime.
I’d better get off the phone.
I have to go now.
I guess I’d better get going. 挂电话
Nice talking to you, bye.
Please hung up the phone.
I was cut off. = I was disconnected.
She hang up on me. = She hang up before I finished.
The phone went dead.
Thank you for returning my call.

[打错电话]
I’m afraid you have the wrong number.
What number are you calling?
Who would you like to talk to?
There is no one here by that name.
There is no Bob in this office.
I’m sorry. I must have misdialed.

[电话留言]
This is Tom’s calling. Please call me as soon as possible.
This is Tom of ABC. Please call me when you get home. My number is 12341234.
This is a recording.
[打电话遇到困难]
Please speak a little more slowly.
I can’t hear you very well.
We have a bad connection.
Could you speak up, please?
The lines are crossed. 串线
I’m sorry to have kept you waiting.
Thank you for waiting.
You give me the wrong number.

[New Words To Me]
extension — an additional telephone connected to a main line.
put sb on — 让..接电话.
tie up — too busy to answer the phone.
call in sick — 打电话请病假

如发现本站页面被黑,比如:挂载广告、挖矿等恶意代码,请朋友们及时联系我。十分感谢! Go语言第一课 Go语言精进之路1 Go语言精进之路2 Go语言编程指南
商务合作请联系bigwhite.cn AT aliyun.com

欢迎使用邮件订阅我的博客

输入邮箱订阅本站,只要有新文章发布,就会第一时间发送邮件通知你哦!

这里是 Tony Bai的个人Blog,欢迎访问、订阅和留言! 订阅Feed请点击上面图片

如果您觉得这里的文章对您有帮助,请扫描上方二维码进行捐赠 ,加油后的Tony Bai将会为您呈现更多精彩的文章,谢谢!

如果您希望通过微信捐赠,请用微信客户端扫描下方赞赏码:

如果您希望通过比特币或以太币捐赠,可以扫描下方二维码:

比特币:

以太币:

如果您喜欢通过微信浏览本站内容,可以扫描下方二维码,订阅本站官方微信订阅号“iamtonybai”;点击二维码,可直达本人官方微博主页^_^:
本站Powered by Digital Ocean VPS。
选择Digital Ocean VPS主机,即可获得10美元现金充值,可 免费使用两个月哟! 著名主机提供商Linode 10$优惠码:linode10,在 这里注册即可免费获 得。阿里云推荐码: 1WFZ0V立享9折!


View Tony Bai's profile on LinkedIn
DigitalOcean Referral Badge

文章

评论

  • 正在加载...

分类

标签

归档



View My Stats