In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to write and use custom Shell functions and libraries under Linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write and use custom Shell functions and libraries under Linux.
Under Linux, 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 will 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.
Where to store Shell scripts
In order to execute your own script without entering the full or absolute path to the location of the script, 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 variable on your system:
$echo $PATH / usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Generally speaking, if there is a directory called bin 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 note: this is true under Centos 6 PATH 7, not under Debian 8, and again under Ubuntu 16.04).
So, create a bin directory under your home directory (of course, it can also be used to store scripts for Perl, Awk, or Python, or other programs):
$mkdir ~ / bin
Next, create a directory called lib (short for libraries) to store your own function library. You can also store libraries of other programming languages, such as C, Python and so on. Create another directory under the lib directory called sh, which will be used to store your shell library:
$mkdir-p ~ / lib/sh
Create your own Shell functions and libraries
A shell function is a set of commands that can accomplish a specific task in a script. They work similarly to procedures, subroutines, and functions in other programming languages.
The syntax for writing a function is as follows:
Function name () {series of commands}
(LCTT check note: you can add the function keyword before the function name, but you can also omit it.)
For example, you can write a function in a script to display the date as follows:
ShowDATE () {date;}
Whenever you need to display the date, simply call the function name of the function:
$showDATE
To put it simply, the shell library is also a shell script, but you can store only the functions that need to be called in other shell scripts in one library.
The following shows a library function named libMYFUNCS.sh in my ~ / lib/sh directory:
#! / 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
Do
Echo $DIR
Done IFS= "$oldifs" # restore old internal field separator} # Function to show logged user showUSERS () {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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.