If you are an IT person who is used to the Windows Command Prompt, when you use the Linux command for the first time, you will feel at a loss. The DOS command you are familiar with does not exist in Linux.
If you are an IT person who is used to the Windows Command Prompt, when you use the Linux command for the first time, you will feel at a loss. The DOS command you are familiar with does not exist in Linux, there are a lot of commands to remember before you.
An alternative solution is to use powerful Linux shell commands to write shell scripts so that you can also use the doscommand in Linux. the following shows how to do it.
Basics of shell script writing
From scheduled backup to simple command execution, Linux shell scripts can execute various functions, and almost all programs can run with shell scripts, the script can even contain some simple conditions. The basic format of the shell script is as follows:
- #! /Bin/sh
- ...
- Your commands here
- ...
Pay attention to #! Starting from/bin/sh, this statement tells the operating system where to explain the script program. most systems have the/bin/sh directory because the directory contains the standard shell program of the root user, in most systems, you can specify the/bin/bash directory.
The scripts of 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 commands from a script is very simple, like running commands at a Windows DOS prompt. for example, you can use the following statement to copy files:
- #! /Bin/sh
- Cp file1 file2
- Mv file2 file3
- Echo "complete"> complete.txt
Automatic command execution is useful for tasks that do not require manual intervention, but not for general users. Therefore, the shell script allows you to enter command line parameters during execution, and then run the command using the input parameters. The input parameters in the script are represented by $1 to $9. if you have written a DOS batch file, you will find similar functions in the batch file, it only uses % 1, % 2, and so on to represent the input parameters. the following example shows how to use the command line parameters:
- #! /Bin/sh
- Cp $1 $2
The preceding script accepts two command line parameters. The first is the original file to be copied, and the second is the target file to be copied. the command format for running the script is :. /myscript file1 file2, myscript represents the script file name, command line options can also be passed in this way, such:
- #! /Bin/sh
- Cp $1 $2 $3
Run the command in the form of./copy-r sourcedir destdir to execute the preceding script, and recursively copy all the files in the $2 directory to the $3 directory. When option $1 is-r, the cp command can recursively copy all files.
Shell script with conditional selection
Simple shell scripts for tasks without variables are generally competent. However, when executing some decision-making tasks, it is necessary to include the if/then condition judgment. Shell script programming supports such operations, including comparison operations and determining whether a file exists. The basic if condition command options include:
-Eq-compare whether two parameters are equal (for example, if [2-eq 5])
-Ne-compare whether two parameters are not equal
-Lt-parameter 1: whether it is smaller than parameter 2
-Le-parameter 1: whether it is less than or equal to parameter 2
-Gt-whether parameter 1 is greater than parameter 2
-Whether ge-parameter 1 is greater than or equal to parameter 2
-F-check whether a file exists (for example, if [-f "filename"])
-D-check whether the directory exists
Almost all judgments can be implemented using these comparison operators. in scripts, the-f command option is often used to check whether a file exists before being executed.