Writing Your First Script And Getting It To Work

Writing Your First Script And Getting It To Work

To successfully write a shell script, you have to do three things:

  1、Write a script  //写一个脚本

  2、Give the shell permission to execute it    //给shell执行权限
  3、Put it somewhere the shell can find it     //把它放在shell可以找到的地方(或者bash  xx.sh  这种格式执行)

Writing A Script

A shell script is a file that contains ASCII text. To create a shell script, you use a text editor. A text editor is a program, like a word processor, that reads and writes ASCII text files. There are many, many text editors available for your Linux system, both for the command line environment and the GUI environment. Here is a list of some common ones:

Name              Description //描述                  Interface    //接口

vi, vim           The granddaddy of Unix text editors,             command line  //命令行

            vi, is infamous for its difficult, non-intuitive command structure.

          On the bright side, vi is powerful, lightweight, and fast.

           Learning vi is a Unix rite of passage,

         since it is universally available on Unix-like systems. On most Linux distributions,

         an enhanced version of the traditional vi editor called vim is used.

Emacs    The true giant in the world of text editors is Emacs by Richard Stallman.        command line

       Emacs contains (or can be made to contain) every feature ever conceived for a text editor.

        It should be noted that vi and Emacs fans fight bitter religious wars over which is better.

nano    nano is a free clone of the text editor supplied with the pine email program.       command line

      nano is very easy to use but is very short on features.

       I recommend nano for first-time users who need a command line editor.

gedit     gedit is the editor supplied with the Gnome desktop environment.        graphical //图形

kwrite    kwrite is the "advanced editor" supplied with KDE. It has syntax highlighting,    graphical

              a helpful feature for programmers and script writers.

Now, fire up your text editor and type in your first script as follows:

#!/bin/bash
# My first script

echo "Hello World!"

The clever among you will have figured out how to copy and paste the text into your text editor ;-)

If you have ever opened a book on programming, you would immediately recognize this as the traditional "Hello World" program. Save your file with some descriptive name. How about hello_world?

The first line of the script is important. This is a special clue, called a shebang, given to the shell indicating what program is used to interpret the script. In this case, it is /bin/bash.

//脚本的第一行很重要,这是一个特殊的线索,称为shebang,给了shell指示用于解释脚本的程序,在这种情况下它是#!/bin/bash

Other scripting languages such as Perl, awk, tcl, Tk, and python also use this mechanism.

The second line is a comment. Everything that appears after a "#" symbol is ignored by bash.

              译:“#”符号后出现的所有内容都被bash忽略

As your scripts become bigger and more complicated, comments become vital. They are used by programmers to explain what is going on so that others can figure it out. The last line is the echo command. This command simply prints its arguments on the display.

Setting Permissions    //设置权限

The next thing we have to do is give the shell permission to execute your script. This is done with the chmod command as follows:

[[email protected] me]$ chmod 755 hello_world

The "755" will give you read, write, and execute permission. Everybody else will get only read and execute permission. If you want your script to be private (i.e., only you can read and execute), use "700" instead.

                                                  译://如果希望你的脚本是私有的(即只有你可以读写执行),请改成700权限

Putting It In Your Path

At this point, your script will run. Try this:

[[email protected] me]$ ./hello_world

You should see "Hello World!" displayed. If you do not, see what directory you really saved your script in, go there and try again.

Before we go any further, I have to stop and talk a while about paths. When you type in the name of a command, the system does not search the entire computer to find where the program is located. That would take a long time. You have noticed that you don‘t usually have to specify a complete path name to the program you want to run, the shell just seems to know.

Well, you are right. The shell does know. Here‘s how: the shell maintains a list of directories where executable files (programs) are kept, and only searches the directories in that list. If it does not find the program after searching each directory in the list, it will issue the famous command not found error message.

This list of directories is called your path. You can view the list of directories with the following command:

[[email protected] me]$ echo $PATH

This will return a colon separated list of directories that will be searched if a specific path name is not given when a command is attempted. In our first attempt to execute your new script, we specified a pathname ("./") to the file.

