(Linuxshell) Chapter 1-test the knife (on)

Source: Internet
Author: User
Tags echo command

(Linuxshell) Chapter 1-test the knife (on)

Source: (linux shell) Chapter 1-test tool (I)

From today on, let's take a look at the book "linux shell script strategy.

1.1 Introduction

A shell script is usually #! The starting text file is as follows:

#! /Bin/bash

There are two ways to execute shell scripts:

Sh script. sh or./script. sh

However, before executing the script, we need to grant the script execution permission. Otherwise, the script cannot be executed.

Chmod a + x script. sh

In the command line, if you need to execute multiple commands at the same time, you can use

Cmd1; cmd2

1.2 interrupted Printing

1.2.1 practice

Echo is a basic command used to interrupt printing.

By default, echo adds a line break after each call.

You only need to use text with double quotation marks and the echo command to print the text on the interrupt. Similarly, text that is not waiting for double quotation marks can also get the nursery rhyme output result:

The same task can be completed using single quotes:

These methods seem loose, but they have some special purposes and side effects. Let's look at the following command line:

When a command line throws an error, it cannot contain an exclamation point. Therefore, if you want to print an exclamation mark, do not put it in double quotation marks, or you can add a special Escape Character slash before it to transfer the exclamation mark:

When an echo with single quotes is used, Bash does not evaluate the variable (such as $ var) in single quotes, but only displays it as is.

Another printable command is printf. printf uses the same parameters as the printf function in C. For example:

No. The output printed by printf does not contain line breaks.

1.2.2 supplementary content:

Print the color output. Each color has a corresponding color code, such as: reset = 0, black = 30, Red = 31, Green = 32, yellow = 33, Blue = 34, Magenta = 35, cyan = 36, white = 37. to print the color text, enter the following command:

1.3 game variables and Environment Variables

1.3.1 practical drills

You can assign values to a variable in the following ways:

Var = value

Var is the name of the variable, and value is the value assigned to the variable. If the value does not contain any blank characters, it does not need to be referenced by quotation marks. to multiply the value, you must use single or double quotation marks. Note that var = value is different from var = value. It is a common error to rewrite var = value to var = value, but the former is a value assignment operation, and the latter is an equal operation. Add the $ prefix before the variable name to print the variable content.

Var = "value"

Echo $ var or echo $ {var}. You can see that the first command is incorrect, and the second three commands mistakenly print the variable content:

We can also reference the variable value in the double quotation marks of the printf or echo command:

The export command is used to set environment variables. After that, any program executed from the current shell script will inherit this variable. You can export specific variables in the executed application or shell script as needed. By default, many standard environment variables are available for shell. PATH is one of them.

$ Echo $ PATH

The value of $ PATH is usually in/etc/environment or/etc/profile or ~ /. Bashrc. To add a new PATH to the PATH, you can use:

Export PATH = $ PATH:/home/user/bin

1.3.2 supplementary content:

To obtain the length of a string, you can use the following method or:

Identify the current shell version:

Echo $ SHELL or echo $0

Modify the Bash prompt string

When we open a terminal or run a shell, we will see a prompt string similar to user @ hostname:/home/$. The prompts and colors in different versions of GNU/Linux are also slightly different. We can use the PS1 environment variable to customize the prompt text. The default shell prompt text is in the file ~ /. Bashrc. You can use the following command to list the rows that set PS1:

To set a prompt string, enter:

PS1 = ">", as shown in, the prompt has changed to ">" angle brackets, but such modifications only apply to the current terminal. If another terminal or computer restarts, the prompt before modification will be restored. You must modify the settings to take effect later ~ /. Bashrc value.

1.4 perform mathematical operations through shell

1.4.1 prerequisites

In the Bash shell environment, you can use let, (), and [] to perform basic arithmetic operations. The expr and bc tools are also useful for advanced operations.

1.4.2 practical drills

A value can be defined using a normal variable value assignment method. In this case, it is stored as a string. However, we can use some methods to make it process like a number, as shown in:

Of course, you can also perform auto-increment or auto-increment operations on variables:

Other arithmetic operations:

Bc is an advanced tool for mathematical operations. This precision calculator contains a large number of options. We can use it to perform floating-point operations and apply some advanced functions:

Other operations for setting bc parameters:

1.5 playback file descriptors and redirection

? 1.5.1 prerequisites

? We frequently use stdin, stdout, and stderr when writing scripts ). Redirecting output to a file through content filtering is one of our basic tasks. When the command outputs the text, the output text may be an error message or a normal (non-error) Output Message. By checking the output text itself, we cannot distinguish between normal output text and error text. However, we can solve this problem through file descriptors and extract the texts associated with specific descriptors.

? A file descriptor is an integer associated with an opened file or data stream. File descriptors 0, 1 and 2 are reserved by the system.

? ? 0 ----------- stdin (standard input)

? ? 1 ----------- stdout (standard output)

? ? 2 ----------- stderr (standard error)

? 1.5.2 practical drills

? ? You can use the following method to redirect or save the output text to an object:

? ? Echo "This is a sample text 1"> temp.txt

? ? The content in the queue is cleared first.

? ? Next, let's look at another example:

? ? Echo "This is a sample text 2"> temp.txt

? ? This method will append the text to the target file.

? ?> And> are different. Although both operators can redirect text to a file, the former clears the file before writing the content, and the latter appends the content to the end of the existing file.

? ?

? ? To see what a standard error is and how to redirect it. When the command outputs an error message, the stderr information is printed. Take a look at the following example:

? ?

Here, + is an invalid parameter, so an error message is returned. Note: When a command is returned with an error, it returns an exit status other than 0. When the command is successfully completed, it returns a number 0, the exit status can be changed from the special variable $? (Run echo $? immediately after the command is executed ?, You can print the exit status)

? ? Next, we will save the error information to a file instead of printing it on the terminal:

? ? Note that the command is ls + 2> out.txt. Error message output to out.txt

? ? We can also execute standard output and standard error messages to the same command:

? ? The error message and standard output are output to different files. You can also use the following command to convert stderr to stdout, so that both stderr and stdout are redirected to the same file:

? ? Cmd 2> & 1 output.txt or cmd &> output.txt. Sometimes, the output may contain unnecessary information. If you don't want the terminal to be filled with complicated topics about stderr, You can redirect stderr output to/dev/null to ensure that everything is cleared. Suppose we have three files: a1, a2, and a3. However, normal users do not have the "read-write-Execute" permission for file a1. To print the content of all files whose names start with a, run the cat command:

? ? As you can see, the a1 file does not have any permissions, but the content can still be read because I log on as the root user. If other users read a1, an error is returned, in this case, we can use the standard error output to capture the error output, and then output the content of other files:

? ?

? 1.5.3 Additional content? ? ?

? ? Reading input commands from stdin can receive data in multiple ways. In addition, we can use cat and pipelines to develop our own file descriptors, for example:

? ? Cat file | cmd

? ? Cmd1 | cmd2

? ? Redirect the file to the command:

? ? Cmd <file

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.