Shell script Application (i)

Source: Internet
Author: User
Tags stdin

1. Prepare my first shell script

What is a shell script? I would simply say that the shell script is a special program in the Linux system, he works between the operating system kernel and the application, acting as a "command interpreter" role, is responsible for receiving the user input operation instruction and explanation, will need to perform the operation passed to the kernel execution, and outputs the results.

Let's see what kind of shell script my own system supports.

[Email protected] ~]# Cat/etc/shells

/bin/sh

/bin/bash

/sbin/nologin

........ The bottom doesn't work.

Here,/bin/sh is the default shell script for the Linux version, and bash is called the Bourne Again Shell, which is the most popular open source project.

In fact, the shell script is to save the usual use of the various Linux commands to a text file in order to add execution permissions, this file can be called Shell script. Cases:

[email protected] script]# cat first.sh

#!/bin/bash

Cd/boot

Pwd

LS-LH vml*


[Email protected] script]# chmod +x first.sh

[Email protected] script]#./first.sh

/boot

-rwxr-xr-x. 1 root root 4.0M vmlinuz-2.6.32-431.el6.x86_64

Here I executed three commands, Cd/boot,pwd, LS-LH vml*, after executing this script found no, and the results of the three command output is the same, this can be said to be a ' batch processing ' automated process.

When we write a script to develop a good habit is to follow the standard script structure, so that the script can output friendly hints, easier to read, for more code, the organization complex script, should add the necessary comment text, following the rewritten script:

[email protected] script]# cat first.sh

#!/bin/bash

Cd/boot

echo "In the present directory:"//The current directory is located in

Pwd

echo "Begin with VML files including:"//Where the files starting with VML include

LS-LH vml*

[Email protected] script]#./first.sh

In the current directory:

/boot

Begin with VML files including:

-rwxr-xr-x. 1 root root 4.0M vmlinuz-2.6.32-431.el6.x86_64

is not very straightforward, haha!

There are other ways to execute directly without enforcing permissions, such as:

[[Email protected] script]# sh first.sh///bin/sh to explain the script

[Email protected] script]#. first.sh//load script with dot number

The Linux system includes a large number of shell script files, such as the various service control scripts under the/ETC/INIT.D directory these shell scripts can only be used for reference, as far as possible to avoid direct modification of system scripts, so as not to cause service or system failure

2. Redirection and pipeline operation

1). REDIRECT operation: The user processes information through the operating system, including the following types of interactive device files:

Standard input (STDIN): The default device is your keyboard, the file number is 0, and the command reads the input data required during execution from the standard input file.

standard Output (STDOUT): The default device is your monitor, the file number is 1, and the command sends the executed output to the standard output file.

standard Error (STDERR): The default device is your monitor, the file number is 2, and the command sends various error messages to the standard error file during execution.

Correlation is the standard input and output and standard errors by default, the keyboard and monitor are used as the associated devices, interacting with the operating system to achieve the most basic input and output operations. This sentence is understood to be the various command strings you input through the keyboard, auxiliary control information, the command output to the screen, command execution error, error messages will be fed back to the screen. In the actual Linux system maintenance, you can change the content direction of the input and output, instead of using the default label input (which refers to the keyboard and the monitor), this operation is called redirection.

REDIRECT output: Use the ">" or ">>" action symbols, respectively, to overwrite or append files, for example:

[[email protected] ~]# uname-p > Kernel.txt//output will be overwritten in this file

[email protected] ~]# cat Kernel.txt

x86_64//CPU Type Information

[[email protected] ~]# uname-r >> kernel.txt//output will be appended instead of overwritten

[email protected] ~]# cat Kernel.txt

x86_64

2.6.32-431.el6.x86_64//CPU Kernel version information

REDIRECT input: The path to receive input from the command is changed from the default keyboard to the specified file, for example:

[[email protected] ~]# vim pass.txt//Add the initial password to "123456" in the text

123456

: Wq

[[email protected] ~]# passwd--stdin root < pass.txt//take password from Pass.txt file

Changing password for user root.

Passwd:all authentication tokens updated successfully. It worked

Error redirection: An error message that occurs during the execution of a command (option over-parameter error) is saved to the resulting file instead of being displayed directly on the screen, and the error redirect uses the ' 2> ' operator, where ' 2 ' refers to the number of the error file (omitting the 1,0 number when using standard input output redirection), In practice, error redirection can be used to collect error messages executed by the program, to provide a basis for troubleshooting, and for shell scripts to redirect insignificant error information to an empty file to keep the output script tidy, for example:

[Email protected] ~]# tar jcf/nonedir/etc.tgz/etc/2> error.log

[email protected] ~]# cat Error.log

Tar:removing leading '/' from member names

Tar (child):/nonedir/etc.tgz:cannot open:no such file or directory

Tar (child): Error isn't recoverable:exiting now

When using the ' 2> ' operator, the contents of the target file are overwritten like the ' > ' operator, and the ' 2>> ' operator can be used to append content

, sometimes you need to compile the Apache packages, and you can direct the make, do install operation information to the empty/dev/null. This information is omitted.

3. Pipeline operation:

Pipeline operations provide a mechanism for working together between different commands, located in the pipeline "|" The command output on the left, as input to the right command (processing object), can use multiple pipelines for the same line of commands, and in shell scripting applications, pipeline operations are often used to filter the critical information needed. Cases:

[[email protected] ~]# grep "/bin/bash$"/etc/passwd

Root:x:0:0:root:/root:/bin/bash

Oracle:x:500:500::/home/oracle:/bin/bash

Zhangsan:x:501:502::/home/zhangsan:/bin/bash

Lisi:x:502:503::/home/lisi:/bin/bash

Wangmazi:x:503:504::/home/wangmazi:/bin/bash

[[email protected] ~]# grep "/bin/bash$"/etc/passwd | Awk-f: ' {print $1,$7} '

Root/bin/bash

Oracle/bin/bash

Zhangsan/bin/bash

Lisi/bin/bash

Wangmazi/bin/bash

The purpose of the awk command in the above example is to separate the first and seventh regions with the colon ":", where the "-F" section is used to specify the delimiter, the default is a space when unspecified, and for his usage please check more information, this use a lot of, for example, the following example filter is/ What is the usage of the catalog:

[Email protected] ~]# DF-HT

Filesystem Type Size used Avail use% mounted on

/dev/mapper/vg_rhel1-lv_root ext4 45G 15G 29G 34%/

Tmpfs tmpfs 936M 72K 936M 1%/DEV/SHM

/DEV/SDA1 ext4 485M 39M 421M 9%/boot

[[email protected] ~]# df-ht |grep "/$" |awk ' {print $6} '

34%

Today first write this, the shell variable how to write to you tomorrow, this thing still has a lot of, please see me update, we start from the most basic!!

This article is from the "one dish can no longer dish of the Little rookie" blog, please be sure to keep this source http://liliming.blog.51cto.com/10925034/1789354

Shell script Application (i)

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.