[Shell] How Shell scripts are executed, and how shell scripts are executed

Source: Internet
Author: User
Tags echo command

[Shell] How Shell scripts are executed, and how shell scripts are executed

Bytes ----------------------------------------------------------------------------------------------------------

Command to complete most output tasks: echo

Echo helloworld; # correct

Echo "hello world"; # correct

Echo "hello world! "; # A space must be enclosed in quotation marks! In Linux, double quotation marks are not allowed, but single quotation marks are used (NOTE: Some Linux systems can output normally, but this statement is not recommended)

Echo 'Hello world! '; # Correct

 

Command: help echo or man echo # view the echo Command help

 

------------------------------------- @ Blackeye poet <www. chenwei. ws> ---------------------------------

I. the first Shell script:

Vi hello. sh # Linux scripts do not differentiate extensions, but end with. sh to tell the system to write a Shell script. A syntax color prompt is displayed when you use vim to open the script.

 

#! /Bin/Bash # mark the following program as a Shell script. In addition to this sentence, all other statements starting with # indicate comments.

# The first program # Is a comment

# Author chenwei # Annotation

Echo-e "chenwei is Black eyed poet" # Content

 

2. Two Methods for executing Shell scripts:

1. Grant the execution permission to run directly

Chmod 755 hello. sh

./Hello. sh # It can be executed in absolute or relative paths.

 

2. Execute the script through bash call

Bash hello. sh # execute bash directly without the execution permission.

 

Iii. tips:

Cat-A hello. sh # Add the-A option to view the complete script content, including hidden characters

# Run the last command to view the Shell script edited in Linux. The carriage return value is $, but for the Shell script edited in Windows, the carriage return value is ^ M $, in Linux, if you want to execute the edited script in Windows, an error will be reported (the file or directory does not exist ).

Solution:

Command: dos2unix hello. sh # convert from dos to unix. If this command is not installed in your system: yum-y install dos2unix, similarly: yum-y install unix2dos

Bytes --------------------------------------------------------------------------------------------------------


What are the main ways to execute shell scripts?

(1) input direction to Shell script
$ Bash <Script Name

(2) Use the script name as the parameter
$ Bash Script Name (parameter 〕

(3) set the Shell script permission to executable, and then execute it directly at the prompt.
$ Chmod a + x Script Name
$ PATH = & PATH :.
$ Script Name

How to run shell scripts

You can run a shell script (such as test) in two ways: 1. $ sh test generally does not use this method, in particular, the "sh <test" Call method is not used, because this method will prohibit shell from reading standard input. You can also use $ ksh test to require shell to have "readable" access permissions. 2. Before directly running an executable shell script, use the following chmod command to set the shell script file to executable. Chmod 755 test (except the file owner can be written, each user has read and executable access permissions) chmod + rx test (same as above) chmod u + rx test (only the file owner has the read and execution permissions). After setting the shell script file access permissions according to the preceding requirements, you can use the following methods, run the shell script directly. 1. test (if the command search path contains the current directory) 2 ,. /test (if the command contraction path does not contain the current directory) * Note: calling a shell via sh test may disable some shell-specific extension functions, therefore, the script may fail to be correctly executed.

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.