Exception analysis of shell script variable get program output result

Source: Internet
Author: User
#! /bin/bashpid=$ (pgrep Hello) echo ${pid}

The script name is hello_test.sh


Always returns two values when a hello process exists

When the hello process does not exist, the return value is not empty, always returns a value, runs multiple times, and the returned value is not fixed


Of course, none of the above is the desired result.

Why not exist, also return, and the return value is not the same, remember that there is an internal variable remember the last shell process PID,

That should be the case.


The person reminds, the script is also hello.sh, finally came to realize, the foot name is hello_test.sh, also contains Hello, that return value should be the current script PID

To finish the test, add in the original script

Echo $$

When Hello is not running, it outputs two lines of the same value, which is expected to be proved.


Find a small hidden dessert bar, hehe!

Exception analysis of shell script variable get program output result

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.