Hello,World!你的第一个程序 - 十五天学会Bash编程

前言:

  一个 Shell脚本就是包含一条或多条命令的可执行文件。按照传统,今天我们的任务就是新建一个Hello,World!脚本文件。

  在本文中,你将学会如何创建并运行一个脚本文件。

  我们大部分的工作都会在控制台(Terminal)下进行,因为它比较简洁,并且可以实时显示运行结果。

  例如:

输出当前工作目录:

dengdefei@ubuntu:~$ pwd
/home/dengdefei/test

输出用户根目录,“$HOME”是系统内嵌变量,存储的是登陆用户的根目录地址:

dengdefei@ubuntu:~$ echo $HOME
/home/dengdefei

环境:

  OS: Ubuntu 12.04 64Bit

 

命令:

  我觉得演示什么是命令,没有比这个更好的了:

dengdefei@ubuntu:~$ echo Hello, World!
Hello, World!

  这里有三个单词(词:一个词是指一个连续的字符组合而成的单一字符串。),第一个“echo”,是Bash的内嵌命令,用于打印其后面跟进的参数,规则是按空格分隔,并在打印结束后换行。所以这里就是将“Hello,”和“World!”打印出来了。

 

 

脚本:

脚本名称:

  首先得满足最基本的条件:命名不得有冲突。这不仅是指脚本的文件名,在特定情况下,它还不能和其它命令名称冲突,如果你不清楚这个命令是否是已经存在的内置命令,可以用“type”命令测试,比如我们以"echo"为例:

dengdefei@ubuntu:~/test$ type echo
echo is a shell builtin
dengdefei@ubuntu:~/test$ type -a echo
echo is a shell builtin
echo is /bin/echo

echo是系统已经存的的脚本命令,它的位置在/bin目录下,所以我们应该尽量避开。

 

脚本文件: 

创建脚本文件,最简单最直接的方法就是用Bash内嵌命令来做,比如下面这样:

dengdefei@ubuntu:~$ echo echo Hello, World! > hw
dengdefei@ubuntu:~$ ls
Desktop    Downloads         hw   Pictures  Templates  Videos
Documents  examples.desktop  Music  Public

上面做的事情是用“echo”命令输出“echo Hello, World!”这三个词,然后用“>”命令输入到“hw”文件中。“>”命令是Bash自带的IO输出命令,在下一篇文章我会详细讲解。

ls”命令是“list directory contents”的简写,意思就是列出当前目录中的内容,我们可以看到显示出的结果中已经有“hw”这个文件,下面就可以运行“hw”查看结果了:

dengdefei@ubuntu:~$ bash hw
Hello, World!

可以看到,我们已经成功运行“hw”脚本,并打印出“Hello, World!”。

要执行一个Script的方式有很多种。 
*************************
第一种 : 将"hw"这个档案的权限设定为可执行。 
dengdefei@ubuntu:~$ chmod 755 hw
执行 
dengdefei@ubuntu:~$ ./hw
Hello, World!
*************************
第二种 : 使用bash内建指令"source"或"."。 
dengdefei@ubuntu:~$ source hw
Hello, World!
或 
dengdefei@ubuntu:~$ . hw
Hello, World!
*************************
第三种 : 直接使用sh/bash/tcsh指令来执行。 
dengdefei@ubuntu:~$ sh hw
Hello, World!
或 
dengdefei@ubuntu:~$ bash hw
Hello, World!

 

环境变量与更改文件权限:

当我们有一系列的脚本,并且经常用到,此时我们不可能每次都要加“bash”命令来运行吧?那出太麻烦了,所以我们可以把这个脚本加入到系统内嵌命令中去,操作如下:

  1. 新建一个存放脚本的目录,比如叫“bin”:
    dengdefei@ubuntu:~$ mkdir bin
  2. 你已经将“bin”目录创建好了,可以用“ls”命令查看。现在我们需要将“bin”目录加到环境变量目录中去:
    dengdefei@ubuntu:~$ echo $PATH
    /home/dengdefei/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
    dengdefei@ubuntu:~$ PATH=$PATH:$HOME/bin

    现在已经把“bin”目录添加到环境变量目录中去了。注意“$PATH”是系统内嵌变量,存储的是系统的环境变量,“PATH=$PATH:$HOME/bin”的意思就是,在原PATH后添加我们的“bin”目录。注意:在Terminal中更改的PATH,只在当前这个Terminal的Session中有效,当你把Terminal关闭后,就需要重新设置。如果要永久更改的话,需要修改配置文件,在以后我会说到,急性子的可以自己去Google一下。

  3. 此时我们需要将“hw”移动到“bin”目录去:
    dengdefei@ubuntu:~$ mv hw bin/
  4. 为“hw”脚本添加可执行权限:
    dengdefei@ubuntu:~$ chmod 755 bin/hw

