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

How to get the output of commands in shell script

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

Share

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

Most people do not understand the knowledge points of this article "how to get the output of the command in the shell script", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to get the output of commands in the shell script" article.

Usually, you need to get the output of the command in the shell script, and then judge the next step based on the output.

One of the more common ways is to match whether there are some keywords in the output of the command and choose different actions to perform.

One of the more commonly used methods is to use reverse single quotation marks-the variable name of the result is saved = `The linux command to be executed`

When this method is used, there are some details to pay attention to. Let's use a few examples to illustrate.

For example, in a CentOS7 environment, use the rpm-qa command to query whether some rpm packages are installed, and if not, install them.

To take a simple example:

#! / bin/bashcheck_results= `rpm-qa | grep "zlib" `echo "command (rpm-qa) results are: $check_results" if [[$check_results= ~ "zlib"]] then echo "package zlib has already installed." else echo "This is going to install package zlib" fi

Save as a test.sh file, and then run

$bash test.sh

The result is:

Command (rpm-qa) results are: zlib-1.2.7-13.el7.x86_64

Package zlib has already installed.

This script basically works.

So, we also use a similar way to check whether the iscsi-initiator package is installed. Unlike the previous command, the installation of this command is not available through the rpm-qa command.

Let's take a different approach.

#! / bin/bashcheck_results= `iscsiadm-- version | grep iscsiadm`echo "check command (iscsiadm) available results are: $check_results" if [[$check_results= ~ "iscsiadm"]] then echo "command iscsiadm could be used already." else echo "command iscsiadm can't be used. Install it" rpm-ivh iscsi-initiator-utils-6.2.0.873-29.el7.x86_64.rpmfi

The result of the execution is:

$bash test.sh

Check command (iscsiadm) available results are: iscsiadm version 6.2.0.873-28

Command iscsiadm could be used already.

At this point, it looks like the script is working properly, indicating that iscsiadm is available. What if the iscsiadm command is not available at first?

We can change the above check_results= `iscsiadm-- version | grep iscsiadm` to check_results= `iscsiadmm-- version | grep iscsiadm`

In this way, we deliberately miswrite the command to simulate the operation of the script without the command installed.

After the repair, the execution results are as follows

$bash test.sh

Test.sh: line 2: iscsiadmm: command not found

Check command (iscsiadm) available results are:

Command iscsiadm can't be used. Install it

It looks like it works, but why is the content of check_results empty? Shouldn't the content of "command not found: iscsiadmm" be assigned to check_results and printed?

We execute iscsiadmm-- version | grep iscsiadm on the command line alone, and there is also output, so why can't the assignment be successful?

At this time, if we execute the command echo$? after the execution of the command The value obtained at this time is 127 (Centos7 system). After executing the scsiadm-- version | grep iscsiadm command alone on the command line, execute echo $? The resulting value is 0 (Centos7 system)

From this you can see that when using (2 reverse single quotes) to get the execution result, you need to ensure that the commands within the single quotation marks can be executed successfully.

Even so, does it guarantee that we can get what we want? Not necessarily. Let's look at another example.

#! / bin/bashcheck_results= `java-version `echo "check java version results are: $check_results" if [[$check_results= ~ "1.8."]] then echo "java version is 1.8, it seems not need to install java again." else echo "It is going to install jdk 1.8 version" fi

The execution result is:

$bash test.sh

Java version "1.8.073"

Java (TM) SE Runtime Environment (build 1.8.0_73-b02)

Java HotSpot (TM) 64-Bit Server VM (build 25.73-b02, mixed mode)

Check java version results are:

It is going to install jdk 1.8 version

What's going on? The command jdk has been installed, why is it still not matched? According to the second example, we manually execute the command java-version and then execute the command $? The result is 0, indicating that the command was executed successfully. How come you still haven't got the output of the command?

The reason for this phenomenon is that it is possible that the execution result of the command has been redirected.

Try changing check_results= `java-version` to check_results= `java-version 1 > / dev/ stdout` and check_results= `java-version 2 > / dev/ sdtout` to see if the output has changed.

To prevent this from happening, we finally changed the above to check_results= `java-version 2 > & 1` to get the desired result.

When you use the variable name of the saved result = `the linux command to be executed'to obtain the output of the command, the situation to be noted is summarized as follows:

1) ensure that the command in anti-single quotation marks is executed successfully, that is, $? The output of must be 0, otherwise the output of the command cannot be obtained

2) even if the return value of the command is 0, you need to ensure that the result is output through standard output, not standard error output, otherwise you need to redirect

Therefore, we recommend that you use the variable name of the saved result = `The linux command 2 > & 1` to be executed to obtain the execution result of the command.

Interested friends can try the result of changing the second example to check_results= `iscsiadmm-- version 2 > & 1`.

In addition, there is a way to get the return value of command execution. The variable name = $(the command to be executed) is not tested for this method, so it will not be discussed here.

For the above-mentioned situation of getting the output of the command execution, which is different from the way to get the result of the function execution, please pay attention to it in use.

The above is about the content of this article on "how to get the output of commands in shell script". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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

Development

Wechat

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

12
Report