Variable 4 in shell script

1. Definition of variables
A variable is the address of an area of memory
Meaning of variables
The command cannot manipulate the value that changes all the time. Using a fixed string of characters to represent an unfixed target can solve this problem
2. Variable definition method in shell script
Environment level: the variable becomes invalid after the environment is shut down
The current SH has this variable A. after running the script, run the new sh to execute the script. The new SH has no variable a

[root@docker3 mnt]# a=1
[root@docker3 mnt]# cat test.sh 
#!/bin/sh
echo $a
[root@docker3 mnt]# sh test.sh 
[root@docker3 mnt]# echo $a
1
[root@docker3 mnt]# . test.sh 
1

Variable a can be identified in all child processes

[root@docker3 mnt]# export a=1
[root@docker3 mnt]# sh test.sh 
1

When you exit and enter again, the system resources are recycled and a process is restarted, so the variable a is gone

[root@docker3 mnt]# logout
[root@docker3 ~]# cd /mnt/
[root@docker3 mnt]# sh test.sh 
[root@docker3 mnt]# echo $a

The permanent setting of the system is that the files can be found on the hard disk, and what is not is temporary
User level

[root@docker3 mnt]# vim ~/.bash_profile

[root@docker3 mnt]# source ~/.bash_profile 
[root@docker3 mnt]# sh test.sh 
1
[root@docker3 mnt]# exit
[kiosk@foundation38 Desktop]$ ssh root@172.25.254.3
[root@docker3 ~]# sh /mnt/test.sh 
1
[root@docker3 ~]# su - yan
[yan@docker3 ~]$ sh /mnt/test.sh 

System level
Main configuration file, but generally do not move it (it will be troublesome to maintain)

[yan@docker3 ~]$ vim /etc/profile

When the sub configuration file is changed, the effect is the same

[yan@docker3 ~]$ vim /etc/profile.d/a.sh
[root@docker3 ~]# cat /etc/profile.d/a.sh 
#!/bin/bash
export a=1
[root@docker3 ~]# su - yan
[yan@docker3 ~]$ sh /mnt/test.sh 
1

Variable name
Alphanumeric underline
Cannot start with a number
Suggestion: use capital characters for short names and long names_ Distinguish word class
YAN
Yan_Linux
yaN_Linux

[root@docker3 ~]# a=1
[root@docker3 ~]# a1=1
[root@docker3 ~]# a-1=1
-bash: a-1=1: command not found
[root@docker3 ~]# a_=1
[root@docker3 ~]# a_1=1
[root@docker3 ~]# 1a=1
-bash: 1a=1: command not found
[root@docker3 ~]# _a=1

Changes to user environment variables
Linux programming of java

[root@docker3 mnt]# vim yan.java
[root@docker3 mnt]# cat yan.java 
public class yan{
public static void main(String args[]){
system.out.println("hello yan")
}
}

Install java

[root@docker3 mnt]# yum install java-11-openjdk.x86_64 java-11-openjdk-devel.x86_64 -y

All commands run by Java are here, and there are commands run by Java under bin

[root@docker3 mnt]# cd /usr/lib/jvm
[root@docker3 jvm]# ls
java     java-11-openjdk                          java-openjdk  jre-11          jre-11-openjdk-11.0.ea.28-7.el7.x86_64
java-11  java-11-openjdk-11.0.ea.28-7.el7.x86_64  jre           jre-11-openjdk  jre-openjdk
[root@docker3 jvm]# cd jre-openjdk/
[root@docker3 jre-openjdk]# ls
bin  conf  include  legal  lib  release  tapset
[root@docker3 jre-openjdk]# cd bin/
[root@docker3 bin]# ls
jaotc      java     javap     jdb        jhsdb   jjs    jmod        jshell  jstatd   rmic         serialver
jar        javac    jcmd      jdeprscan  jimage  jlink  jps         jstack  keytool  rmid         unpack200
jarsigner  javadoc  jconsole  jdeps      jinfo   jmap   jrunscript  jstat   pack200  rmiregistry

ls is a relative path, because the system will retrieve the path during execution

