shell Scripting (1)

Keywords: shell vim Python Linux

I. variables

Types of variables in shell s
 1) Local variables (local variables)
2) Environmental variables (global variables)
3) Location variable
 4) Special variables

1. Local variables

Also known as custom variables, users define variables according to their own needs
 Scope: Acts throughout the shell script; if a variable is defined on the command line, the scope is the current shell.

Define variables: variable name = variable value defines local variables  
Note: There must be no spaces on either side of the equal sign.
[root@shell ~]# name=mary        
[root@shell ~]# echo $name
mary
[root@shell ~]# bash // Open a child shell
[root@shell ~]# echo $SHLVL **// / View the current shell level
3
[root@shell ~]# echo $name // is a local variable that cannot be seen in the parent shell in the child shell
[root@shell ~]# exit
exit
[root@shell ~]# echo $SHLVL 
2

2. Environmental variables

Scope of action: Current shells and their sub-shells

Define environmental variables:

1) export defined local variables to environment variables
[root@shell ~]# export name
[root@shell ~]# echo $name
mary
[root@shell ~]# bash
[root@shell ~]# echo $name // / You can see the value of name in the child shell
mary
2) When defining variables, they are directly defined as environmental variables.
[root@shell ~]# echo $SHLVL
2
[root@shell ~]# export age=88
[root@shell ~]# bash
[root@shell ~]# echo $age
 88
3) declare-x name defines environment variables
    Note: Modifying the value of the variable in the child shell does not affect the value of the variable in the parent shell.
[root@shell ~]# echo $SHLVL
3
[root@shell ~]# age=66
[root@shell ~]# echo $age
66
[root@shell ~]# exit
exit
[root@shell ~]# echo $age
88     
    If you want the environment variable to take effect permanently, you can write it in the environment variable configuration file.
Cancel the value of a variable
[root@shell ~]# unset name
[root@shell ~]# echo $name    

3. Location variables

[root@shell ~]# ls /root /tmp
ls  ——  $0
/root  —— $1
/tmp  —— $2
[root@shell ~]# cd /script/
[root@shell ~]# vim position.sh
#!/bin/bash
echo '$0='$0     
echo '$1='$1     
echo '$2='$2     
echo '$3='$3     
echo '$4='$4     
echo '$5='$5     
echo '$6='$6     
echo '$7='$7     
echo '$8='$8     
echo '$9='$9     
echo '$10='$10     
echo '$11='${11}     
echo '$#='$#
echo '$*='$*
echo '$@='$@
[root@shell ~]# chmod +x position.sh 
[root@shell ~]# ./position.sh a b c d e f g h i j k l m
$0=./position.sh
$1=a
$2=b
$3=c
$4=d
$5=e
$6=f
$7=g
$8=h
$9=i
$10=a0
$11=k
$#=13                             // Number of parameters
$*=a b c d e f g h i j k l m      //Print parameter list
$@=a b c d e f g h i j k l m
    $0 -- Represents the script or command itself currently running
    $1 ~ - represents the location parameter of the script, $1 represents the first parameter, and so on.
    $# - Number of position parameters
    $@ and $* -- a list of parameters

4. Special variables

$View the pid of the current shell
	[root@shell ~]# echo $$

$and $
    The last parameter of the previous command
    [root@shell ~]# vim filename
    [root@shell ~]# cat !$
        Or alt+. (point) can be    

$?: Return value of execution status of the previous command
   Execution status return value (0-255), divided into two categories
    0: Represents the successful execution of the previous command
    Non-0: Represents a failure to execute the previous command

5. Reference to Variables

    1, $variable name
     echo $variable name
    2, ${Variable Name} - To prevent ambiguity
[root@shell ~]# fruit=banana
[root@shell ~]# echo "There are some $fruits."
There are some .
[root@shell ~]# echo "There are some ${fruit}s."
There are some bananas.      

2. Arithmetic operations

1, $[] and $(())
[root@shell ~]# echo $[1+2]
3
[root@shell ~]# echo $((1+2))
3
[root@shell ~]# echo $[2**16]
65536
2. let is often used to update loop variables
        let variable = value
[root@shell ~]# let sum=1+2
[root@shell ~]# echo $sum
3   
[root@shell ~]# a=8
[root@shell ~]# b=5
[root@shell ~]# let ji=$a*$b
[root@shell ~]# echo $ji
40
[root@shell ~]# let i++// is commonly used to update variable values in loops, adding 1 to the value of I each time.
3. Linux Calculator
[root@shell ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
5/6
0
scale=3    //Designated precision, precision is to reserve several decimal places after the decimal point.
9/8
1.125
ctrl + d Sign out

III. Script Programming

1. Reasons for using shell scripts

  1) It has powerful functions and can be transplanted.
  2) Save time, improve work efficiency, and repeat commands

