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

Set command instance usage of shell

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "the usage of set command instance of shell". In daily operation, I believe that many people have doubts about the usage of set command instance of shell. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "set command instance usage of shell". Next, please follow the editor to study!

Execute the set command without options

When the set command is executed without options, all variables of the current shell are output in the same format as the variable assignment in the shell script: name=value. Therefore, the output of the set command can be used directly as the input of a stdin.

Basic grammar

Set switches on and off different features of shell through options, each corresponding to an option. Each feature can be configured in two ways:

One is in the form of set-e and set + e, that is, to specify options directly.

The other is in the form of set-o errexit and set + o errexit, that is, the option name is specified through the o option.

I think you must be very curious about whether to use the + sign or the-sign. In the set command, the option is preceded by a-sign to turn it on, and + to turn it off.

Introduction to options-o

Executing set-o outputs the current set option configuration:

~ / programming/test$ set-oallexport offbraceexpand onemacs onerrexit offerrtrace offfunctrace offhashall onhistexpand onhistory onignoreeof offinteractive-comments onkeyword offmonitor onnoclobber offnoexec offnoglob offnolog offnotify offnounset offonecmd offphysical offpipefail offposix offprivileged offverbose Offvi offxtrace off+o

Executing set + o also outputs the configuration of the current set option, but in the form of a series of set commands. This form of output is generally used when rebuilding the current set configuration item.

~ / programming/test$ set + o

Set + o allexportset-o braceexpandset-o emacsset + o errexitset + o errtraceset + o functraceset-o hashallset-o histexpandset-o historyset + o ignoreeofset-o interactive-commentsset + o keywordset-o monitorset + o noclobberset + o noexecset + o noglobset + o notifyset + o nounsetset + o onecmdset + o physicalset + o pipefailset + o posixset + o privilegedset + o verboseset + o viset + o xtrace-e or-o errexit

When this option is set, shell exits immediately when a command fails.

-n or-o noexec

When this option is set, shell reads commands, but does not execute them. This option can be used to check shell scripts for syntax errors.

-u or-o unset

When this option is set, when shell wants to extend a variable that has not yet been set, shell must output information to stderr and then exit immediately. But interactive shell should not quit.

-x or-o xtrace

After this option is set, shell outputs trace to stderr after extending the command (parameter extension) and before executing the command for each command to be executed.

-o pipefail

This option affects the return value of the pipe. By default, the return value of a pipe is the return value of the last command, such as cmda | cmdb | cmdc. The return value is determined by the return value of the cmdc command. If the pipefail option is specified, the return value of the pipe is determined by the last failed command, meaning that a non-zero value is returned if a command fails. If all commands are successful, success is returned.

The example #! / bin/bashset-o xtraceset-o errexit # can comment out this to see how the execution effect is different. Echo "Before" ls filenoexists # ls does not exist in the file echo "After", on the "shell set command instance usage" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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