Linux commands: simple function calls

Source: Internet
Author: User

Functions of Script Programming: function: Functions



Structured programming, cannot run independently, needs to be executed when called, can be invoked multiple times



1. Define a function:

function FUNCNAME {

Command

}



FUNCNAME () {

Command

}



2. Custom function Execution Status return values: Return # (0-255)



3. Functions that Accept parameters:

./a.sh m N

$1:m

$2:n



Twoint 5 6

$1:5

$2:6



Exercise 1: Write a script that automatically displays relevant information

#!/bin/bash

# diskusage.sh

function ShowMenu () {#定义函数
Cat << EOF
d| D) Show disk usages
m| M) Show memory usages
s| S) Show swap usages
q| Q) quit
Eof
}

Read-p "Your Choice:" Choice
until [$CHOICE = = ' Q '-o $CHOICE = = ' Q '];d o
Case $CHOICE in
d| D) df-h;;
m| M) free-m |grep "^mem";;
s| S) free-m |grep "^swap";;
*) ShowMenu
Read-p "Your choice,again:" Choice;;
Esac
ShowMenu
Read-p "Your choice,again:" Choice
Done


Exercise 2: Write a script that automatically adds a user and the same password as the user

#!/bin/bash
# adduser.sh
#
ADDUSER () {
if! Id-u $ &>/dev/null;then
Useradd $
echo $ | passwd--stdin $ &> 1116.www.qixoo.qixoo.com/dev/null
return 0
Else
Return 1
Fi
}

For I in {1..3};d o
ADDUSER user$i
If [$?-eq 0]; then
echo "Add user$i finished."
Else
echo "User$i exists,add failuer."
Fi
Done


Exercise 3: Write a script that determines which hosts are online between 192.168.0.200-192.168.0.254. Requirements:

1, the use of functions to achieve a host of the decision process;

2. Call this function in the main program to determine the online status of all hosts within the specified range.

#!/bin/bash
# ping.sh
#
PING () {
If PING-C1-W1 $ &>/dev/null;then
return 0
Else
Return 1
Fi
}

For I in {200..210};d o
PING 10.109.131. $I
If [$?-eq 0]; then #上一条语句执行状态结果0或1
echo "10.109.131.$1 is up."
Else
echo "10.109.131.$1 is down."
Fi
Done


For I in {200..210};d o
If PING 10.109.131. $I; then #彩色部分输出return的结果0或1, plus anti-quotes is the information that echo displays
echo "10.109.131.$1 is up."
Else
echo "10.109.131.$1 is down."
Fi
Done


Exercise 4: Writing a script: using Functions to complete

1, the function can accept a parameter, the parameter is the user name, determine whether a user exists,

Returns the shell and UID of this user if it exists, and returns the normal status value 0;

If it does not exist, it says that the user does not exist and returns an error status value of 1;

2, the function is called in the main program;

Extension 1: In the main program, let the user enter the user name, passed to the function to judge;

Extension 2: In the main program, after entering the user name judgment does not exit the script, but prompts the user to continue to enter the next user name, if the user entered the user does not exist, please re-enter the user, but if the user entered Q or Q to exit;

#!/bin/bash
# bash.sh
#
USER () {
If ID $ &>/dev/null; then
echo "' grep ^$1/etc/passwd |cut-d:-f3,7 '"
return 0
Else
Return 1
Fi
}

Read-p "Keyin your username:" username
until [$USERNAME = = ' Q '-o $USERNAME = = ' Q '];d o
If USER $USERNAME; then #if语句执行USER的return值0或1
Read-p "Keyin your username:" username
Else
Read-p "$USERNAME exists,please keyin your USERNAME again:" USERNAME
Fi
Done

[etc]#./bash.sh
Keyin your Username:root
0:/bin/bash
Keyin your USERNAME:WW
WW Exists,please Keyin your username again:tt
TT Exists,please Keyin your username again:root
0:/bin/bash
Keyin your Username:q

[etc]#

Linux commands: simple function calls

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.