Unix scripting 3 - a first script

本文将引导您创建第一个简单的Shell脚本,通过编写并执行一个打印'Hello World'的脚本来理解基本的Shell编程概念,包括文件权限、注释、命令参数解析等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

For our first shell script, we'll just write a script which says"Hello World". We will then try to get more out of a Hello Worldprogram than any other tutorial you've ever read :-)
Create a file (first.sh) as follows:
first.sh
#!/bin/sh
# This is a comment!
echo Hello World        # This is a comment, too!

The first line tells Unix that the file is to be executed by /bin/sh.This is the standard location of the Bourne shell on just about everyUnix system. If you're using GNU/Linux, /bin/sh is normally a symbolic link to bash.

The second line begins with a special symbol: #. This marksthe line as a comment, and it is ignored completely by the shell.
The only exception is when the very first line of the filestarts with #! - as ours does. This is a special directivewhich Unix treats specially. It means that even if you are using csh, ksh, or anything else as your interactive shell, that what followsshould be interpreted by the Bourne shell.
Similarly, a Perl script may start with the line #!/usr/bin/perl to tell your interactive shell that the program which follows should be executedby perl. For Bourne shell programming, we shall stick to #!/bin/sh.

The third line runs a command: echo, with two parameters, orarguments - the first is "Hello"; the second is "World".
Note that echo will automatically put a single space between its parameters.
The # symbol still marks a comment; the # and anythingfollowing it is ignored by the shell.

now run chmod 755 first.sh to make the text file executable,and run ./first.sh.
Your screen should then look like this:

$ chmod 755 first.sh
$ ./first.sh
Hello World
$

You will probably have expected that! You could even just run:

$ echo Hello World
Hello World
$

Now let's make a few changes.
First, note that echo puts ONE space between its parameters. Put a few spaces between "Hello" and "World". What do you expect the output to be? What about putting a TAB character between them?
As always with shell programming, try it and see.
The output is exactly the same! We are calling the echo programwith two arguments; it doesn't care any more than cp doesabout the gaps in between them.Now modify the code again:

#!/bin/sh
# This is a comment!
echo "Hello      World"       # This is a comment, too!

This time it works. You probably expected that, too, if you have experienceof other programming languages. But the key to understanding what is goingon with more complex command and shell script, is to understand and be ableto explain: WHY?
echo has now been called with just ONE argument - the string"Hello    World". It prints this out exactly.
The point to understand here is that the shell parses the arguments BEFORE passing themon to the program being called. In this case, it strips the quotes but passesthe string as one argument.
As a final example, type in the following script. Try to predict theoutcome before you run it:


first2.sh
#!/bin/sh
# This is a comment!
echo "Hello      World"       # This is a comment, too!
echo "Hello World"
echo "Hello * World"
echo Hello * World
echo Hello      World
echo "Hello" World
echo Hello "     " World
echo "Hello \"*\" World"
echo `hello` world
echo 'hello' world

Is everything as you expected? If not, don't worry! These are just someof the things we will be covering in this tutorial ... and yes, we willbe using more powerful commands than echo!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值