Use function (function) examples in shell scripts _linux shell

Source: Internet
Author: User

function can make a similar custom execution command in shell script, the biggest function is to be able to simplify many of our program code. It is important to note that the shell script is executed from top to bottom/left to right, so the function in Shellscript must be set at the very front of the program so that the available program segments can be found at execution time.

Copy Code code as follows:

#!/bin/bash
# program
# This program was to show the use of ' function '
# History
# 2013/5/4 by Lvcy
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/loacl/sbin:~/bin
Export PATH

#输出统一信息
function Printinfo ()
{
Echo-n "Your choice is"
}
#将小写字符转换为大写字符
function Dotr ()
{
Tr ' A-Z '
}
Read-p "Please input your choice (one|two|three|four):" num
#用case做条件判断
Case $num in
"One")
Printinfo; echo $num | Dotr
;;
"Two")
Printinfo; echo $num | Dotr
;;
"Three")
Printinfo; echo $num | Dotr
;;
"Four") Printinfo; echo $num | Dotr
;;
Esac
Exit 0

The following is a generic shell script with function functions:

Copy Code code as follows:

#!/bin/bash
# program
# this are show the params of function
# History
# 2013/5/14 by Lvcy
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
function Printinfo ()
{
echo "Your choice is $"
}
Case is in
"One")
Printinfo 1
;;
"Two")
Printinfo 2
;;
"Three")
Printinfo 3
;;
"Four")
Printinfo 4
;;
Esac

Exit 0

If the above file name is sh02.sh, the command to execute this script is:

Copy Code code as follows:

SH sh02.sh One

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.