What about Linux (13) -- shell programming

Source: Internet
Author: User

Before writing an article, I want to know what shell programming is and what is the difference between shell programming and C/C ++ programming?

I understand that shell programming is to use Linux operating commands to program according to certain syntaxes.

The difference with C/C ++ is that shell programming has a certain degree of platform relevance, that is, some commands can be used in this Linux operating system, however, in another Linux system, it may not be enough, but C/C ++ programming can achieve platform independence.

If you don't mind the running time, you can use shell programming because it is simple. If you consider platform independence, you need to consider general shell commands or C/C ++ programming.

1. Introduction to shell scripts

1.1 why shell scripts are used

Shell scripts save a lot of time and are powerful in Processing Automatic loops or large tasks. If you have a command list for processing a task, you have to repeat it one by one, observe the output result, and decide whether it is correct. If it is correct, continue to the next task, otherwise, go back to the list and observe it step by step. A task may be to classify files, insert text to files, migrate files, delete rows from files, clear expired files from the system, and perform common system management and maintenance work. Create a script. When using a series of system commands, you can quickly create scripts using variables, conditions, arithmetic, and loops to complete the corresponding work. This saves a lot of time than the next entry in the command line. A shell script can receive information in a command and use it as the input of another command. For different UNIX and Linux systems, using a shell script requires some minor changes to run. In fact, shell migration is not a problem, but there are differences in command migration between systems.

1.2 script content

A script is not a complex program. It is interpreted by line. The program must start with the following line (the first line of the file must be placed ):
#! /Bin/sh
Symbol #! The parameter used to tell the system that the program is used to execute the file.

Any script may have comments. The first character of this line is #. The interpreter will not explain this line. Writing a script name in the second line of comment is a good habit. Run the script from top to bottom. You need to grant the execution permission before running the script. Make sure that the script path is correctly established so that you can run it only with the file name.

1.3 run a script

$ Vim cleanup

#! /Bin/sh

# Name: cleanup

# This is a general cleanup script

Echo "Starting cleanup... wait"

Rm/usr/local/apps/log/*. Log

Tail-40/var/adm/messages>/tmp/messages

Rm/var. ADM/messages

MV/tmp/messages/var/adm/messages

Echo "finished cleanup"

By truncating the file names in the directory, the above script clears information under/usr/ADM/and deletes all registration information under/usr/local/apps/log.

You can use the C h m o d command to increase the script execution permission.

$ Chmod U + x cleanup

Run the script now. Just enter the file name: $ cleanup

SH: cleanup: Command not found

Try: $./cleanup

 

 

 

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.