This article is the Second Reading Note of Flow Control in Chapter 5 of Learning the bash Shell 3rd Edition, but we will not limit it to this. Flow control is a common part of any programming language and includes bash. Here, we will continue to learn about them.
Unlike C, the shell matches the elements in the list, so it is very suitable for command parameters and file lists. The for format is as follows:
For name [in list]
Do
Statements that can use $ name...
Done
We will further learn through the example below. Three examples are set in total.
# Test for the bash loop: for method
#
# Test 1: displays detailed information about each specific PATH in $ PATH.
Function getpath
{
# Set the separator to ":". The default Delimiter is space.
IFS =:
# For testing, where-d in ls-ld indicates that the directory attributes are displayed, and files under the directory are not displayed.
For dir in $ PATH
Do
Ls-ld $ dir
Done
}
# Usually $ PATH contains ~ /Bin directory, which generally does not exist. An error is reported in the ls display and needs to be picked out for processing. At the same time, we add an empty element for analysis.
Function getpath1
{
Path = $ path ::
Echo check: $ path
Ifs =:
For dir in $ path
Do
# If it is an empty element, convert it to the current directory.
If [-Z $ dir]; then dir =.; FI
If! [-E $ dir]; then
Echo "$ DIR is not exist! "
Elif! [-D $ dir]; then
Echo "$ DIR is not a directory! "
Else
Ls-ld $ dir
Fi
Done
}
# Echo 'run getpath'
# Getpath
Echo 'run getpath1'
Getpath1
# Test 2 shows whether the specified file is a valid file name
Function finfo
{
If! [-E "$1"]; then
Echo "$1 is not an availble file ."
Else
Echo "$1 is a file ."
Fi
}
Echo 'run fileinfo'
# If the command does not contain any parameter, that is, if list is empty, the code in for is not executed.
For filename in $ @; do
Echo "in for loop ..."
Finfo "$ filename"
Done
The following is a recursive example. We hope to display the files in the directory as well. Based on the hierarchy, move a tab from the next level to the next level.
# Test 3: displays the files in the directory in recursive mode. Showdirfile indicates that all files in the directory are displayed. If it is also a directory, it is recursively executed. For ease of record, load comments to the end of the command line. Note that this is only for ease of reading, not for shell syntax.
Function showdirfile
{
#Step 1
: Search only for valid directory files
If [-d $1]; then
# Step 2
To record the character of the previous tab.
Currenttab + = "/t"
#$ (Command <command>)
Returns the output result of a command as a string.
# Step 3
: Search for the entry displayed by ls <dir_name>. If it is a file, it is displayed. If it is a string and "/" is added to it, it indicates a directory. recursive is used, search for files in this directory.
For file in$ (Command ls $1 );
Do
Echo-e-n $ {currenttab} $ File
# Step 4:
The file's path name is $1/$ file. Because we didn't use CD $ file, we didn't access this directory. Therefore, we need to record the path. If it is a directory, use dir to record the name of this subdirectory
If [-d $1/$ file]; then
Echo "/"
Showdirfile $1/$ File
Else
Echo
Fi
Done
# Step 2
After the launch, restore the tab string at the upper level, that is, remove the last/t.
Currenttab =$ {currenttab % "/t "}
# End of step 1
Fi
}
# If IFS is set earlier, the ls output is not separated by spaces. If an exception occurs, you need to reset it.
Unset
IFS
Echo 'run file prepare echture'
Showdirfile ${1 :-"~ "}
Related Links: My articles on Linux operations