In this article, I will describe libMerge, a Python 3.x script I made. libMerge has the ability to merge function definitions in the Bash library into ShellScript. To be precise, only the definition part of the function used in Script is extracted from the Bash library, Create a new Script that describes "extracted function + original Script contents".
The reason for creating libMerge is as follows.
By the way, in 2005, in the article "shsubrmerge (CodeZine) to share and merge shell scripts", A Script with almost the same function is published. It's a reinvention of the wheel. Furthermore, there seems to be no well-known package with equivalent functionality, so It is expected that there will be no demand. ~~ It's good because I can solve my problem (・ word ・) ~~
Since it is published on Github, you can get it by the following procedure (Debian assumption). The libMerge installation directory is under "/ usr / local / bin /".
$ git clone https://github.com/nao1215/BashScriptsCompilation.git -b master
$ cd BashScriptsCompilation
$ sudo ./installScripts #Installation script
It seems that few people have a Bash library, so I prepared a sample library bashLib. BashLib will also be installed by the installation procedure above.
The functions that exist in bashLib are as follows.
Function name | Description |
---|---|
errMsg | Error message displayed in red on standard output |
warnMsg | Display error message in yellow on standard output |
checkArgc | Check the number of arguments |
isFile | Check if the file exists |
isDir | Check if the directory exists |
isSubshell | script is"source"Check if it is executed by the command |
isNumeric | Check if the variable is quantified |
isNull | variable(String)Check if is Null |
isRoot | Check if the user is root |
++ | variable(Numerical value)Increment |
-- | variable(Numerical value)Decrement |
AplusB | Add the specified value to the variable |
AminusB | Subtract the specified value to the variable |
_sum | variable(Numerical value)Add |
makeDir | Create a directory after various checks |
makeEmptyFile | After various confirmations, create a 0Byte file |
getLineNum | Get the number of lines in the file |
dos2unix | Convert files from windows format to unix format |
option | Description |
---|---|
-l(--lib) | Describe the PATH to the Bash library. |
-s(--script) | To Script that wants to give function definition in library Describe the PATH. |
-o(--output) | Script that posted the function definition(Create New)Name If you want to change it, enter the file name. (Default:"m_<original_scriptname>)" |
As an example, prepare the following sample script (sample.sh). In sample.sh, makeDir () and errMsg () defined in "/ usr / local / bin / bashLib" are used. To use these functions, you need to include the library with the "source" command.
sample.sh
#!/bin/bash
# Description : Sample Script
source /usr/local/bin/bashLib #Library include
makeDir "dir" #Library function
errMsg "Sample" #Same as above
Execution result.
$ ./sample.sh
make directory dir
Sample
Use libMerge to add a function definition to the above sample.sh. Note that instead of inserting the function definition directly into sample.sh, a new Script called m_sample.sh will be generated. The name of the new Script here can be changed with the "-o" option.
$ ls
sample.sh
$ libMerge -s sample.sh -l /usr/local/bin/bashLib
$ ls
m_sample.sh sample.sh
The generated m_sample.sh is as follows. The function definition is inserted under the Script header, and only the function definition used in Script (sample.sh) is posted.
m_sample.sh
#!/bin/bash
# Description : Sample Script
function errMsg() {
local message="$1"
echo -n -e "\033[31m\c" # Escape sequence to make text color red
echo "${message}"
echo -n -e "\033[m\c" # Escape sequence to restore font color
}
function makeDir() {
local dir_path="$1"
checkArgc "$#"
if [ -d "${dir_path}" ]; then
echo "Directory "${dir_path}" already exists. Not make it."
else
mkdir -p $1
if [ "$?" = 0 ]; then
echo "make directory "${dir_path}""
fi
fi
}
source /usr/local/bin/bashLib #Library include
makeDir "dir"
errMsg "Sample"
The following is an overview of the libMerge process.
Limitation -There is room for improvement in the extraction of the function definition part (especially the definition end part). -The function to delete the library include processing that exists in the original Script is not implemented yet. ・ We have not conducted sufficient tests.
"Because the goal of 2017 is" package creation (C language) ", let's implement libMerge in C language." I thought about such a thing at the time of design (sleeping on February 17, 2011), but I implemented it in Python and it was the correct answer.
Also, the reason why it is difficult to make Bash into a library is "Lack of return value" "The nature of Bash that makes full use of redirect pipes" I experienced it with the creation of libMerge and bashLib this time. The advantages of creating a library are about "improving readability of one-liner (shell art)" and "integrating template processing". If you try to create a good library that demands more than that, you're likely to get bogged down.
Recommended Posts