Getting Started with Shell programming (i)

Source: Internet
Author: User

There are three ways to run a Linux program:

(1), so that the file has executable permissions to run the file directly.

(2), directly invoke the command interpreter execution program (Linux Shell is an interpreted language).

(3). execute file using source

The third one runs differently than the first two. Let's look at a program small chestnut, file name:echo.sh:

#! /bin/SHCD tmpecho"helloworld"

This is a completed, executable Linux shell program. Let's run the program to see the results of the operation:

 [[email protected] shtest]#  # View current working directory /home/jason/shtest #返回当前工作 catalog [[email protected] shtest]#  chmod  +x echo . sh   /echo . sh                                                                   #运行echo. sh file Hello World #返回的运行结果 [[email protected] shtest]#  pwd /home/jason/shtest #工作目录没有发生改变 [[email protected] shtest]#  

From the above output you will see that the program is running. Let's take a look at the contents of this file, first the first line #!/bin/sh

When the command line executes the program, first determine whether the program has executable permissions. If you do not have sufficient permissions, the machine will tell the user: "Insufficient permissions." From a security standpoint we can also understand that any program running on the machine should first be judged whether the user executing the program has this permission. In the above three ways to run the program, we directly execute the file requires that the file has executable permissions.

Where the chmod command modifies the file permissions, the +x parameter causes the executable to have enforceable permissions. When the command-line shell receives our execution command and determines that we have executable permissions, it calls the Linux kernel command to create a new (fork) process. Call our command in the new process. The Linux kernel knows how to execute a file if this is a command-type compiled (binary) file. But our echo.sh is a file file, not a binary file (compiled file) The kernel doesn't know how to run it. The kernel doesn't know how to run it, but the shell knows it's a script file, so the shell launches a new shell process to execute the program. Now that Linux is used for several shells, it's time for this program to choose that shell, when #!/bin/sh to work, he specifies what process the shell will create to execute the script. In this example, we created a/bin/sh (standard shell) process to execute the script. His execution is the command line sweeping through the first line and discovering #! After reading the characters behind him, search the full path of the interpreter. If this line has parameters, it will be read together. This program is run first through the first judgment, if not directly can run the file, you will see whether it is the second mode of operation (by invoking the command line interpreter), just echo.sh file can be run by the command line interpreter. In these three methods of execution, if the #! The shell interpreter specified in is the same as the second specified shell interpreter, and the result of the execution is the same for each of the two.

If you do not know the exact path of the interpreter, you can query through Whereis: $>whereis  bash/bin/bash

Here's a look at the third way to execute the program:

pwd                    #查看当前工作目录/home/jason/echo. SH pwd/tmp                                                                               

How did the working directory change?? Let's see what the reasons are:

Before we solve the above problem, let's look at the Shell's command type:

There are three types of commands that the Linux shell can execute: Built-in commands, shell functions, and external commands

(1)), the built-in command is the command contained in the shell program itself. These commands are integrated into the shell interpreter, for example, almost all shell command interpreters contain CD built-in commands to change the working directory. Part of the built-in command exists to change the shell's own property settings, and there is no process creation or extinction when executing the built-in command. Another common built-in command is common I/O commands, such as: Echo

(2), Shell function is a series of program code, is written in the shell language. It can be quoted as if it were his command.

(3), external command is a shell-independent executable program. such as find,grep,echo.sh. When the command-line Shell executes an external command, it creates a copy process of the current shell to execute. There is a process creation and extinction during execution.

The external command executes as follows:

Refer to the following: http://www.cnblogs.com/stephen-liu74/archive/2011/12/20/2285454.html

Getting Started with Shell programming (i)

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.