Shell advanced usage--functions

Source: Internet
Author: User

In the shell, if a piece of code needs to be called repeatedly, the code definition can be called a function, which is called directly in a later script. There are two ways to define a function:

1. function functionname {



}


2, functionname () {


}


Example: Define a function to add user Hadoop

ADDUSER () {username=hadoopif! id-u $USERNAME &>/dev/null, then Useradd $USERNAME echo $USERNAME | passwd- -stdin $USERNAME &>/dev/nullfi}adduserecho $?
[[email protected] ~]#/adduser.sh 0[[email protected] ~]#./adduser.sh 0

Execute the script twice in a row, return the status value is 0, here is a problem, the first time to execute this script, successfully added the user Hadoop, but the second execution, Hadoop already exists, then why this is the return status value of 0?

[Email protected] ~]# bash-x adduser.sh + adduser+ username=hadoop+ id-u hadoop+ echo 00

As can be seen from the above analysis, the return value is 0 because the last script is named Id-u Hadoop and the command executes successfully. Obviously, this is unreasonable. So what if we get different status values based on different situations?

You can use the return command to define a function's state value in a function using return.

The above script changes to this:

#!/bin/bash#adduser () {username=hadoopif! id-u $USERNAME &>/dev/null, then Useradd $USERNAME echo $USERNAM E | passwd--stdin $USERNAME &>/dev/null return 0else return 1fi}adduserif [$?-eq 0]; Then echo "Add user finished." else echo "Failed" fi


Delete Hadoop user re-execute see results

[Email protected] ~]# userdel-r hadoop[[email protected] ~]#./adduser.sh Add user finished. [Email protected] ~]#/adduser.sh Failed

The second time you add user Hadoop, you get an error.




Functions that Accept parameters:

#!/bin/bash#adduser () {username=$1if! id-u $USERNAME &>/dev/null, then Useradd $USERNAME echo $USERNAME | passwd--stdin $USERNAME &>/dev/null return 0else return 1fi}# #调用函数时也要跟 This variable adduser $ if [$?-eq 0]; Then echo "Add user finished." else echo "Failed" fi



Exercise: Write a script that determines which of the hosts between 192.168.0.200~192.168.0.254 are online and requires:

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 () {if ping-c 1-w 1 192.168.0. $I &>/dev/null; then echo "192.168.0. $I is online."      else echo "192.168.0. $I is offline." Fi}for I in {200..254}; Do PING 192.168.0. $Idone

Execute script

[Email protected] ~]#./ping.sh 192.168.0.200 is offline.192.168.0.201 are offline.192.168.0.202 is offline.192.168.0.203 is offline.


The information returned by the script output here is shown by the function ping, and if you do not want the function to show whether it is online or not, you can use return to improve the script, as follows:

#!/bin/bash#ping () {if ping-c 1-w 1 192.168.0. $I &>/dev/null; then return 0 else re Turn 1 fi}for I in {200..254}; Do PING 192.168.0. $I if [$-eq 0];    Then echo, "192.168.0. $I is online."    else echo "192.168.0. $I is offline." Fidone














This article from "Zengestudy" blog, declined reprint!

Shell advanced usage--functions

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.