Learning shell script programming (Issue 1) _ writing and modifying permissions and executing Shell programs

Source: Internet
Author: User

  • Write shell program
  • Execute shell program

Shell programs have many features similar to C and other programming languages, but they are not as complex as programming languages. A shell program is a series of Linux commands and utilities stored in a file. During execution, each command is interpreted and executed one by one through the Linux operating system. First, write the first shell program to learn how to write, modify permissions, and execute the shell program.

2.1 edit shell program

Edit a source program with the following content, save the file name as date, and save it to the directory/bin.

[[Email protected] bin] # VI Date

#! /Bin/sh

Echo "mr. $ user, today is :"

Echo & date "+ % B % d %"

Echo "Wish you a lucky day! "

Note:

#! Bash is used to explain/bin/sh notifications. If you execute the shell command date in the ECHO statement, you need to add the symbol "&" before the date command, where % B % d % A is the input format controller.

2.2 establish an executable program

After editing the file, you cannot execute the file immediately. You must set executable program permissions for the file. Run the following command.

[[Email protected] bin] # chmod + x date

2.3 run the shell program

There are three methods to execute a shell program:

Method 1:

[[Email protected] bin] #./date

Mr. Root, today is:

Tuesday, February 06

Wish you a lucky day!

Method 2:

Another method for executing date is to pass it as a parameter to the shell command:

[[Email protected] bin] # bash date

Mr. Root, today is:

Tuesday, February 06

Wish you a lucky day!

Method 3:

To compile and execute shell programs in any directory, add the/bin directory to the entire environment variable.

The procedure is as follows:

[[Email protected] root] # export Path =/bin: $ path

[[Email protected] bin] # date

Mr. Root, today is:

Tuesday, February 06

Wish you a lucky day!

Example 1: Write a shell program mkf. The function of this program is to display the file information under the root, create a KK folder, and create a file AA under this folder, the permission to modify this file is executable.

Analysis: In this shell program, the following commands must be executed in sequence:

Go to the root directory: CD/root

Display the file information in the root directory: ls-l

Create a folder named KK: mkdir kk

Go to the root/KK Directory: CD kk

Create a new file AA: VI aa # Save it manually after editing

The permission to modify the AA file can be executed: chmod + x AA

Go back to the root directory: CD/root

Therefore, this shell program is only a set of the preceding commands. Assume that the program is named mkf.

 

[[Email protected] root] # vi mkf

CD/root

Ls-l

Mkdir kk

CD kk

VI AA

Chmod + x AA

CD/root

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.