Some self-written Linux Shell functions

Source: Internet
Author: User
Tags egrep

1. Get user input
.
The parameter is the regular expression that is correctly checked by the input prompt, and the default value is entered.
Return
The correct user input is returned.

Function get_input ()
{
Prompt = $1
RegEx = $2

Default = $3

If ["x $ {default}" = "X"]; then

Echo "$ {prompt}" 1> & 2
Read Input

Chk_result = 'echo "$ {input}" | egrep "^ $ {RegEx} $ "'
While [
"X $ {chk_result}" = "X"]; do
Echo "format error. Please
Input again: "1> & 2
Read Input

Chk_result = 'echo "$ {input}" | egrep "^ $ {RegEx} $ "'
Done

Echo "$ {input }"
Else
Echo "$ {prompt} [$ {default}]"
1> & 2
Read Input
If ["x $ {input}" = "X"];
Then
Echo "$ {default }"
Return 0

Fi
Chk_result = 'echo "$ {input}" | egrep "^ $ {RegEx} $ "'

While ["x $ {chk_result}" = "X"]; do
Echo "format error.
Please input again: [$ {default}] "1> & 2
Read Input

If ["x $ {input}" = "X"]; then
Echo "$ {default }"

Return 0
Fi
Chk_result = 'echo "$ {input}" |
Egrep "^ $ {RegEx} $ "'
Done
Echo "$ {input }"

Fi
}

Example:
Re_ip = '([1-9]? [0-9]) | (1 [0-9] {2 }) | (2 ([0-4] [0-9] | 5 [0-5])/.) {3} ([1-9]? [0-9]) | (1 [0-9] {2 }) | (2 ([0-4] [0-9] | 5 [0-5])'

Curr_ip = 'get _ INPUT "Please input current IP:" "$ {re_ip }"
"127.0.0.1 "'
Curr_ip is the correct value entered by the user. If you press enter, It is the default value "127.0.0.1 ".
Functions are used
The error output channel (2) is used to print the prompt string. The default value (if any) appears in [] After the prompt. The standard output channel (1) is used to return the processing result of the function.
The function also has a value format error deduplication mechanism.

2. Check the current user

Function check_user ()
{
Expect_user = $1
User = "'Id
-Un '"
If ["$ {user }"! = "$ {Expect_user}"]; then
Echo
"Current user is [$ {user}]."
Echo "you must run this shell
With user [$ {expect_user}]."
Return 1
Fi
}

3. Obtain the PID of the process with the specified keyword

Function get_pid ()
{
Proc_sign = $1
Proc_pid = 'ps axwwo
'% P % a' | grep $ {proc_sign} | grep-V' ^ * [0-9]/+ grep '| awk' {print
$1 }''
Echo $ {proc_pid}
}

From: http://blog.163.com/panfeng_pf/blog/static/11196085420093205644479/

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.