The basic syntax of linux shell programming

The basic syntax of linux shell programming

  Shell is an interface for interactive operation between users and the kernel. Currently, the most popular Shell is called bash Shell. Shell is also a programming language <interpreted programming language> , that is, shell script <is programming with Linux shell commands>. A system can have multiple shells. You can use the cat/etc/shells command to view the shells installed in the system. Different shells may support different command syntax.

Basic format

  The code is written in a normal text file, usually with .sh as the suffix

vi intsmaze.sh

#!/bin/bash ## Indicates which shell parser is used to parse and execute our script program
echo "hello world" ## Comments can also be written here
## This is a line of comment

Execute script

sh intsmaze.sh

Or add x permission to the script and execute it directly, otherwise it can only be executed through sh xxx.sh

chmod 755 intsmaze.sh

./intsmaze.sh

Basic syntax

System variable

Variables in Linux Shell are divided into "system variables" and "user-defined variables". You can view system variables through the set command.

[hadoop@centos-Reall-131 shtest]$ set
BASH=/bin/bash
HADOOP_HOME=/home/hadoop/hadoop-2.6.4
HISTCONTROL=ignoredups
HISTFILE=/home/hadoop/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/hadoop
HOSTNAME=centos-Reall-131
HOSTTYPE=i386
ID=500
JAVA_HOME=/home/hadoop/app/jdk1.7.0_65
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=24
LOGNAME=hadoop

Access system variables: $HOME, $PWD, $SHELL, $JAVA_HOME, etc.

Custom variable

  1. Grammar

  Variable = value (for example, STR=abc)

  No spaces on both sides of the equal sign

  Use variable: $arg

   There is a difference between double quotation marks and single quotation marks: double quotation marks only escape spaces in the string; single quotation marks will escape variables in the string, such as $param.

  2. Example

[hadoop@centos-Reall-131 ~]$ STR=hello world 
-bash: world: command not found
[hadoop@centos-Reall-131 ~]$ STR="hello world"
[hadoop@centos-Reall-131 ~]$ A=9
[hadoop@centos-Reall-131 ~]$ echo $A
9
[hadoop@centos-Reall-131 ~]$ echo $STR
hello world

What if I want to print hello worlds is greater?

Is echo $STRs is greate OK?

No, the correct way of writing is:

echo ${ STR}s is greate

[hadoop@centos-Reall-131 ~]$ echo ${STR}s is greate
hello worlds is greate
[hadoop@centos-Reall-131 ~]$ echo'${STR}s is greate'
${STR}s is greate
[hadoop@centos-Reall-131 ~]$ echo "please$STR" 
pleasehello world
[hadoop@centos-Reall-131 ~]$ echo'please$STR'
please$STR

unset A Unset variable A

readonly B=2 declares the static variable B=2 and cannot be unset

export A #The    variable can be promoted to the global environment variable in the current shell process, which can be used by other subshell programs

 vi a.sh

#!/bin/bash
a="a in a.sh"
echo $a
/root/scripts/b.sh

vi b.sh

#!/bin/bash
b="b in b.sh"
echo $b
echo $a

Then execute ./a.sh and you will find that the a variable defined in the a script is not printed out in the b script.

If you want to print the variable a of the a script in b, you need to export the variable a in the a script. At this time, the a variable becomes the global variable of the bash process where the a.sh script is located, and all the child processes of the process All have access to variable a.

another way:

If you call b.sh in the a.sh script as follows

. ./b.sh     ## Note: Pay attention to the first "."

or

source ./b.sh ##

Then, b.sh runs in the bash process space where a.sh is located, instead of running in a child process of the a process.

summary:

1. Invoking b.sh directly from a.sh will make b.sh execute in the "child process" space of the bash process where a is located

2. The child process space can only access the variables defined by export in the parent process

3. A shell process cannot promote variables defined by itself to the parent process space

4. When the "." sign executes the script, the script will be executed in the shell process space where the caller is located

3. Backquote assignment

A=`ls -la` ## Backquote, run the command inside, and return the result to the variable A

A=$(ls -la) ## Equivalent to backticks

4. Special variables

$? indicates the status code of the previous command exit

$$ represents the current process number

$0 represents the current script name

$n represents the input parameter at position n (n represents a number, n>=1)

$# indicates the number of parameters, often used in loops

Both $* and $@ represent the parameter list

Note: The difference between $* and $@

Both $* and $@ represent all parameters passed to the function or script

ü When not enclosed by double quotes ""——

Both $* and $@ form a parameter list in the form of $1 $2… $n

ü When they are enclosed in double quotes ""——

"$*" will take all the parameters as a whole, forming a whole string in the form of " $1 $2… $n " ;

"$@" will separate each parameter and form a parameter list in the form of "$1" "$2"… "$n"