Under the Linux system, Shell scripts can help us in a variety of situations, such as displaying information, even automating specific system administration tasks, creating simple command-line tools, and so on.
In this guide, we'll show Linux novices how to reliably store custom shell scripts, explain how to write shell functions and libraries, and how to use functions from libraries in other scripts.
650) this.width=650; "class=" Size-full wp-image-55832 "src=" http://www.linuxprobe.com/wp-content/uploads/2017/03/ Shell11.jpg "width=" "height=" "style=" Height:auto; "/>
Where to store the Shell script
In order to execute your own script without entering the full or absolute path where the script is located, the script must be stored in one of the paths defined by the $PATH environment variable.
Use the following command to view the $PATH environment variables in your system:
$ echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
In general, if a directory named bin exists in the user's home directory, you can store the shell script in that directory, because that directory is automatically included in the user's $PATH environment variable (LCTT: Centos 6/7 This is not the case under Debian 8, under Ubuntu 16.04.
So, create a bin directory in your home directory (it can also be used to store Perl, Awk, or Python scripts, or other programs):
$ mkdir ~/bin
Next, create a directory called Lib(libraries) to store your own library of functions. You can also store libraries of other programming languages, such as C, Python, and other languages. Create another directory named Sh in the lib directory, which will be used to store your shell library:
$ mkdir-p ~/lib/sh
Create your own Shell functions and function libraries
A shell function is a set of commands that can accomplish a specific task in a script. They work in the same way as in other programming languages (LCTT may refer to a stored procedure like SQL), subroutines, and functions.
The syntax for writing a function is as follows:
Function name () {Series of commands}
(LCTT glossing: You can add the funtion keyword before the function name, but you can omit it)
For example, you can write a function to display a date in a script as follows:
Showdate () {date;}
Whenever you need to display a date, simply call the function's function name:
$ showdate
In a nutshell, the shell library is also a shell script, but you can store only functions in other shell scripts that need to be called in a library of functions.
Below is shown in my Under a directory named libmyfuncs.sh Library functions:
#!/bin/bash ### function to clearly list directories in path Showpath () { oldifs= "$IFS" ### store old internal field separator ifs=: ### specify a new internal field separator for dir in $PATH <br> do<br> echo $DIR <br> done ifs= "$oldifs" ### restore old internal Field separator}### function to show logged usershowusers () { echo -e "below are the user logged on the system:/n" w }### print a user ' s details printuserdets () { oldifs= "$IFS" ### store old internal field separator ifs=: ### specify a new internal field separator read -p "Enter user name to be searched: " uname ### read username echo " " ## # read and store from a here string values into variables ### using : as a field delimiter read -r username pass uid gid comments homedir shell <<< "$ (cat /etc/passwd | grep "^ $uname") " ### print out captured values echo -e "username is :&nBSP; $username/n " echo -e " User ' s id : $uid/n " echo -e " User ' s GID : $gid/n " echo -e "User ' s comments : $comments/n" echo -e "User ' s home dir : $homedir/n" echo -e "User ' s shell : $shell/n " ifs=" $oldifs " ### store old internal field separator}
Save the file and add execute permissions to the script.
How to call a function from a function library
To use a function in a lib directory, you first need to import the function library that contains the function into the shell script that you want to execute, in the following form:
$ . /path/to/lib or $ source/path/to/lib
(LCTT: The first line . and Paths must have spaces between them)
This allows you to use the printuserdets function from ~/lib/sh/libmyfuncs.sh in other scripts as demonstrated below.
In the following script, if you want to print out the details of a particular user, you don't have to write one by one more code, just simply call the existing function.
Create a new file named test.sh :
#!/bin/bash # # # include LIB. ~/lib/sh/libmyfuncs.sh### use function from libprintuserdets### exit Scriptexit 0
Save the file and make the script executable, and then run it:
$ chmod 755 test.sh$./test.sh
650) this.width=650; "class=" Alignnone size-full wp-image-55833 "src=" http://www.linuxprobe.com/wp-content/uploads/ 2017/03/shell12.png "width=" 438 "height=" 306 "style=" Height:auto; "/>
In this article, we show you where to store shell scripts reliably, how to write your own shell functions and libraries, and how to call some of the functions in a library from a library in a normal shell script.
After that, we'll also introduce a fairly straightforward way to configure Vim as an IDE (integrated development environment) that writes a Bash script. Before that, remember to keep an eye on us, and if you can share your thoughts on this guide, it will be even better.
。
Original address:http://www.linuxprobe.com/compile-shell-function.html
This article is from the "blog" blog, please be sure to keep this source http://coderhsf.blog.51cto.com/12629645/1912651
Linux custom Shell functions and function libraries