UNIX System Management: Introduction to Shell programming

Source: Internet
Author: User
Keywords UNIX System Management Introduction to programming
Tags command line command-line arguments create environment environment variables file name programming

To complete this chapter, you can do the following things:

Write a simple shell program

Passing parameters to the shell program through environment variables

Passing parameters to the shell program by positional parameters

Using special shell variables, * and #

Using the SHIFT and read commands

1.1 Shell Programming Overview

A shell program is a common file that contains UNIX commands.

Permission for this file should be at least readable and executable.

You can execute the shell program by typing the file name at the shell prompt.

A shell program can accept data in three ways:

-Environment variables

-Command-Line arguments

-User input

The shell is a command interpreter that interprets and executes the commands entered at the command prompt. However, you may want to execute a set of commands multiple times, and the shell provides a feature that allows you to store this set of commands in a file, and then you can execute the file like any other program provided by the UNIX system, which is called a shell program or a shell script. When you run this file, it executes these commands just as you would enter them at the command line. In order for the shell to read and execute your shell program, the file permissions of the shell script must be set to readable and executable. To allow the shell to find your program, you can choose to enter the full pathname, or place the path of the script in the path list specified by your PATH environment variable. Many users create a bin directory in their home directory to store their own script, and then add $home/bin to their PATH environment variables. You can write very complex shell scripts, because shell scripts support complex structures such as variables, command-line arguments, interactive inputs, tests (judgments), branches (branches), and loops (loops).

1.2 Shell Program Example

$ cat Myprog

#this is the Myprog

Date

Ls–f

$ myprog

To create a shell program, consider the following steps:

$ VI Myprog A program that contains shell commands.

#this is the program Myprog

Date

Ls–f

$ chmod +x myprog Add File Execution mode

$ myprog

Thu June 11:10 EDT 1994

F1 F2 memo/myprog*

First, create a shell program Myprog using a http://www.aliyun.com/zixun/aggregation/18444.html ">" text editor. This file must be given the executable permission before the program executes. Then enter the program name at the command prompt, as shown in the example above, when Myprog executes, a child shell is created. This child shell reads input from the shell program file Myprog rather than from the command line, and the execution of each command in the shell creates a child shell. Once all the commands are executed, all the child shells are aborted and then returned to the original parent shell.

Comments in Shell programs:

It is recommended to provide annotation statements in the shell program to indicate the contents of the program. Comments start with a # symbol, and the shell does not execute any statements after #. #能够出现在命令行的任何位置.

Note: You should not name the shell program test because test is an internal shell command.

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.