Shell basics-an easy-to-use scripting language


definition

What is Shell

•Shell is not only a command language, but also a programming language.
•It is an environment that provides an interface to the linux (unix) system.
•Receives input from the user, executes the program based on the input, and displays the output of the program.
•You can run the user Commands, programs and shell scripts

For users, the shell is the most important utility. In-depth understanding and proficiency in the characteristics of the shell and how to use it is the key to making good use of the Unix/Linux system


Shell classification

• Just like there are different types of systems, there are different versions of the shell, each with its own set of commands and functions.
• The UNIX shell was written in the mid-1970s by Stephen Byrne, and AT&T Bell Labs in New Jersey

  1. Bourne shell prompt $

Bourne shell includes three types: Bourne shell (sh), Korn shell (ksh), and Bourne Again Shell.

  1. C shell prompt %

C shell includes two types: csh and tcsh


Shell script interpreter

View the system default shell
echo $SHELL

[root@single conf]# echo $SHELL
/bin/bash

Because bash is easy to use and free, it is widely used in daily work. At the same time, bash is also the default shell script interpreter for most Linux systems.

View the shells supported by the system
cat /etc/shells

[root@single conf]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

Shell specification

  1. The file name of the shell script has an extension of .sh, for example: test.sh
  2. Shell script files should be given execution permission (the file just created does not have execution permission)
    chmod u+x test.sh
  3. A standard Shell script will indicate which interpreter is used to execute the content of the script on the first line. The programming of this line in Linux is generally: #!/bin/bash
  4. Shell script execution
    method./test.sh

command

Console input/output

	echo [-n] "..."		控制台输出
	read VAR			控制台输入

case:

#!/bin/bash
echo -n  "please enter your name => "
read NAME
echo "your name is $NAME"

result:

[root@single shell]# ./test.sh 
please enter your name => jun
your name is jun

Annotation

	单行:#...
	多行::<<!...!

case:

#!/bin/bash
:<<!
echo -n  "please enter your name => "
read NAME
echo "your name is $NAME"
!
echo "break"
#echo -n "hello shell"

result:

[root@single shell]# ./test.sh 
break

variable

Syntax format: variable name = variable value

•数字、字母、下划线、一般字母大写
•值可以是一个数字、文本、文件名、设备或任何其他类型的数据,不可以是指针
•shell可以创建,分配和删除变量

Variable type

Types of meaning
Local variable Local variables are defined in scripts or commands and are only valid in the current shell instance. Programs started by other shells cannot access local variables.
Environment variable All programs, including those started by the shell, can access environment variables, and some programs need environment variables to ensure their normal operation. You can use the set command to view the current environment variables
shell variables Special variables set by the shell program. Some of the shell variables are environment variables and some are local variables. These variables ensure the normal operation of the shell.

Variable usage

  1. Local variable
创建
NAME="henry"
AGE=18
使用
echo "$NAME,$AGE"
echo $NAME
取消
unset NAME
常量
readonly NAME

case:

[root@single shell]# NAME="JUN"
[root@single shell]# AGE=22
[root@single shell]# echo "$NAME,$AGE"
JUN,22
[root@single shell]# unset NAME
[root@single shell]# echo "$NAME,$AGE"
,22

[root@single shell]# NAME="jun"
[root@single shell]# readonly NAME
[root@single shell]# echo $NAME
jun
[root@single shell]# NAME="kun"
-bash: NAME: readonly variable
  1. Environment variable

Global environment variables: Cross-instance access
Temporary environment variables: Temporary environment variables that are valid for the current login

export -p		#列出所有环境变量
export -n VAR	#删除环境变量VAR
export VAR		#添加环境变量VAR
[root@single shell]# export -p
declare -x CLASS_PATH=".:/opt/software/jdk180/lib/tools.jar:/opt/software/jdk180/lib/dt.jar"
declare -x HADOOP_HOME="/opt/software/hadoop/hadoop260"
declare -x HBASE_HOME="/opt/software/hadoop/hbase120"
···
[root@single shell]# export jun
[root@single shell]# export -p
···
declare -x jun
[root@single shell]# export -n jun
[root@single shell]# export -p
···
  1. shell variables
symbol meaning
$$ Current shell process number
$0 The file name of the current script
$n Script parameters
$# Number of script parameters
$* Complete set of script parameters
$@ Independent collection of script parameters
$? The status value of the execution result of the previous command or the return value of the function: 0 means normal, 1 means failed
$! The process number of the last background command
	$* 和 $@ 的区别
不加"两者相同
加上"则$*代表整体,而$@代表个体

Guess you like

Origin blog.csdn.net/weixin_48482704/article/details/110383862