好了现在你可以在控制参任何地方执行“hw”文件了,不知道怎样执行?OMG!如下:

dengdefei@ubuntu:~$ hw
Hello, World!

怎样?感觉和系统内嵌命令使用是不是一样的了?

 

进阶:

现在我们将“hw”改造一下,请在Terminal中执行如下代码打开“hw”脚本:

dengdefei@ubuntu:~$ gedit ~/bin/hw

“gedit”是Ubuntu自带的文本编辑器,你可以随意选择,不一定限制于这个编辑器。然后我们将“hw”脚本里面的代码替换成以下的内容:

#!/bin/bash
#:Filename    :hw
#:Date         :2013-02-18
#:Author    :DengDefei
#:Description    :Say hello to somebody.
printf "Hello, %s!\n" "$1"

保存并退出,运行:

dengdefei@ubuntu:~$ hw Defei
Hello, Defei!

是的,没看错,它把你输入的“Defei”给打印出来了,当然,你可以输入自己的名字试试 :)  

大家应该会注意到第一行的"#!/bin/sh"。在UNIX下,所有的可执行Script,不管是那一种语言,其开头都是"#!",例如Perl 是"#!/usr/bin/perl",tcl/tk是"#!/usr/bin/wish",看您要执行的Script程式位置在那里。您也可以用"#! /bin/bash"、"#!/bin/tcsh"等等,来指定使用特定的Shell。以#号开头的行为注释行,不会被当作脚本执行。

 

 

好了,今天就讲以这里,回见。

 

 

Summary - 总结:

Commands - 命令:

Commands - 命令 Explains - 解释
pwd Prints the name of the current working directory - 打印当前工作目录的地址
cd Changes the shell’s working directory - 更改工作目录。
echo Prints arguments separated by a space and terminated by a newline - 按空格分隔打印参数,并在最后换行。
type Displays information about a command - 显示指定命令的信息。
mkdir Creates a new directory - 新建文件夹。
ls list directory contents - 列出当前目录中的内容。
mv move (rename) files - 移动并重命名文件。
chmod Modifies the permissions of a file - 更改文件权限。
source a.k.a. . (dot): executes a script in the current shell environment - (可用'.'点号代替sorce命令)运行当前目录下的指定脚本。
printf Prints the arguments as specified by a format string - 按指定格式打印输出参数。

 

Concepts - 概念:

Concepts - 概念 Explains -解释
Script This is a file containing commands to be executed by the shell. - 脚本:这是一个包含可执行命令的文件。
Word A word is a sequence of characters considered to be a single unit by the shell. - 词:一个词是指一个连续的字符组合而成的单一字符串。
Output redirection You can send the output of a command to a file rather than the terminal using > FILENAME. - 输出重定向:你可以将你的输出流写入到一个指定的文件。
Variables These are entities that store values. - 变量:指用来存储数据的已命名对象。
Comments These consist of an unquoted word beginning with #. All remaining characters on that line constitute a comment and will be ignored.  - 注释:所有包含在“#”号中的语句都所于注释,不会被程序运行。
Shebang or hash-bang This is a hash and an exclamation mark (#!) followed by the path to the interpreter that should execute the file. - 工作指示符:在UNIX下,所有的可执行Script,不管是那一种语言,其开头都是"#!",例如Perl 是"#!/usr/bin/perl",tcl/tk是"#!/usr/bin/wish",看您要执行的Script程式位置在那里。您也可以用"#! /bin/bash"、"#!/bin/tcsh"等等,来指定使用特定的Shell。
Interpreter This is a program that reads a file and executes the statements it contains. It may be a shell or another language interpreter such as awk or python. - 解释器:从一个文件读取并运行命令语句的程序。

 

Variables - 变量:

Variables - 变量 Explains - 解释
PWD Contains the pathname of the shell’s current working directory. - 当前工作目录的地址
HOME Stores the pathname of the user’s home directory. - 用户根目录的地址
PATH Is a colon-separated list of directories in which command files are stored. The shell searches these directories for commands it is asked to execute. - 环境变量,是一个以冒号分隔的,用以存储命令文件的目录列表。

 

练习:

  编写一个脚本,实现以下功能:

    1.在主目录下创建一个“test”目录

    2.在此目录下创建两个子目录“sub1”,“sub2”。

    3.在test目录里创建一个如上面"hw"的脚本,并将其设置为可执行,并执行它。

 转载请注明出处:博客园 德飞 首发,大部分资源来自《Pro Bash Programming》一书。

posted @ 2013-03-15 16:45  德飞  阅读(1356)  评论(1编辑  收藏  举报