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 ~]
[root@shell ~]
mary
[root@shell ~]
[root@shell ~]
3
[root@shell ~]
[root@shell ~]
exit
[root@shell ~]
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 ~]
[root@shell ~]
mary
[root@shell ~]
[root@shell ~]
mary
2) When defining variables, they are directly defined as environmental variables.
[root@shell ~]
2
[root@shell ~]
[root@shell ~]
[root@shell ~]
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 ~]
3
[root@shell ~]
[root@shell ~]
66
[root@shell ~]
exit
[root@shell ~]
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 ~]
[root@shell ~]
#!/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 ~]
[root@shell ~]
$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 ~]
[root@shell ~]
There are some .
[root@shell ~]
There are some bananas.
2. Arithmetic operations
[root@shell ~]
3
[root@shell ~]
3
[root@shell ~]
65536
2. let is often used to update loop variables
let variable = value
[root@shell ~]
[root@shell ~]
3
[root@shell ~]
[root@shell ~]
[root@shell ~]
[root@shell ~]
40
[root@shell ~]
[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 ~]
[root@shell ~]
#!/bin/bash
for i in $(seq 5)
do
echo $i
done
[root@shell ~]
[root@shell ~]
#!/bin/bash
for i in $(seq 5)
do
set -x
echo $i
echo hello $i
set +x
done
[root@shell ~]
5. Running of scripts
1) Complete scripting, add execution permissions, and run scripts in relative or absolute paths
[root@shell ~]
bash: ./sc1.sh: Permission denied
[root@shell ~]
[root@shell ~]
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 ~]
#!/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 ~]
#!/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 ~]
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 ~]
[root@shell ~]
#!/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 ~]
Please input the username you want to add: doushuine
user doushuine added.
3. read does not echo user input-s
[root@shell ~]
[root@shell ~]
#!/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 ~]
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 ~]
[root@shell ~]
#!/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