1. Overview
It is often necessary to use bash to batch process file formats, and it is troublesome to query some basic syntax every time.
So this article summarizes some common syntax when using Bash to traverse a folder directory to process images as an example.
2. Iteration
Bash traversal is relatively simple and not significantly different from the for loops of most programming languages.
The output is as follows.
This traversal is practical in most cases.
3. Path handling
The above output shows that the execution script demo.sh is also in it, and it can only be processed for the current path.
Then try to add a custom path and put the demo script in another location.
The output is as follows.
In practice, you can adjust $path
as an incoming parameter with the following script.
Execution.
$0 is the name of the script file, e.g. demo.sh
In addition to $[number]
getting the passed-in parameter, there are some common special parameter values that can be used directly.
parameter | description |
---|---|
$# |
Total number of parameters |
$$ |
Current PID number |
4. Extracting file names
After adding a custom path above, the output $file
also carries the path information. In practice, you will also use filenames, extensions, etc.
The following shows how to extract file path, file name (with extension), file name, extension.
|
|
The output after execution is as follows.
If there are multiple suffixes, such as a.tar.gz
, other less common ones are extracted as follows.
5. conditional judgments
5.1. test and []
Bash provides the test keyword for conditional judgments, and occasionally you will see if [ string1 ! = string2 ]
The above two write-ups are equivalent, i.e. test
and [
are equivalent, both are keywords in Bash, the difference is that [
requires the last argument to be ]
Since
[
is a keyword and]
is an argument, there must be spaces, [string1 != string2], which is written without spaces, is wrong.
Since the two are equivalent, the following illustration for test, replacing test condition
in the script with [ condition ]
, is also valid.
5.2. Judgment Operators
The following is a sample script for common conditional judgments.
|
|
The output after execution is as follows.
The list of common operator parameters for conditional judgment numeric type
is as follows.
parameter | description |
---|---|
-eq | equal |
-ne | not equal |
-gt | > |
-ge | >= |
-lt | < |
-le | <= |
When judging the condition document
, the list of common operator parameters is as follows
parameter | description |
---|---|
-e | file exists |
-r | file exists and is readable |
-w | file exists and is writable |
-x | file exists and is executable |
-s | file exists and is not empty |
-d | is a directory |
-f | is a file |
The determination of file and numeric content is relatively simple, and the next step is to look at the more complex string determination.
In test
, when determining if a string is empty, -z
means True when the string is empty
, and -n
means True when the string is not empty
.
The output after execution is as follows.
When using test to make judgments, it is recommended to use double quotes to enclose variables, otherwise the expected execution will not be achieved because of spaces in the variable content.
In addition to double quotes to avoid empty cells in the variable path, you can also use [[]]
to avoid splitting the content, considering that the variable path may be entered by the user.
5.3. Special [[]]
Both [
and test
split the content when judged, e.g. test="go to"
is recognized as go
and to
arguments.
Unlike [
, both [[
and ]]
are commands, and ]]
is not an argument.
The following code demonstrates the difference.
|
|
The output is as follows.
6. Image conversion example
By this point, the basic syntax for writing a traversal script is complete, and for reference, here is a traversal script that converts all files in a given directory to webp images.
|
|