use of function functions
Defining functions
1)
Function name () {
...
...
}
2)
Function name {
...
...
}
Calling functions
Function name
You can also pass parameters to a function by positional variables
Example:
Scripting, Implementing directory management functions, requiring the use of functions
#!/bin/bash
#
Createdir () {
Read-p "Enter directory:" dir
If [-D $dir]; Then
echo "Directory $dir exists"
Else
Mkdir-p $dir
echo "Directory $dir creation Complete"
Fi
}
RemoveDir () {
Read-p "Enter directory:" dir
If [-D $dir]; Then
RM-RF $dir
echo "Directory $dir Delete Complete"
Else
echo "Directory $dir does not exist"
Fi
}
Cat << EOF
===================
Directory Management
1. Create a Directory
2. Delete Directory
3. Exit script
==================
Eof
Echo
While true; Do
Read-p "Please enter your choice:" Choice
Case $choice in
1)
Createdir
;;
2)
RemoveDir
;;
3)
Exit 0
;;
*)
ECHO-E "\033[31m input Error! \033[0m "
;;
Esac
Done
Write scripts to implement user management functions
#!/bin/bash
#
CreateUser () {
Read-p "Enter User:" Name
if! ID $name &>/dev/null; Then
Useradd $name
Read-p "Enter Password:" Password
echo "$password" | passwd--stdin $name &>/dev/null
echo "User $name creation Complete"
Else
echo "User $name not present."
Fi
}
Removeuser () {
If ID $ &>/dev/null; Then
Userdel-r $
echo "User Delete complete"
Else
echo "User $ not present"
Fi
}
ModifyUser () {
Read-p "Enter User:" Name
If ID $name &>/dev/null; Then
sh_name=$ (grep "^ $name:"/etc/passwd | Awk-f: ' {print $7} ')
echo "User $name current shell: $sh _name"
Echo
echo "System Current Shell:"
Cat-n/etc/shells
Echo
Read-p "Please enter the shell to be modified:" New_sh_name
Usermod-s $new _sh_name $name
echo "User $name shell has been modified to $new_sh_name"
Else
echo "User $name not Present"
Fi
}
Cat << EOF
===============
User Management
1. Create user
2. Delete users
3. Modify the user shell
4. Exit script
===============
Eof
Echo
While true; Do
Read-p "Please enter your choice:" Choice
Case $choice in
1)
CreateUser
;;
2)
Read-p "Enter User:" Name
Removeuser $name
;;
3)
ModifyUser
;;
4)
Exit 0
;;
*)
echo "Input Error! "
;;
Esac
Done
Nginx Service Control Script:
Installing Nginx
[email protected] ~]# Yum install-y gcc pcre-devel zlib-devel openssl-devel
[Email protected] ~]# Tar XF nginx-1.11.4.tar.gz
[Email protected] ~]# CD nginx-1.11.4/
[Email protected] nginx-1.11.4]#/configure--prefix=/usr/local/nginx
[[email protected] nginx-1.11.4]# make && make install
The script is as follows:
#!/bin/bash
#
Nginx_cmd=/usr/local/nginx/sbin/nginx
Nginx_conf=/usr/local/nginx/conf/nginx.conf
Nginx_pid_file=/usr/local/nginx/logs/nginx.pid
Start () {
$nginx _cmd
If [$?-eq 0]; Then
echo "Service Nginx start ... [OK] "
Fi
}
Stop () {
$nginx _cmd-s Stop
}
Reload () {
If $nginx _cmd-t &>/dev/null; Then
$nginx _cmd-s Reload
Else
$nginx _cmd-t
Fi
}
Status () {
If [-e $nginx _pid_file]; Then
echo "Service Nginx (PID ' cat $nginx _pid_file ') is running ..."
Else
echo "Service Nginx is stopped ..."
Fi
}
If [-z]; Then
echo "using: $ {start|stop|restart|reload|status}"
Exit 9
Fi
Case $ in
Start
Start
;;
Stop
Stop
;;
Restart
Stop
Sleep 2
Start
;;
Reload
Reload
;;
Status
Status
;;
*)
echo "using: $ {start|stop|restart|reload|status}"
Exit 8
Esac
This article is from the "lyw666" blog, make sure to keep this source http://lyw666.blog.51cto.com/12823216/1957418
The definition and use of shell script function