Use shell programming to implement DOS-style Linux command lines

Source: Internet
Author: User
Tags linux shell commands
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:

  1. #! /Bin/sh
  2. ...
  3. Your commands here
  4. ...

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:

  1. #! /Bin/sh
  2. Cp file1 file2
  3. Mv file2 file3
  4. 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:

  1. #! /Bin/sh
  2. 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:

  1. #! /Bin/sh
  2. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.