The example above the Linux Shell script illustrates the conversion of decimal and binary, and ends the learning of this Part with an instance related to file loops. I believe that the basic learning of Linux shell scripts should be able to master the compilation of some simple Linux shell scripts.
File LoopProgram
Maybe you want to save all emails to one of the people in a file, but after a few months, this file may become so large that the access to this file may be slowed down. The following script rotatefile can be
To solve this problem. This script can rename the email storage file (assuming outmail) to outmail.1, and change outmail.1 to outmail.2 and so on...
#! /Bin/sh
# VIM: Set Sw = 4 ts = 4 et:
Ver = "0.1"
Help ()
{
Cat <
Rotatefile -- rotate the file name
Usage: rotatefile [-H] filename
Options:-H help text
Example: rotatefile out
This will e. g rename out.2 to out.3, out.1 to out.2, out to out.1
And create an empty out-File
The Max number is 10
Version $ ver
Help
Exit 0
}
Error ()
{
Echo "$1"
Exit 1
}
While [-n "$1"]; do
Case $1 in
-H) Help; shift 1 ;;
--) Break ;;
-*) Echo "error: no such option $1.-H for help"; Exit 1 ;;
*) Break ;;
Esac
Done
# Input check:
If [-z "$1"]; then
Error "error: You must specify a file, use-H for help"
Fi
Filen = "$1"
# Rename any. 1,. 2 etc file:
For N in 9 8 7 6 5 4 3 2 1; do
If [-F "$ filen. $ N"]; then
P = 'expr $ n + 1'
Echo "Mv $ filen. $ N $ filen. $ P"
MV $ filen. $ N $ filen. $ P
Fi
Done
# Rename the original file:
If [-F "$ filen"]; then
Echo "Mv $ filen $ filen.1"
MV $ filen $ filen.1
Fi
Echo touch $ filen
Touch $ filen
How does this script work? After the user provides a file name, we perform a 9-1 loop. File 9 is named 10, file 8 is renamed to 9, and so on. After the loop is completed, we name the original file as file 1.
Create an empty file with the same name as the original file.
Debugging
The simplest debugging command is the echo command. You can use echo to print any variable value in any suspected error. This is why most shell Programmers spend 80% of their time debugging programs. Shell Program
The advantage is that it does not need to be re-compiled, and it does not take much time to insert an echo command.
Shell also has a real debugging mode. If an error occurs in the script "strangescript", you can debug it as follows:
Sh-x strangescript
This will execute the script and display the values of all variables.
Shell also has a mode that only checks the syntax without executing the script. It can be used as follows:
Sh-N your_script
This will return all syntax errors
Here, the basic learning of Linux shell scripts is all over. Thank you for your support.