Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell exit status code, IFS environment variable, tee command, cat & gt; file & lt;< EOF usage

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Exit status code

Each command run in shell uses an exit status code (exit status). The exit status code is an integer of 0,255, and a command is passed to shell when the command ends.

View exit status code

Linux provides a special variable $? To save the exit status code of the last executed command. For commands that need to be checked, you must view or use $? immediately after they have finished running. Variable. Its value becomes the exit status code for the last command executed by shell.

The exit status code for a successfully terminated command is 0. There are no criteria for Linux error exit status codes, but there are some references available:

Status code description 0 command successfully ended 1 general unknown error 2 unsuitable shell instruction 127. command 128 invalid exit parameter 128 invalid exit parameter 128 critical error related to Linux signal x 130 exit status code outside the normal range of commands terminated by Ctrl+C

Examples are as follows:

An invalid command returns an exit status code 127. Exit status code 126, indicating that the user does not have the correct permission to execute the command. Invalid parameters are provided to the command, resulting in a general exit status code of 1, indicating that an unknown error has occurred in the command. Exit command

By default, the shell script exits with the exit status code of the last command in the script.

Use the exit command to change this behavior, allowing you to specify an exit status code at the end of the script.

The exit status code is an integer of 0255. if the parameter is too large, it will be modeled by the system.

IFS field delimiter

The environment variable IFS is called the internal field separator (internal field separator). The IFS environment variable defines some of the column characters that bash shell uses as field delimiters. By default are spaces, tabs, and newline characters. You can change this environment variable.

Because they are all special characters, the direct output cannot be seen:

$echo "$IFS" $echo "$IFS" | od-b0000000040011012 0120000004 $

This is converted to octal. "040" is a space, "011" is a tab, and "012" is a newline character. The last 012 is because echo breaks lines by default.

You can also try the-c option, which will print out\ t and\ nbut the space is still a space, so it's not obvious.

Od command

The default display mode of the od command system is octal, with the name derived from Octal Dump. There is also a hexdump command that outputs in hexadecimal. There should be some functional differences, and it should be about the same, and replacing the od command with the hexdump command in this case should have the same effect.

Hexdump has a-C (uppercase) option that od does not have. It has no effect here and works well when viewing files.

Only newline characters are recognized:

IFS=$'\ n' temporarily change IFS

A referenced security practice is to save the original value before changing the IFS and then restore it:

IFS_old=$IFSIFS=$'\ n'IFS=$IFS_old

This ensures that the default value of IFS is used in subsequent operations of the script. It is recommended to do so at any time.

Specify multiple IFS characters

To specify multiple IFS characters, simply string them on the assignment line:

IFS=$'\ nFleximeter; "

Line breaks, colons, semicolons, and double quotes are used as field delimiters.

Tee sends the output to both the screen and the file

The tee command is equivalent to a T-joint for a pipe. It sends data from STDIN to two places at the same time. One is STDOUT, and the other is the file name specified by the tee command line.

By default, the file is overwritten each time:

$date | tee test1Fri Dec 6 16:32:59 CST 2019$ cat test1Fri Dec 6 16:32:59 CST 2019$ date | tee test1Fri Dec 6 16:33:10 CST 2019$ cat test1Fri Dec 6 16:33:10 CST 2019 $

If you want to append data, use the-an option:

$cat test1Fri Dec 6 16:33:10 CST 2019$ date | tee-a test1Fri Dec 6 16:34:00 CST 2019$ cat test1Fri Dec 6 16:33:10 CST 2019Fri Dec 6 16:34:00 CST 2019 $

Using this method, the data can not only be saved in the file, but also displayed on the screen. This allows you to permanently save a copy of the output while displaying the output for the user.

Cat > file testline 1, > Line 2; > LINE 3. > EOF$ cat testline 1

Output redirection writes the output of the cat command to a file (you can also append it with > >). The input is no longer taken from standard input, but is redirected to something after the command line (or script). The EOF symbol marks the end of the data appended to the file.

Using this command, it is convenient to create a file in a script and write multiple lines of content

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report