1. Definition of variables
Variables are used to store all kinds of data. Scripting languages usually do not need to specify the type when defining variables. They can be assigned directly, and shell variables follow this rule.
2. Variables in shell scripts
Variables in the shell are divided into environment-level variables, user-level variables and system-level variables. Environment-level variables only work in the current shell, shell closing variables are missing, user-level variables uninstall the user's bone files and only work for the current user. System-level variables are written in the system's configuration file/etc/profile or/etc/frofile.d/, which works for all users.
3. How to define variables in shell scripts
1).export a=1 /Environmental level [root@westos mnt]# vim a.sh [root@westos mnt]# sh a.sh [root@westos mnt]# lzy=1 [root@westos mnt]# sh a.sh [root@westos mnt]# export lzy=1 [root@westos mnt]# sh a.sh
vim ~/.bash_profile /User level [root@westos mnt]# vim ~/.bash_profile [root@westos mnt]# tail -n 1 ~/.bash_profile [root@westos mnt]# source ~/.bash_profile [root@westos mnt]# echo $b [root@westos mnt]# su - student [student@westos ~]$ echo $b
vim /etc/profile /System level [root@westos ~]# vim /etc/profile [root@westos ~]# tail -n 1 /etc/profile [root@westos ~]# source /etc/profile [root@westos ~]# echo $c [root@westos ~]# su - student [student@westos ~]$ echo $c
4. Character translation and variable declaration
\ Translated but a character Weak citation, batch translation of characters in "" Strong quotation, batch translation of characters in'' / The distinction between''and''can be found in:''Untranslatable',''','','$''. Variable declaration
e.g.
[root@westos ~]# A=1 [root@westos ~]# echo $Ab [root@westos ~]# echo $\Ab [root@westos ~]# echo $\A b [root@westos ~]# echo $A \b
[root@westos ~]# echo '#####`hostname`#####'/ Translate `Symbols and hostname output together [root@westos ~]# echo "#####`hostname`#####"/ Weak reference, execute the commands in `hostname'"
e.g. user1, user2, user3 can be created after creating_user.sh
[root@westos mnt]# vim create_user.sh [root@westos mnt]# cat userfile [root@westos mnt]# sh create_user.sh userfile
e.g. user1, user2, user3 and their corresponding login passwords are the corresponding rows in passfile after executing create_users.sh
[root@westos mnt]# cat userfile passfile [root@westos mnt]# sh create_users.sh userfile passfile [root@westos mnt]# su - student [student@westos ~]$ su - user1 Password: [user1@westos ~]$
5. Variable Value Transfer
The first string after the script The second string after the script The third string after the script Number of strings after the script All strings after the script, mode "123" All strings after the script in the modes of "1", "2", "3"
e.g.
[root@westos mnt]# vim westos.sh [root@westos mnt]# chmod +x westos.sh [root@westos mnt]# sh westos.sh linux [root@westos mnt]# sh westos.sh linux lin [root@westos mnt]# sh westos.sh linux lin li
6. Realizing variable transfer with read
read WESTOS read -s WESTOS read -p "input:" WESTOS
Establishment and deletion of users can be achieved by executing usercel.sh file
[root@westos mnt]# sh userctl.sh -c Please input username: 123 Please input password: [root@westos mnt]# su - student [student@westos ~]$ su - 123 Password: [123@westos ~]$ exit [student@westos ~]$ exit [root@westos mnt]# sh userctl.sh -d Please input username: 123 [root@westos mnt]# sh userctl.sh -d Please input username: westos
7. Assignment of command aliases
unalias xie /Revoke command alias setting 1).alias xie='vim' /At present shell The alias will automatically disappear after exit. [root@westos mnt]# alias xie='vim' [root@westos mnt]# xie xiefile [root@westos mnt]# cat xiefile [root@westos mnt]# unalias xie [root@westos mnt]# xie file
2).vim ~/.bashrc /User-level aliases alias xie='vim' [root@westos mnt]# vim ~/.bashrc [root@westos mnt]# source ~/.bashrc [root@westos mnt]# tail -n 1 ~/.bashrc [root@westos mnt]# xie rootfile [root@westos mnt]# cat rootfile [root@westos mnt]# su - student [student@westos ~]$ xie studentfile
3).vim /etc/bashrc /System-level aliases alisas xie='vim' [root@westos mnt]# vim /etc/bashrc [root@westos mnt]# source /etc/bashrc [root@westos mnt]# tail -n 1 /etc/bashrc [root@westos mnt]# vim haha [root@westos mnt]# cat haha [root@westos mnt]# su - student [student@westos ~]$ xie studentfile [student@westos ~]$ cat studentfile
4. Revoke settings Execute unalias after deleting alias settings in files [root@westos mnt]# vim /etc/bashrc [root@westos mnt]# unalias xie [root@westos mnt]# xie checkfile
8. Use command execution results to set variables
Hostname=$(hostname) Hostname=`hostname` $? The $? Is the exit value generated after the command is executed The scope is [1-255] When $0 = 0, there is no error output for command execution. This value can be executed with exit command.
9. Script function
e.g. Write a function that allows users to be created while executing usercreate.sh. If the user exists, it can not be established, if there is no user name and password to be entered.
[root@westos mnt]# vim usercreate.sh [root@westos mnt]# sh usercreate.sh Please input username: student Please input username: liu [root@westos mnt]# su - liu [liu@westos ~]$
e.g. Check which hosts in the classroom have a smooth network and output these hosts
[root@westos mnt]# vim check_host.sh [root@westos mnt]# sh check_host.sh