Some application methods of B-SHELL are introduced.
Source: Internet
Author: User
Article title: introduce some application methods of B-SHELL. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Preface:
Here only introduce some of the use of B-SHELL, do not explain the basic knowledge of B-SHELL (with other languages have a lot of commonalities, if you know a language, will soon be able to get started ), if you are not very familiar with B-SHELL, and want to learn, it is recommended that you go to consult the relevant information (there are a lot of online about this aspect of the East; of course you can also buy some books; if you are a person who can read only by borrowing others' books, lend it! I am one of those people .). My capabilities are limited. if you have any shortcomings or errors, please give me more advice. thank you! All the introductions are applicable to Linux environments of kernel 2.2.13 and later.
B-SHELL is a convenient development tool, in an emergency to achieve a function of the product, and the C language can not be completed quickly, you can select the B-SHLL to achieve, and then use the C language to complete; in the product for those not often run programs can be replaced by B-SHELL, which can avoid the adverse consequences of improper use of C language; for some small amount of processing programs available B-SHELL implementation.
Discussion:
#! /Bin/sh à B-SHELL interpreter flag
ALL =$ @
#{
Description of variables with '$' in the B-SHELL: $0 à SHELL command itself
$ Nà location parameter n (n! = 0)
$ @ À all input variables
$ À current process number
$! À PID value of the latest background process
$? À return value of the latest command execution status
$ # À parameter count
}#
Declare-x TIME = 0
#{
Description of variable declaration:
Declare declares global variables. if declared in the function, it is only valid in the function (local variables ); -x indicates that the variable value can be passed to the subsequent command (from the function to the subsequent command);-I indicates that the variable is processed as an integer;-r indicates that the variable is degree-only; replace "-" with "+", block the declaration of the original variables, and restore to the undefined state. The attribute of the parameter in the script is determined by the first value assignment. if the parameter is a character for the first time, it will be processed later.
Local declares local variables. The local variable name [= "value"] is generally used within the function.
Define variables directly without specifying declare and local. The attribute of the parameter in the script is determined by the first value assignment. if the parameter is a character for the first time, it will be processed later. It can also be determined in the definition.
}#
Declare-x NUMBEROFFILE = 0
#{
Description of script functions:
Definition method: function name () or function name ()
{
Function body (for what you want to do)
}
Call method: function name parameter column
In the script, function calls are used like calling commands. À will be mentioned later
}#
Function hel_ver () à displays help information or version number
Function find_offer () à searches for a file containing the specified string in the specified directory and displays its path
{
For file in 'ls "$1" 'à for var in command do ......... A typical application of done to process directories or files under the $1 Directory
Do
If [-d "$1"/$ file]; thenà judge whether $ file is a directory
Continue
Else
String = 'grep "$2" "$1"/$ file' à query string
If [-n "$ string"]; thenà determines that the string is not empty
Echo $ string>/str
Str = 'awk' {print $1} '/str' à get the first field value of str
Rm/str
If ["$ str "! = "Binary"]; thenà remove Binary files
NUMBEROFFILE = 'expr $ NUMBEROFFILE + 1', which is equivalent to NUMBEROFFILE + = 1 in C language. Available
Let 'numberoffile = NUMBEROFFILE + 1' replace
TIME = 'expr $ TIME + 1'
Echo "$1/$ file" à Show matching files
Fi
Fi
Fi
Done
}
# Else
Function find_curr () à searches for files containing the specified string in the current directory, and displays the path (relative path). the implementation mechanism is the same as that of find_offer.
{
# If ["$ #"-eq "1"]; thenà
For file in 'ls./'if you only enter one parameter, find the file in the current working directory
Do
If [-d "$ file"]; then
Continue
Else
String = 'grep "$1" $ file'
If [-n "$ string"]; then
Echo $ string>/str
Str = 'awk' {print $1} '/str'
Rm/str
If ["$ str "! = "Binary"]; then
NUMBEROFFILE = 'expr $ NUMBEROFFILE + 1'
TIME = 'expr $ TIME + 1'
Echo "./$ file"
Fi
Fi
Fi
Done
}
Function print_num () à shows the number of files to be searched. (this is not implemented when you query multi-level directories. if you know it, please let me know. I will be very happy !)
{
If ["$ NUMBEROFFILE"-gt "0"]; then
# Echo ""
If ["$ #"-eq "1"]; then
#{
Color Display description:
Different linux systems run different commands. In LINUX/BSD: echo-e "\ 033 [40; 32 M"; in V: echo "\ 033 [40; 32 M ". In this example, the previous method is used. 40 bits are the background color, with a value of 40 to 47, 32 bits are the foreground color, and a value of 30 to 37; \ 033 is a fixed usage. Color display must be matched in teams, as shown in the script. Usually the next color is
Echo-e "\ 033 [40; 37 m" (meaning restoring normal color ). Color Display Control usually appears in pairs, for example,
Echo-e "\ 033 [40; 37 m" à normal display (black background, white foreground ). The color display control can be used to create beautiful liunx menus (of course not comparable to Xwindows and WINDOWS ).
}#
Echo "Finded 'echo-e" \ 033 [40; 32 m "'$ NUMBEROFFILE 'echo-e" \ 033 [40; 37 m "' file (s) which have the string 'echo-e "\ 033 [40; 33 m" '"$1" 'echo-e "\ 033 [40; 37 m "'."
Else
Echo "Finded 'echo-e" \ 033 [40; 32 m "'$ NUMBEROFFILE 'echo-e" \ 033 [40; 37 m "' file (s) which have the string 'echo-e "\ 033 [40; 33 m" '"$2" 'echo-e "\ 033 [40; 37 m "'."
Fi
Else
If [-z "$2"]; then
Echo "Finded 'echo-e" \ 033 [40; 32 m "'no' echo-e" \ 033 [40; 37 m "'File under current directory ."
Else
Echo "Finded 'echo-e" \ 033 [40; 32 m "'no' echo-e" \ 033 [40; 37 m "'File under directory" $1 "."
Fi
Fi
}
Function fdir () à searches for the directories contained in the current directory and its subdirectories from the given directory, and stores the/tmp/dtmp file.
{
>/Tmp/dtmp
# Cd/
Echo "$1">/tmp/dtmp
Tmp = 'echo $1 | grep "\/proc" 'à do not find the directory in the/proc directory
If [! -Z $ tmp]; then
# Echo "/proc"
Exit
Fi
I = 'expr $ I + 1' à number of directories found in the record. this value is added to each directory record added to the/tmp/dtmp file (the same as below)
### First grade
Cd $1 à enter the script's working directory (the console's working directory will not be changed)
For loop in 'ls ./'
Do
# Echo $ loop
If [-d $ loop]; thenà determines whether $ loop is a directory (the same as below)
Tmp = 'echo $ loop | grep "proc "'
If [! -Z $ tmp]; then
Continue
Fi
I = 'expr $ I + 1'
Echo "'pwd'/$ loop">/tmp/dtmp à add the absolute path of the directory to the/tmp/dtmp file (the same as below)
### Second grade
Cd $ loop à enter the script level 2 working directory
For loop1 in 'ls ./'
Do
# Echo $ loop1
If [-d $ loop1]; then
Tmp = 'echo $ loop1 | grep "proc "'
If [! -Z $ tmp]; then
Continue
Fi
I = 'expr $ I + 1'
Echo "'pwd'/$ loop1">/tmp/dtmp
### Third grade
Cd $ loop1 à go to the script level 3 working directory
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.