2. Benefits of shell Scripting

      Reduce repetitive work and improve efficiency

3. Elements that make up shell scripts, i.e. the basic format of shell scripts

  #!/ bin/bash - Top-lattice shabang, also known as magic numbers, is used to specify the environment in which commands in scripts are parsed.
  perl:  #!/usr/bin/perl
  python: #!/usr/bin/python
  Comment lines from one line to multiple lines begin with #.
     Notes include: Author, Writing Time, Update Time, Functional Introduction of the script, etc.

4. Debugging shell scripts

  1)Debugging the script as a whole
    sh -x Name of script     //- x: Prints each line of script execution to standard output
  2)Local debugging of scripts
            set -x     //Turn on debugging function
            set +x     //Turn off debugging
[root@shell ~]# cd /script/
[root@shell ~]# vim sc1.sh
#!/bin/bash
for i in $(seq 5)
    do
        echo $i
    done
[root@shell ~]# sh -x sc1.sh
[root@shell ~]# vim sc1.sh
#!/bin/bash
for i in $(seq 5)
    do
        set -x
        echo $i
        echo hello $i
        set +x
    done
[root@shell ~]# sh sc1.sh

5. Running of scripts

1) Complete scripting, add execution permissions, and run scripts in relative or absolute paths
[root@shell ~]# ./sc1.sh
bash: ./sc1.sh: Permission denied
[root@shell ~]# chmod +x sc1.sh 
[root@shell ~]# ./sc1.sh 
2)Write, use sh perhaps bash Command run script(You may not have permission to execute)
[root@shell ~]# sh sc1.sh// with or without execution permissions

6. Simple little scripts

Script: stacking of commands, plus process control statements
[root@shell ~]# vim sc3.sh
#!/bin/bash
free -m
df -h
date

IV. Read variables

read: The ability to read user input values from standard input and pass them to variables in the script

echo: Print variable values, print strings, etc.
    -e: Give Way echo The following symbols are supported
        \t: tab
        \n: Line feed
[root@shell ~]# echo -e "hello\tworld\nhello everyone"
hello   world
hello everyone
    -n: nowrap

read reads user input

[root@shell ~]# vim sc4.sh
#!/bin/bash
echo -n "Please input the username you want to add: "
read username     //read Variable name
useradd $username && echo "user $username added."
[root@shell ~]# sh sc4.sh 
Please input the username you want to add: Loyal-Wang
user Loyal-Wang added.

2. read itself displays prompt information - p print prompt - prompt: prompt

[root@shell ~]# cp sc4.sh sc5.sh
[root@shell ~]# vim sc5.sh
#!/bin/bash
read -p "Please input the username you want to add: " username  //read -p Tip information variable name
useradd $username && echo "user $username added."    //There must be a space between prompt information and variable name
[root@shell ~]# sh sc5.sh 
Please input the username you want to add: doushuine
user doushuine added.

3. read does not echo user input-s

[root@shell ~]# cp sc5.sh sc6.sh
[root@shell ~]# vim sc6.sh
#!/bin/bash
read -p "Please input the username you want to add: " username
read -s -p "Please input the password you want to set: " password
echo   //Line feed
useradd $username && echo "user $username added."
echo $password | passwd --stdin $username &>/dev/null && echo "password set successfully."
[root@shell ~]# sh sc6.sh 
Please input the username you want to add: taa
Please input the password you want to set: 
user taa added.
password set successfully.
echo: It can be used to print blank lines or newlines.

4. Restrict read time-t

[root@shell ~]# cp sc6.sh sc7.sh
[root@shell ~]# vim sc7.sh
#!/bin/bash
read -p "Please input the username you want to add: " -t 5 username               //Units are seconds.
read -s -p "Please input the password you want to set: " password
echo "***"
useradd $username && echo "user $username added."
echo $password | passwd --stdin $username &>/dev/null && echo "password set successfully."
    read:  
        -p: Print prompt information
        -s: Do not echo user input information
        -t Time: Limit the waiting time for reading input
        -n  Number: Enter the specified number of characters

5. bash configuration file:

    Global configuration
        /etc/profile, /etc/profile.d/*.sh, /etc/bashrc
    Personal configuration
        ~/.bash_profile, ~/.bashrc

    Files of profile class:
        Setting environmental variables
        Run commands or scripts

    Files of the bashrc class:
        Setting local variables
        Define command aliases

Posted by cdinca on Thu, 04 Apr 2019 18:21:31 -0700