If you are an IT person accustomed to a Windows command prompt, you will feel overwhelmed when you first use the Linux command line. You are familiar with DOS commands that are basically non-existent in Linux. In front of you is a lot of orders to memorize.
One alternative is to use a powerful Linux shell command to write shell scripts that allow you to use DOS commands under Linux as well. Let me tell you what to do.
Shell Scripting Basics
From scheduled backups to simple commands, Linux shell scripts can perform a variety of functions. Almost all programs can be run with shell scripts. You can even include some simple conditional selections in your script. The basic format of the shell script is as follows:
#!/bin/sh
...
Your commands here
...
Note that the file starts with #!/bin/sh. This statement tells the operating system where the program is used to interpret the script. Most systems will have a/bin/sh directory, because the directory contains the standard shell program of the root user. In most systems you can also specify the/bin/bash directory.
The scripts for each shell are different. Some shells, such as bash, support more commands than the standard shell. In most Linux versions, SH is actually bash.
Running a command from a script is very simple, much like running a command at a Windows DOS prompt. For example, you can copy a file by using the following statement:
#!/bin/sh
cp file1 file2
mv file2 file3
echo "complete" > complete.txt
Automating commands is useful for tasks that do not require human intervention, but is less useful for general users. To do this, the shell script allows the user to enter command-line arguments during execution and then runs the command with the input parameters. The input parameters in the script are expressed in $ $. If you have written a DOS batch file, you will find that there are similar functions in the batch file, except that it uses% 1,%2, etc. to represent the input parameters. Here's how to use command-line arguments, for example:
#!/bin/sh
cp $1 $2
The previous script accepts 2 command-line arguments, the first is the original file to be copied, and the second is the target file for the copy. The command format to run the script is:./myscript file1 File2,myscript represents the script file name. command-line options can also be passed in this way, such as:
#!/bin/sh
cp $1 $2 $3
Typing the./copy–r sourcedir destdir Command executes the previous script to recursively copy all the files in the $ directory to the $ directory. Option-r enables the CP command to recursively copy all files.
Shell script with conditional selection
Simple shell scripts are generally competent for tasks that do not contain variables. However, in the implementation of a number of decision-making tasks, it is necessary to include the if/then of the conditional judgment. Shell scripting supports such operations, including comparing operations, determining whether a file exists, and so on. The basic if Condition command options are:
-eq-compares two arguments for equality (for example, if [2–eq 5])
-ne-compare two arguments for unequal
-lt-parameter 1 is less than parameter 2
-le-parameter 1 is less than or equal to parameter 2
-gt-parameter 1 is greater than parameter 2
-ge-parameter 1 is greater than or equal to parameter 2
-f-Check if a file exists (for example, if [-F "filename"])
D-Check if the directory exists
Almost all judgements can be implemented using these comparison operators. The common-f command option in a script checks to see if it exists before executing a file.