first shell script

Shell environment

Shell programming is the same as java and php programming, as long as there is a text editor that can write code and a script interpreter that can interpret and execute.

There are many types of Linux shells, the common ones are:

  • Bourne Shell(/usr/bin/sh或/bin/sh)
  • Bourne Again Shell(/bin/bash)
  • C Shell(/usr/bin/csh)
  • K Shell(/usr/bin/ksh)
  • Shell for Root(/sbin/sh)
  • ……

Bash, also known as Bourne Again Shell, is widely used in daily work due to its ease of use and free. At the same time, Bash is also the default shell of most Linux systems.

In general, people don't distinguish between Bourne Shell and Bourne Again Shell, so, like  #!/bin/sh , it can also be changed to  #!/bin/bash .

#! tells the system that the program specified by the following path is the shell program that interprets the script file.

 

first script

Open a text editor (you can use the vi/vim command to create a file), create a new file test.sh with the extension sh (sh stands for shell), the extension does not affect the execution of the script, it is good to know the name, if you Use php to write shell scripts, and use php for the extension.

Enter some code, the first line is generally like this:

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

#! is a convention mark that tells the system what interpreter this script needs to execute, i.e. what kind of shell to use.

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

 

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 file called test.sh, but 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, you will not find the command, use ./test.sh to tell the system to say , just look in the current directory.

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.

 

From: rookie tutorial

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325286421&siteId=291194637