[root@docker3 bin]# ll /bin/ls
-rwxr-xr-x. 1 root root 117680 Jun 15  2018 /bin/ls
[root@docker3 bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@docker3 ~]# vim /etc/profile.d/java.sh
[root@docker3 ~]# cat /etc/profile.d/java.sh 
#!/bin/sh
export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export PATH=$PATH:$JAVA_HOME:/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib
[root@docker3 ~]# source /etc/profile.d/java.sh 
[root@docker3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/lib/jvm/jre-openjdk:/bin

test.sh becomes a system command

[root@docker3 ~]# test.sh
-bash: test.sh: command not found
[root@docker3 mnt]# cat /etc/profile.d/java.sh 
#!/bin/sh
export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export PATH=$PATH:$JAVA_HOME:/bin:/mnt
export CLASSPATH=.:$JAVA_HOME/jre/lib
[root@docker3 ~]# source /etc/profile.d/java.sh 
[root@docker3 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/lib/jvm/jre-openjdk:/bin:/mnt
[root@docker3 mnt]# test.sh
1
[root@docker3 ~]# test
test      testgdbm  test.sh   
[root@docker3 ~]# test.sh 
1

3. Translation of variables
If you want to display ¥ 5, the variable 5 is displayed

[root@docker3 ~]# echo $5

\Only one character can be translated at a time

[root@docker3 ~]# echo \$5
$5
[root@docker3 ~]# echo \$$5
$
[root@docker3 ~]# echo \$\$5
$$5

Batch translation (quotation)
Double quotation marks, weak references, can only refer to some resources: $! \` Cannot reference
You can quote any string, single quotation mark, strong quotation mark

[root@docker3 ~]# echo "$$$"
3989$
[root@docker3 ~]# echo "!!!!!!"
echo "echo "$$$"echo "$$$"echo "$$$""
echo 3989echo 3989echo 3989
[root@docker3 ~]# echo "```"
>
[root@docker3 ~]# echo "\\\\\"
> 
[root@docker3 ~]# echo '$$$$$5'
$$$$$5
[root@docker3 ~]# echo '```5'
```5

variable declaration
Because ab is regarded as a variable, it should be that a is a variable and b is not

[root@docker3 ~]# a=1
[root@docker3 ~]# echo $ab
[root@docker3 ~]# echo ${a}b
1b

Array of variables

[root@docker3 ~]# a=(1 2 3 4 5 6)
[root@docker3 ~]# echo $a
1
[root@docker3 ~]# echo ${a[0]}
1
[root@docker3 ~]# echo ${a[1]}
2
[root@docker3 ~]# echo ${a[-1]}
6
[root@docker3 ~]# echo ${a[@]}
1 2 3 4 5 6
[root@docker3 ~]# echo ${a[*]}
1 2 3 4 5 6

*Represents a string of characters @ represents 6 strings of characters

[root@docker3 ~]# "1 2 3 4 5 6"
[root@docker3 ~]# "1" "2" "3" "4" "5" "6"

Represents the number of elements

[root@docker3 ~]# echo ${#a[@]}
6
[root@docker3 ~]# unset a[5]
[root@docker3 ~]# echo ${#a[@]}
5
[root@docker3 ~]# echo ${a[@]#}
1 2 3 4 5
[root@docker3 ~]# a[5]=6
[root@docker3 ~]# echo ${a[@]#}
1 2 3 4 5 6
[root@docker3 ~]# echo ${#a[@]}
6
[root@docker3 ~]# echo ${a[@]:0:3}
1 2 3
[root@docker3 ~]# unset a
[root@docker3 ~]# echo $a

4. Alias setting of commands in Linux
Similarly ls, some have colors and some don't

Check the parameters to know that ls has added the color parameter, / bin/sh has not

[root@docker3 ~]# alias | grep ls
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'

Temporary settings

[root@docker3 ~]# alias xie="vim"
[root@docker3 ~]# xie yan
[root@docker3 ~]# exit
[kiosk@foundation38 Desktop]$ ssh root@172.25.254.3
[root@docker3 ~]# xie
-bash: xie: command not found

User level

[root@docker3 ~]# vim ~/.bashrc 

All user levels of the system

[root@docker3 ~]# vim /etc/bashrc 


Stop xie
Delete the previous user level and system level alias = 'vim'
There is still xie, because the source is an incremental environment

[root@docker3 ~]# source ~/.bashrc 
[root@docker3 ~]# source /etc/bashrc 
[root@docker3 ~]# xie 

That's it

[root@docker3 ~]# unalias xie
[root@docker3 ~]# xie
-bash: xie: command not found

Use the command execution result to set the value of the variable

[root@docker3 ~]# TIME=date
[root@docker3 ~]# echo $TIME
date
[root@docker3 ~]# echo TIME=$(date)
TIME=Mon Feb 21 01:30:08 CST 2022
[root@docker3 ~]#  TIME=`date`
[root@docker3 ~]# echo $TIME
Mon Feb 21 01:30:53 CST 2022

Parameter passing in script
Non interactive parameter transfer

[root@docker3 ~]# cat test.sh 
#!/bin/sh
echo '$0' is $0
echo '$1' is $1
echo '$2' is $2
echo '$3' is $3
echo '$*' is $*
echo '$@' is $@
echo '$#' is $#
[root@docker3 ~]# sh test.sh 
$0 is test.sh
$1 is
$2 is
$3 is
$* is
$@ is
$# is 0

@#Represents the number of strings followed by the script

[root@docker3 ~]# sh test.sh yan
$0 is test.sh
$1 is yan
$2 is
$3 is
$* is yan
$@ is yan
$# is 1

¥ * and ¥ @ represent all characters, stars represent a string of characters, and @ represents three strings of characters

[root@docker3 ~]# sh test.sh yan wei qiu
$0 is test.sh
$1 is yan
$2 is wei
$3 is qiu
$* is yan wei qiu
$@ is yan wei qiu
$# is 3

Interactive parameter transmission

[root@docker3 ~]# cat test.sh 
#!/bin/sh
read -p "please input world:" WORLD
echo $WORLD

Script function
Output red font

[root@docker3 ~]# cat test.sh 
#!/bin/sh
echo -e "\033[31mhello yan\033[0m"

But you need to output colored fonts many times, and it's troublesome to write them every time

[root@docker3 ~]# cat test.sh 
#!/bin/sh
ECHO()
{
echo -e "\033[$1m$2\033[0m"
}
ECHO 31 "hello yan"
ECHO 32 "hello wei"


Run once and finish it. Want to execute it all the time

[root@docker3 ~]# cat test.sh 
#!/bin/sh
read -p "please input world:" WORLD
echo $WORLD
[root@docker3 ~]# sh test.sh
please input world:yan
yan
[root@docker3 ~]# cat test.sh 
#!/bin/sh
ACTION()
{
  read -p "please input world:" WORLD
  [ "$WORLD" = "exit" ] && {
  exit
 }||{
  echo $WORLD
  ACTION
  }
}
ACTION
[root@docker3 ~]# sh test.sh
please input world:yan
yan
please input world:wei
wei
please input world:qiu
qiu
please input world:exit

Keywords: Linux CentOS shell bash

Added by ChrisDarl on Sun, 20 Feb 2022 15:44:40 +0200