function function~

Source: Internet
Author: User
Tags disk usage

functions: Functions, function

Code reuse:

Library : So

a method that defines a function has 2 species

The first type:

function funcname{

Command

}

The second type:

FUNCNAME () {

Command

}

write a script "input D Display hard disk usage information, enter m Display memory usage information, enter s Show Swap usage information, enter Q then exit the "

#!/bin/bash

#

Cat << EOF

d| D) Show Diskusages

m| M) Show Memoryusages

s| S) Show Swapusages

q| Q) quit

Eof

Read-p "Yourchoice:" CHOICE

until [$CHOICE = = ' Q '-o $CHOICE = = ' Q '];d o

Case $CHOICE in

d| D

Df-lh

;;

m| M

Free-m |grep "^mem"

;;

s| S

Free-m |grep "^swap"

;;

*)

Cat << EOF

d| D) Show Diskusages

m| M) Show Memoryusages

s| S) Show Swapusages

q| Q) quit

Eof

Read-p "Yourchoice:" CHOICE

;;

Esac

Cat << EOF

d| D) Show Diskusages

m| M) Show Memoryusages

s| S) Show Swapusages

q| Q) quit

Eof

Read-p "Yourchoice:" CHOICE

Done

put Cat made into a function, and then called

#!/bin/bash

#

ABC () {

Cat << EOF

d| D) Show Diskusages

m| M) Show Memoryusages

s| S) Show Swapusages

q| Q) quit

Eof

}

Abc

Read-p "Yourchoice:" CHOICE

until [$CHOICE = = ' Q '-o $CHOICE = = ' Q '];d o

Case $CHOICE in

d| D

Df-lh

;;

m| M

Free-m |grep "^mem"

;;

s| S

Free-m |grep "^swap"

;;

*)

Abc

Read-p "Yourchoice:" CHOICE

;;

Esac

Abc

Read-p "Yourchoice:" CHOICE

Done

Return # Define Execution status return value, function does not execute return after the statement.

# : 0-255

If a user calls a function if the user Admin exists then returns 1 Create a user if the user does not exist, and set the password, return value 0 ,

If you return 0 the user is added successfully, if the return 1 shows that the user already exists

#!/bin/bash

#

function ABC {

Username= ' admin '

if! Id-u $USERNAME &>/dev/null;then

Useradd $USERNAME

echo "$USERNAME" |passwd--stdin&>/dev/null

return 0

Else

Return 1

Fi

}

Abc

If [$?-eq 0];then

echo "Add user finished"

Else

echo "User exist"

Fi

Functions that Accept parameters:

FUNCTION a B

$1:a

$2:b

1 to the Ten any 2 add an adjacent integer

#!/bin/bash

#

function Qwe {

Echo $[$1+$2]

}

For I in {1..10};d o

Let j= $I +1

echo "$I pluse$j is ' qwe $I $J '"

Done

Modify the above script so that the script can be created consecutively Ten a user

#!/bin/bash

#

function ABC {

Username=$1

if! Id-u $USERNAME &>/dev/null;then

Useradd $USERNAME

echo "$USERNAME" |passwd--stdin&>/dev/null

return 0

Else

Return 1

Fi

}

For I in {1..10};d o

ABC user$i

If [$?-eq 0];then

echo "Add user finished"

Else

echo "User exist"

Fi

Done

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

    1. 1. use a function to implement the decision process for a single host:

    2. 2. This function is called in the main program to determine the online status of all hosts within the specified range.

#!/bin/bash

#

function PING {

If Ping-c 1-w 1 192.168.0. $I &>/dev/null;then

return 0

Else

Return 1

Fi

}

For I in {200..254}; Do

PING $I

If [$?-eq 0];then

echo "192.168.0. $I is Up"

Else

echo "192.168.0. $I is Down"

Fi

Done

Write a script: Use the function to complete

1. The function can accept a parameter with the user name:

Determine if a user exists

if it exists, it returns the user's Shell and the UID and returns the normal status value

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

2 . To invoke a function in a program:

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

Extended 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 enter the user, but if the user entered the Q or Q exit;

#!/bin/bash

#

function Iduser {

If Id-u $1&>/dev/null; then

echo "Shell is: ' Cat/etc/passwd|grep" ^$1\b "|awk-f: ' {print $7} '"

echo "UID is: ' Id-u $ '"

return 0

Else

echo "User not exist."

Return 1

Fi

}

Read-p "Username:" username

until [$ = = ' Q '-o $? = = ' Q ']; Do

Iduser $USERNAME

If [$?-eq 0];then

Read-p "PLEASW input again:" USERNAME

Else

Read-p "No$username,please input again::" USERNAME

Fi

Done


This article from "Linux operation and Maintenance" blog, declined reprint!

function function~

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.