How to program and run a Shell program in Linux

Source: Internet
Author: User
Tags echo command
You can use any editing program to compile a Shell program. because the Shell program is interpreted and executed, you do not need to compile it into a target program. according to the Shell programming convention, take bash as an example.

You can use any editing program to compile a Shell program. because the Shell program is interpreted and executed, it does not need to be compiled into a target program. according to the Shell programming convention, bash is used as an example, the first line of the program is generally "#! /Bin/bash ", where # indicates that this row is a comment and an exclamation point! Tell the Shell to run the command after the exclamation mark and use the rest of the document as the input, that is, run/bin/bash and let/bin/bash execute the Shell program content.

There are three methods for executing Shell programs.

1. sh Shell program file name

The command format for this method is: bash Shell program file name

This is actually to call a new bash command to explain the program, and pass the Shell program file name as a parameter to it. the newly started Shell will read the specified file, the commands listed in the executable file. after all the commands are executed, this method can use the Shell debugging function.

2, sh

Format: bash <Shell program name

In this way, the Shell command interpreter uses the specified program file to redirect input.

3. run the chmod command to make the Shell program executable.

Whether a file can run depends on whether the content of the file is executable and the file has the execution right. for Shell programs, when a file is generated using the editor, the permission granted by the system is 644 (rw-r --). Therefore, when you need to run this file, you only need to directly type the file name.

Among the three methods for running Shell programs, it is best to choose the following method: when a Shell program is just created and its correctness is not yet grasped, the first method should be used for debugging. When a Shell program has been debugged, use the third method to fix it. later, you only need to enter the corresponding file name and can be called by another program.

4. bash program debugging

Errors are inevitable during programming. sometimes, debugging takes more time than programming, as does Shell.

The main purpose of Shell program debugging is to use the bash command to explain the program selection items. the form of calling bash is: bash-select item Shell program file name

Several common options are:

-E. exit immediately if a command fails.

-N reads commands but does not execute them.

-During u replacement, unconfigured variables are considered as errors.

-V displays the Shell input rows.

-X displays commands and their parameters when executing commands.

All the options above can also be referenced in the Shell program in the form of "set-select item", and "set + Select item" will disable this option, if you only want to use some selection items for a part of the program, you can enclose this part with the preceding two statements.

(1) unset variable exit and exit now

The unset variable exit feature allows you to check all variables. If an unassigned variable is referenced, Shell program execution is terminated. Shell usually allows the use of unspecified Variables. in this case, the variable value is null. If you have set an unset variable to exit the selection item, once you have used the unset variable, the error message is displayed and the program is terminated. Unset variable exit select item-u.

When a Shell is running, if a command does not exist or cannot be executed, the redirection fails, or the command ends abnormally, the error message is displayed on the terminal screen without redirection, the Shell program continues to run. To force the Shell program to end immediately when an error occurs, you can use the-e option to terminate the execution of the Shell program immediately.

(2) Shell program tracking

The main method to debug a Shell program is to use Shell commands to explain the program's-v or-x options to track program execution. -V selects the item so that the Shell will display every command line it reads during the execution of the program, the-x option enables Shell to display every command it executes in the process of executing the program with a + + command name at the beginning of the line. And display each variable and the value of the variable. Therefore, the main difference between them is that-v is not available before the command line is executed, and the original contents of the command line are displayed, if-v exists, the content of the replaced command line is displayed.

In addition to Shell-v and-x options, you can also take some auxiliary debugging measures within the Shell program, such, the echo command can be used in some key aspects of the Shell program to display the necessary information. its function is equivalent to the printf statement in the C language, in this way, you can know where the program runs and the current state of the program.

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.