Linux (8) ---- shell programming

Source: Internet
Author: User
Related links: linux that thing (1) http://www.2cto.com/ OS /201205/131407.html?linuxthat thing (2) http://www.2cto.com/ OS /201205/131408.html?linuxthat thing (3) http://www.2cto.com/ OS /201205/13140
Related links: linux (1) http://www.2cto.com/os/201205/131407.html ; Linux (2) http://www.2cto.com/os/201205/131408.html Linux (3) http://www.2cto.com/os/201205/131409.html Linux (4) ---- detailed description of user management http://www.2cto.com/os/201205/131410.html Linux (5) ---- common commands for user management http://www.2cto.com/os/201205/131411.html Linux-Process Management http://www.2cto.com/os/201205/132474.html Linux-file system management http://www.2cto.com/os/201205/133789.html Let's look at a shell program directly under a simple shell program. -----------------#! /Bin/sh # This is to show what a example looks like. echo "Our first example" echo # This inserts an empty line in outputecho "We are currently in the following directory. "/bin/pwdechoecho" This directory contains the following files "/bin/ls www.2cto.com shell structure :#! /Bin/sh indicates that the following script will be interpreted and executed using the Bourne shell interpreter in the system. #! Is a special identifier, followed by
This explains the shell path of the script. # Comment the content of the line. The echo output statement is not executed, which is equivalent to printing the output/bin/pwd in programming and displaying the current path pwd, add/bin/above to indicate the absolute path of the Command/bin/ls to display the content in the current directory. this is not explained. it should be the first thing to know about linux. Run the following command: [root @ bogon bin] # sh exple. shOut first example --- print the content we are currently in the following directory. --- print content/bin --- display the current path www.2cto.com this directory contains the following files ----- print content alsaunmute date gettext mail red touch --- display the file arch dd grep mailx rm tracepathash df gtar in the current directory mkdir rmdir tracepath6ash. static dmesg gunzip mknod rpm tracerouteaumix-minimal dnsdomainname gzip mktemp rvi t Raceroute6awk doexec hostname more rview truebasename domainname igawk mount sed exple. sh is simple. this is the simplest shell programming, just integrating some of the commands I usually use.
I don't know if you are familiar with the batch processing in DOS. Similar to them, they all put a group of commands for our smooth operation together for execution.
Shell is an explanatory language. In fact, we will find that such programming is highly dependent on our system. For example:/bin/pwd to execute the pwd command,
If pwd is not stored in the bin/directory on your system, an error may occur when the program runs here.
A more formal explanation: Shell is basically a command interpreter, similar to command.com in DOS. It receives user commands (such as ls ),
Then, call the corresponding application. The common shells include standard Bourne shell (sh) and C shell (csh ).
In interactive shell and non-interactive shell interactive mode, shell waits for your input and runs the commands you submit. This mode is called Interactive
Because shell interacts with users. This mode is also very familiar to most users: logon, command execution, and logout.
When you sign back, shell is terminated. Shell can also run in another mode: non-interactive mode. In this mode, shell does not interact with you,
Instead, read the commands stored in the file and execute them. When it reads the end of the file, shell is terminated.
Step 1: Create a file containing the command and control structure. Step 2: modify the permission of the file so that it can be executed. Of course, if a user wants to execute a file, the user must have the permission to execute the file. Use the "chmod u + x" command to grant the user the execution permission Step 3: execute. /example can also run the "sh example" command to execute the shell variable www.2cto.com in the current directory. Do we have a preliminary understanding of shell? is it just a simple command set? No! As a language,
Of course, he also has his own syntax. If you know any language as follows, let's look at the differences between shell and the language you know. Variable: a method used by shell to transmit data. it represents the symbolic name of each value. Shell has two types of variables: temporary variables and permanent Variables. temporary variables are defined within the shell program. The scope of use is limited to programs that define them and are invisible to other programs. Including user-defined variables and location variables. User-defined variable format requirements: it must start with a letter or underline and may consist of letters, numbers, or underline sequences.
Variable name length is not limited. When using a variable, you need to add the prefix "$" before the variable name (I remember php loves this symbol ^_^). Generally, the variable name will use uppercase characters, for example: when MUM = 100 is defined, the value of TIME = $ (date) is assigned the execution result of A command to the variable. A = $ B copies A variable to another variable definition and views A variable: [root @ bogon ~] # Num= 100 [root @ bogon ~] # Echo $ NUM100 [root @ bogon ~] # TIME = $ (date) [root @ bogon ~] # Echo $ TIME 5 October 11, June 1 22:57:28 CST 2012 [root @ bogon ~] # B = fnngj [root @ bogon ~] # Echo $ Bfnngj [root @ bogon ~] # A = $ B [root @ bogon ~] # Echo $ Afnngj the permanent variable is an environment variable, and its value does not disappear as the execution of the shell script ends. [Root @ bogon ~] # Echo $ LANGzh_CN.UTF-8 www.2cto.com [root @ bogon ~] # Echo $ PATH/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:
/Usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin these system configurations will certainly not disappear as my shell program ends. Let's take a look at the variables defined in our system: [root @ bogon ~] # Set | moreA = fnngjB = fnngjBASH =/bin/bashBASH_ARGC = () BASH_ARGV = () BASH_LINENO = () BASH_SOURCE = () BASH_VERSINFO = ([0] = "3" [1] = "00" [2] = "15" [3] = "1" [4] = "release" [5] = "i686-redhat-linux-gnu ") BASH_VERSION = '3. 00.15 (1)-release 'Colors =/etc/DIR_COLORS.xtermCOLUMNS = 80 ............. delete variable: [root @ bogon ~] # Unset A: execute the unset command to delete A variable. the read command is interesting, but it is also common. for example, when the program runs A certain sentence, it must be stopped to ask the user
Enter the content, and then proceed according to the content entered by the user. Read command: read data from the keyboard and assign values to variables such as: read USERNSME [root @ bogon shell] # touch read. sh [root @ bogon shell] # vi read. sh #! /Bin/shread first second thirdecho "the first parameter is $ first" echo "the second parameter is $ second" echo "the third parameter is $ third" www.2cto.com [root @ bogon shell] # sh read. sh100 200 300 --------------- the first parameter is 100the second parameter is 200the third parameter is 300 shell variable arithmetic operations are also frequently used, let's take a look at shell's simple addition, subtraction, multiplication, division. Expr command: perform arithmetic operations on integer variables, for example, [root @ localhost ~] # Expr 3 + 53 + 5 [root @ localhost ~] # Expr 3 + 58 [root @ localhost ~] # Expr 9-54 [root @ localhost ~] # Expr 9/51 [root @ localhost ~] # Expr 9 \ * 545 this division is division, so 9 Division 5 equals 1, directly ignoring the remaining 4. This multiplication is because the asterisk (*) has other meanings. for example, when you look for it, * represents an unknown character,
So to use it as a multiplication character, we need to add the escape symbol (\) to a shell instance. we have mentioned the task plan before. now we can write a meaningful shell program based on the task plan. There is a very junk apache server in a school. many students visit this server from,
Then, the server often fails, and the school has no money to add equipment. Now, let's write a shell program,
Let it check apache every two minutes. if the server crashes. Restart the instance.
(Of course, this does not solve this problem from the perspective of the current) how to determine whether an apache is started? [Root @ bogon bin] # pgrep httpd7942 7944 7946 7947 7948 if the server has crashed, the input pgrep httpd will have no input. Now that we know the method, we can start to write this program test. apache ----------------------------------- www.2cto.com #! /Bin/sh # "if .... else "usage # using this program to show your system's service. echo "now, the web services of this linux system will be detect... "echo # Detesct www serviceweb = '/usr/bin/pgrep httpd' if [" $ web "! = ""] Thenecho "the web sercice is running. "elseecho" the web sercice is running. "/etc/rc. d/init. d/httpd startfi ----------------------------------- In fact, the core content is to judge whether pgrep httpd is empty and execute the Restart command. ^_^! For Task plans, see Chapter 6 process management in linux. Meanings of the above parameters: http://www.2cto.com/os/201205/132474.html [Root @ bogon test] # crontab-e */2 20-22 ** 1-7 test. apache: wq! Save and exit. Then there is nothing to do with us. Execute the script as planned. Haha! Postscript: I have introduced little about shell programming, and I have not even introduced loop statements (if... else.
The last example is used. Shell programming can be a thick and thick book. However, it is not my current learning focus.
If you have time, I will add another shell. Www.2cto.com has been learning linux since the beginning of this year. In fact, many problems have been encountered during the course of study.
This "linux thing" only sorts out the main content I learned. I'm glad to know nothing about it.
I know a little about linux. However, I think it takes too long to learn this course. maybe I can write a draft in a month or two.
This year, I had a job change from the north to the south. now, my work is very busy and there are too many things in my life.
Therefore, the learning time is low. However, I will continue to write my work and study here. The following learning will be done in two aspects: oracle and performance testing. I have always been a database idiot and have little interest in databases. However, it is a shortcoming of my technological development, so,
I want to spend some time learning about this. Performance testing has always been my favorite aspect. Currently, no performance tests are available. However, I have performed performance in the previous job.
The next goal is to perform professional performance tests. Both loadrunner and jmeter have been used.
However, I will focus on the theoretical basis. the focus of performance testing is not on tools, but on a wide range of knowledge.
Some things are recorded here. Haha! Author wormhole
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.