Learning big data -- variables and operators in Shell

Keywords: shell vim

Variables in Shell

System variables

  1. Common system variables
    $HOME, $PWD, $SHELL, $USER, etc
    2. Case practice
    (1) View the value of the system variable
[root@hadoop100 learnshell]# echo $HOME
/root

(2) Display all variables in the current Shell: set

[root@hadoop100 learnshell]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
...

Custom variable

  1. Basic grammar
    (1) Define variable: variable = value
    (2) Undo variable: unset variable
    (3) Declare static variable: readonly variable, note: cannot unset
    (the readonly variable is declared under the current bash program. When you log in again and run a bash program again, this variable will not work, which is equivalent to starting multiple programs.)
    2. Variable definition rules
    (1) Variable names can consist of letters, numbers, and underscores, but they cannot start with numbers. It is recommended that environment variable names be capitalized.
    (2) No spaces on both sides of equal sign
    (3) In bash, the default type of variables is string type, which can not be directly used for numerical operation.
    (4) If the value of a variable has spaces, you need to use double or single quotes.
    3. Case practice
    (1) Define variable A
[root@hadoop100 learnshell]# a=5
[root@hadoop100 learnshell]# echo $a
5
(2) Reassign variable A
[root@hadoop100 learnshell]# a=8
[root@hadoop100 learnshell]# echo $a
8

(3) Undo variable A
[root@hadoop100 learnshell]# unset a
[root@hadoop100 learnshell]# echo $a

[root@hadoop100 learnshell]# 

(4) Declare static variable B=2, cannot unset
[root@hadoop100 learnshell]# readonly b=2
[root@hadoop100 learnshell]# echo $b
2
[root@hadoop100 learnshell]# b=9
-bash: b: readonly variable
[root@hadoop100 learnshell]# unset b
-bash: unset: b: cannot unset: readonly variable

(5) In bash, the default type of variables is string type, so it is impossible to perform numerical operation directly
[root@hadoop100 learnshell]# c=1+1
[root@hadoop100 learnshell]# echo $c
1+1

(6) If there is a space for the value of a variable, it needs to be enclosed in double quotation marks or single quotation marks

[root@hadoop100 learnshell]# d=i love shell
-bash: love: command not found
[root@hadoop100 learnshell]# d="i love shell"
[root@hadoop100 learnshell]# echo $d
i love shell

(7) It can promote variables to global environment variables, which can be used by other Shell programs
export variable name

[root@hadoop100 learnshell]# vim testexport.sh

Add echo $B to helloworld.sh

#!/bin/bash
echo "testexport"
echo $B

[root@hadoop100 learnshell]# B=1
[root@hadoop100 learnshell]# bash testexport.sh 
testexport

The value of the printout variable B was not found.

[root@hadoop100 learnshell]# export B
[root@hadoop100 learnshell]# bash testexport.sh 
testexport
1

Special variable: $n

  1. Basic grammar
    $n (function description: n is a number, $0 represents the script name, $1 - $9 represents the first to ninth parameters, more than ten parameters, more than ten parameters need to be enclosed in braces, such as )
  2. Case practice
    (1) Output the script file name, input parameter 1, and input parameter 3 values
[root@hadoop100 learnshell]# vim testparameter.sh
#!/bin/bash
echo "$0  $1   $3"
[root@hadoop100 learnshell]# bash testparameter.sh 1 2 3
testparameter.sh 1 3

Special variable:$#

1. Basic grammar
Function Description: get the number of all input parameters, commonly used in loops.
2. Case practice
(1) Get the number of input parameters

[root@hadoop100 learnshell]# vim testparameter.sh
#!/bin/bash
echo "$0  $1   $3"
echo $#
[root@hadoop100 learnshell]# bash testparameter.sh 1 2 3
testparameter.sh 1 3
3

Special variables: $*, $@

1. Basic grammar
$* (function description: this variable represents all parameters in the command line, $* treats all parameters as a whole)
$@ (function description: this variable also represents all parameters in the command line, but $@ treats each parameter differently)
2. Case practice
(1) Print all parameters entered

[root@hadoop100 learnshell]# vim testparameter.sh

#!/bin/bash
echo "$0  $1   $3"
echo $#
echo $*
echo $@
[root@hadoop100 learnshell]# bash testparameter.sh 1 2 3
testparameter.sh 1 3
3
1 2 3
1 2 3

Special variable: $?

  1. Basic grammar
    $? (function description: the return status of the last executed command. If the value of this variable is 0, the last command is executed correctly; if the value of this variable is non-zero (which number is determined by the command itself), the last command is executed incorrectly.)
  2. Case practice
    (1) Determine whether the helloworld.sh script executes correctly
[root@hadoop100 learnshell]# bash testparameter.sh 1 2 3
testparameter.sh 1 3
3
1 2 3
1 2 3
[root@hadoop100 learnshell]# echo $?
0

operator

  1. Basic grammar
    (1) "$((expression))" or "$[expression]"
    (2) expr +, -, *, /,% plus, minus, multiply, divide, take remainder
    Note: spaces are required between expr operators
  2. Case practice:
    (1) Calculate the value of 3 + 2
[root@hadoop100 learnshell]# expr 2 + 3
5

(2) Calculate the value of (2 + 3) X4
(a) expr one step calculation

[root@hadoop100 learnshell]# expr `expr 2 + 3` \* 4
20

(b) Adopt $[expression] mode

[root@hadoop100 learnshell]# S=$[(2+3)*4]
[root@hadoop100 learnshell]# echo $S
20

Quoted from the courseware of Silicon Valley

Published 61 original articles, won praise 7, visited 1262
Private letter follow

Posted by chacha102 on Tue, 25 Feb 2020 00:15:27 -0800