You can add directories to your path with the following command, where directory is the name of the directory you want to add:

//你可以使用一下命令将目录添加到路径,其中directory是要添加的目录的名称:

[[email protected] me]$ export PATH=$PATH:directory

A better way would be to edit your .bash_profile or .profile file (depending on your distribution) to include the above command. That way, it would be done automatically every time you log in.

Most Linux distributions encourage a practice in which each user has a specific directory for the programs he/she personally uses. This directory is called bin and is a subdirectory of your home directory. If you do not already have one, create it with the following command:

[[email protected] me]$ mkdir bin

Move your script into your new bin directory and you‘re all set. Now you just have to type:

//将您的脚本移动到你的新bin目录下,并将其全部植入,现在你只需输入:

[[email protected] me]$ hello_world

and your script will run. On some distributions, most notably Ubuntu, you will need to open a new terminal session before your newly created bin directory will be recognised.

//你的脚本会运行。在某些发行版中,尤其是ubuntu,您将需要在新创建的bin目录被识别之前打开一个新的终端会话

时间: 2024-11-05 13:49:15

Writing Your First Script And Getting It To Work的相关文章

[转载]Python 包管理工具解惑

原文链接:http://zengrong.net/post/2169.htm Python 包管理工具解惑 python packaging 一.困惑 作为一个 Python 初学者,我在包管理上感到相当疑惑(嗯,是困惑).主要表现在下面几个方面: 这几个包管理工具有什么不同? distutils setuptools distribute disutils2 distlib pip 什么时候该用pip,什么时候该用 setup.py ,它们有关系么? easy_install.ez_setup

考试题目

Q1. An administrator wants to provide users restricted access. The users should only be able to perform the followingtasks:- Create and consolidate virtual machine snapshots- Add/Remove virtual disks- Snapshot ManagementWhich default role in vCenter

Dive into python3

Table of Contents What’s New in “Dive Into Python 3” a.k.a. “the minus level” Installing Python Diving In Which Python Is Right For You? Installing on Microsoft Windows Installing on Mac OS X Installing on Ubuntu Linux Installing on Other Platforms U

js事件——Events

没有共产党就没有新中国,没有 events 就没有 scripts.每个包含 JavaScript 的页面中: 几乎所有的脚本都是由 event 触发的.原因很简单. JavaScript 可以增加交互性: user 做一些操作,页面给出响应.Events 是所有 JavaScript 应用的小心脏.本文介绍了事件处理机制,相关问题以及如何写出跨浏览器的 scripts.同时也展示了几个带有事件处理细节的页面. 因此JavaScript 检测用户的操作,以便知道何时做出反应,反应时执行哪个 fu

[email protected]一个有效的配置管理工具--Ansible configure management--翻译(十)

未经书面许可,.请勿转载 Custom Modules Until now we have been working solely with the tools provided to us by Ansible. This does afford us a lot of power, and make many things possible. However, if you have something particularly complex or if you find yourself

Performance Tuning of Spring/Hibernate Applications---reference

http://java.dzone.com/articles/performance-tuning For most typical Spring/Hibernate enterprise applications, the application performance depends almost entirely on the performance of it's persistence layer. This post will go over how to confirm that

Vim tips——Working with external commands

A common sequence of events when editing files is to make a change and then need to test by executing the file you edited in a shell. If you're using vim, you could suspend your session (ctrl-Z), and then run the command in your shell. That's a lot o

shell中如何判断某一命令是否存在

参考: http://www.cnblogs.com/tuzkee/p/3755230.html https://segmentfault.com/q/1010000000156870 http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script 避免使用which,可用下列命令实现: $ command -v foo >/dev/null 2>&1 || { echo &g

zabbix 监控Nginx

一.开启nginx_status页面 server {  \\定义一个虚拟主机     listen 80 default_server;  \\定义监听地址     server_name _;   \\定义主机名     location = /ngx_status {         stub_status on;   \\开启status         access_log off;    \\关闭访问日志         allow 127.0.0.1;   \做访问权限控制