The -11-bash variable script of the ma series reads the configuration file

 

 

0 bash variable type:
 environment variable
 local variable (local variable)
 location variable: $1, $2, ... respectively represent the first parameter, the second parameter ....
 special variable

 $?: Status code of last command execution result
 $#: Number of parameters
 $*: Parameter list
 $@: Parameter list

 

 

1 Reference after variable assignment:

${varname} where {} can be omitted, as long as it will not cause confusion when used, then {} can be omitted

 eg:

[root@h2sliver114 local]# name=zm
 [root@h2sliver114 local]# echo "hello ${name}s." At this time, it is recommended to add {}

 

 

2 Introduction to specific variable types:

 

local variable:
set VARNAME=VALUE: The scope is the entire bash process;


local variable:
local VARNAME=VALUE: the scope is the current code segment;


Environment variables: The scope is the current shell process and its subprocesses;
export VARNAME "export"


position variable:
$1, $2, ...


Special variables:
$?: The return value of the execution status of the previous command;
Program execution, there may be two types of return values:
 program execution result
 Program status return code (0-255)
 0: correct execution
 1-255: Error execution, 1, 2, 127 system reservation;

  

 

 3 View the environment variables in the current shell:
printenv This is the customary
env
export

 

 

 

 4 Position variables:

 

eg:

sh filetest.sh /etc/fstab /etc/inittab
$1: /etc/fstab
$2: /etc/inittab

 

5 shift use: 

shift 2 means to kick out 2 at a time to get the last of the two kicked out by default is to move 1

 

 Use Cases:

#!/bin/sh
#
echo "the first param is: $1"
shift
echo "the second param is: $1"
shift
echo "the third param is: $1"

result:
[root@chinadaas109 zhoumingtest]# sh fortest.sh  1 2 3
the first param is: 1
the second param is: 2
the third param is: 3

 

 6 Variables are assigned initial values:

 

${parameter:-word}: If parameter is empty or undefined, the variable is expanded to "word"; otherwise, it is expanded to the value of parameter; the so-called expansion is to print the result directly
${parameter:+word}: If parameter is empty or undefined, do nothing; otherwise, expand to "word" value;
${parameter:=word}: If parameter is empty or undefined, the variable is expanded to "word", and the expanded value is assigned to parameter;
${parameter:offset} Get the data from the beginning of the array script offset to the last character
${parameter:offset:length}: Take the substring, starting from the next character at offset (the index starts from 0), and take the substring with length;

Use Cases:
[root@chinadaas11 ~]# a=${a:-2}
[root@chinadaas11 ~]# echo $a
2
[root@chinadaas11 ~]# echo ${a:=30}
30
[root@chinadaas11 ~]# echo $a
30
[root@chinadaas11 ~]# a='hello world'
[root@chinadaas11 ~]# echo ${a:1:4}
it
[root@chinadaas11 ~]# echo ${a:1}
it world

 

7 Script to read configuration file

 

Usually in the following directory:
/etc/rc.d/init.d/service script
Service script support configuration file: /etc/sysconfig/configuration file with the same name as service script

How to make the script use the configuration file:   
[root@chinadaas11 zm]# ll
total 8
-rw-r--r-- 1 root root 10 Nov  2 14:48 a.conf
-rw-r--r-- 1 root root 60 Nov  2 14:54 test.sh
[root@chinadaas11 zm]# cat a.conf
name='zm'
[root@chinadaas11 zm]# cat test.sh  
#!/bin/bash
#
./root/zm/a.conf ---> Equivalent to source /root/zm/a.conf Read and execute the commands in the file in the current bash environment

[ -n $name ] && echo $name
[root@chinadaas11 zm]# sh  test.sh
d

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326992960&siteId=291194637