Linux Shell Script Introductory Tutorial Series (2) The first shell script

This article is the second part of the Linux Shell Scripting Series . For more shell tutorials, please see: Linux Shell Scripting Series Tutorials

Through the study of the previous tutorial , I believe that everyone has been able to establish a general impression of the shell. Next, we will continue to learn more about the shell through the simplest script.

 

New shell script

Create a new file with the extension sh (sh stands for shell), or any other name. In fact, the extension does not affect the execution of the script. It is good to know the meaning of the name. The sh is used here for easy identification.


Enter the following in your newly created file:

#!/bin/bash
echo "Hello World !"

 

"#!" is a convention mark, which tells the system what interpreter this script needs to execute, that is, which shell to use to execute it.

The echo command is used to output text to the window.

 

run shell script

There are two ways to run a shell script.

 

1) As an executable program

Save the above code as test.sh and cd to the corresponding directory:

chmod +x ./test.sh #Make the script have execute permission
./test.sh #Execute the script

 

Note that it must be written as ./test.sh, not test.sh.

 

The same is true for running other binary programs, write test.sh directly, the Linux system will go to the PATH to find out if there is a test.sh, and only /bin, /sbin, /usr/bin, /usr/sbin, etc. are in the PATH , your current directory is usually not in the PATH, so if you write test.sh, the command will not be found. Use ./test.sh to tell the system that you are looking for it in the current directory.

 

To run a bash script in this way, the first line must be written correctly so that the system can find the correct interpreter.

 

2) as an interpreter parameter

This mode of operation is to run the interpreter directly, and its parameter is the file name of the shell script, such as:

/bin/sh test.sh
/bin/php test.php

 

A script running in this way does not need to specify the interpreter information on the first line, and it is useless to write it.

Let's look at another example. The following script uses the read command to take input from stdin, assign it to the PERSON variable, and finally output on stdout:

#!/bin/bash
# Author : linuxdaxue.com
echo "What is your name?"
read PERSON
echo "Hello, $PERSON"

 

Run the script:

chmod +x ./test.sh
$./test.sh
What is your name?
linuxdaxue
Hello, linuxdaxue
$

 

Summarize

Do you see that the students here have a deeper understanding of shell scripting? Then hurry up and practice it yourself according to the above method. If there is something you don’t understand, you can find it in time and solve it in time, so that you can truly turn knowledge into your own ability.

 

Original: Linux Shell Series Tutorial (2) The First Shell Script

This article is transferred from: Linux Shell Scripting Tutorial Series (2) The first shell script

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326635990&siteId=291194637