Basic syntax and variables of shell programming

Time:2019-12-9

I. basic format of shell script writing

Take the simplesthello wordGive an example

.#!/bin/bash       
echo 'hello world'
  • . / bin / bash: tell the computer to use the bash interpreter to execute the code
  • Echo: printing

II. Running shell script

Method 1(recommended)

chmod 777 myshell.sh

			#Give file permission

			./myshell.sh

			#Running files

Method 2

sh myshell.sh

Three. Notes

Single-Line Comments
			The content of the content

			multiline comment
			:<

IV. defining variables

Basic grammar

1. Define variable: variable name = variable value
			2. Undo variable: unset variable name
			3. Declare static variable: readonly variable name ා static variable cannot be unset

			Return command execution result to variable
			1. A = ` LS - La ` back quote
			2. A = $(LS - LA) is equivalent to backquotes

			System variable:
			$PATH
			$HOME
			$PWD
			$SHELL
$USER

V. setting environment variables

Basic grammar

Export variable name = variable value output shell variable to environment variable
			Source profile name makes the modified configuration information take effect immediately
			Echo $variable value view the value of environment variable


			Give an example
			vim a.sh
			export a = 111
			:wq
			source a.sh
			echo $a
111

Vi. position parameter variable

Mainly pass parameters to shell script

$n (function description: n is a number, $0 represents the command itself, $1 - $9 represents the first to ninth parameters, more than ten parameters, more than ten parameters need to be enclosed in braces, such as )

			$* (function description: this variable represents all parameters in the command line, and $* takes all parameters as a whole)

			[email protected] (function description: this variable also represents all parameters in the command line, but [email protected] treats each parameter differently)


$(function description: this variable represents the number of all parameters in the command line)

Give an example

Content in a.sh script

#!/bin/bash  
			echo $2   
			#Show the second parameter
			echo $*   
			#This whole
			echo [email protected]   
			#Only in this separate for loop can cash be withdrawn
			echo $#   
#Number of parameters passed

implement

[[email protected] ~]# sh a.sh 1 2 3
			a.sh: line 1: .#!/bin/bash: No such file or directory
			2
			1 2 3
			1 2 3
3

7. Predefined variables

It is a variable that has been defined in advance by the shell designer and can be used directly in the shell script

$$(function description: process number (PID) of the current process)
			$! (function description: process number (PID) of the last process running in the background)
$? (function description: the return status of the last executed command. If the value of this variable is 0, it indicates that the last command executed correctly; if the value of this variable is non-zero (which number is determined by the command itself), it indicates that the last command failed to execute.)

Basic operation of variables

Operation expression

1. $((expression))  
			#$((1 + 1)) no spaces
			2. $[expression]   
			#$[1 + 1] no spaces
			The above two operation symbols are common, do not \ comment out


			This is special
			3. ` expr m + n ', ` expr 1 + 1' operators must have spaces between them 
			+   
			Jia Jia
			-   
			Subtraction
			/   
			To eliminate
			%   
			Remove the surplus
			\*  
			Multiply
			\(  \)  
Brackets

Recommended Today

PDO data object in PHP

PDO: PHP data object: the data object of PHP.PDO is a database operation tool class!1. It can operate many kinds of databases, such as mysql, Oracle, Sybase….2. It has more functions to operate the database, such as transactions.3. It can operate all kinds of data, but it has the same syntax: that is to use […]