Shell Script Learning Guide [i] (Arnold Robbins & Nelson h.f Beebe) _linux Shell

Source: Internet
Author: User
Tags chmod

The first chapter skips, the bottom starts from the second chapter, everybody understands.
PS: Here is a very important thing when my egg hurts, very! Already code article code to the fourth chapter, the tragedy of the wrong browser button to refresh, how can not find back before writing things. Want to Die! Forget it, review the side. The following is a summary of their own writing, there are errors in the wrong place is not necessarily the book (of course, there may be errors in the book, but I will personally operate the drip), may be I write wrong, please correct me.

Chapter II Introduction

Because the second time write, it is simple and quick to write, try to write clearly. We all know the terminal command CD Ah, chmod, who and the like. Together with some control statements, variables and so on the shell programming, give a simple example:

Copy Code code as follows:

$ cat > Nusers
#! /bin/sh
who | Wc-l
^d
$ chmod +x nusers
$./nusers

The top is the Nusers file is a complete shell script, the function is to query how many users login system. Cat >nusers Redirect to nusers file input data stream, #! #号之后都是注释, but #!. is read by the interpreter because it tells the system what interpreter to use, and this is/bin/sh. ^d is to press Ctrl+d to end the input, chmod add executable permissions, and finally run the script that you wrote. This is a complete process of writing to run the usage. Are you getting started with wood? (Amount ~ If there is wood, then look for another article)

Now let's improve, in order for us to execute our own programs more like the system built-in commands, we can modify the system environment variable path, the modified windows of children's shoes know, Linux is the same, is to allow the system to search the command when there is a search path basis. You can output a look at first: Echo $PATH. The result is to output a string of strings that are delimited, and we give him a new assignment, assuming that the shell code we write exists in the Myshell folder in the user's directory, so we assign the value: Path= $PATH: $HOME /myshell so we can directly run the nusers command to get the number of user login information.

Here to learn two commands, sorry you think wrong, not who and WC, but from the most basic output Hello,world start, so first learn echo and printf, the former is very simple, will output all parameters, such as: Echo Hello,world. C-language friends can be happy, because you do not have to study printf, the opportunity is exactly the same, such as: printf "hello,%s\n", the world, which will output hello,world and then change lines (there will be no C child shoes?). )。

In addition to the previous appearance of the > | < (not expression--!) (This is), this is the basic I/O redirection, using the Windows cmd command and know that you can skip. > Output redirection. < input redirection. | Pipe, the output information before the symbol is used as the input information after the symbol. There is also >> <<, and the single difference is that a single will overwrite the original file, not created. The double is not overwritten, only added behind the existing file.

To practice this redirection, learn a TR command that is primarily used to convert deleted condensed characters.
Syntax: TR [options] source-char-list replace-char-list
Option:-C to take the source antisense, that is, the character tr is converting is not listed in source, usually-d-s with
-D deletes the characters listed in the Source-char-list, rather than the conversion, from the standard input.
-S condensed duplicate characters.
Easy to work with redirect exercises. See how it's concentrated and how it's replaced.

Here are two special files:/dev/null and/dev/tty, the first data sent to this file will be discarded by the system, similar to garbage cans. Reading data from it can only be read to NULL. Another/dev/tty, when the program opens this file, Linux will automatically redirect it to a terminal (console or serial port or a network and Windows login pseudo terminal, etc.) and then combined with the program. This is especially useful when the program must read human input, or it can be used to generate error messages. Cases:

Copy Code code as follows:

printf "Enter New password:" #提示输入
Stty-echo #关闭自动打印输入字符功能
Read Pass </dev/tty #读取密码
Printf:enter again: "#再次提示输入
Read Pass2 </dev/tty #再次读取
Stty Echo #打开自动打印输入字符功能

The Stty (set TTY) command controls various settings for a terminal (or window).

Now can write their own shell script, can also be called as the system built commands, but also the disadvantages of what, yes, is passing parameters, to their own script to pass parameters, so that a richer function to achieve.

Parameter acquisition in the script is very simple, the first argument is $, the second $, but more than 9 of the numbers are enclosed in curly braces, such as ${10}. Below, write a complete script program:

Copy Code code as follows:

$ cat > Finduser
#! /bin/sh
#finduser---To see if the specified user is logged on
who | grep $
^d
$ chmod +x Finduser
$ finduser Root

The premise is that you should also have the environment variable path. (Don't know how to change, please look forward)

We have been able to write some scripts, but it is inevitable that errors can not be expected, we need to be like C + + as debug, you can directly in the command line to perform sh-x finduser to open the execution tracking function.
You can also use the Set-x command in the script to open the function that performs the trace, and then use the set +x to turn off the feature.

Individual original, reprint please specify: Sanjiang Ferry

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.