Comprehensive use of special variables and scripts
first, environment variables :
ENV # #查看所有的环境变量
Echo-e "1\n2" # #换行显示
Echo-e "1\t2" # #在1和2之间加入tab显示
Echo-e "\033[44;37;5m me\033[0m COOL" # #有颜色显示, "\033[" is the terminal escape character start
Echo-e "$PAHT \ $SHELL \ $HOSTNAME \ $MAIL $HISTSIZE \n\t $LANG"
Note: path is the command search route, the shell uses the shell,histsize for the current user to record the number of records for the history command, Lang is currently using the language
Export # #查看全局环境变量定义情况
Export Lang=en_us. UTF-8 # #设置默认语言, if you want to set the value to Chinese, change it to ZH_CN. UTF-8
Locale # #查看语言设置
unset Histsize # #取消环境变量HISTSIZE
Ps1= "\e[1;31m====>" # #设置提示符
====>ps1= "[\[email protected]\h \w]\$" # #恢复PS1的变量
Vim/etc/dir_colors # #修改第81, the value of 128 rows is as follows
Bayi DIR 01;43
. XZ 01;32
: Wq
Tar Jcf changecls.xz/etc/hosts; mkdir dir # #创建测试文件和目录
LS #查看创建的文件和目录的颜色
Exit # #注销系统, re-login verification
LS # #颜色已经变了
echo "Export tmout=300" >>/etc/profile # #设置TMOUT为永久生效的环境变量, note that only the variables that are written to the file are permanently active
Source/etc/profile
Sed-i ' s/histsize=100/histsize=800/g '/etc/profile # #设置历史命令条数
grep hist/etc/profile |grep ^hi # #验证
Source/etc/profile
Echo $HISTSIZE
Summary: Environment variables are variables that are set for the user's working environment parameters, tested before modification, and then written to the file
Two: Positional variables and pre-defined variables
VI position.sh # #编写并理解位置变量和与定义变量
#!/bin/bash
echo "This is \$0:$0"
echo "This is \$1:$1"
echo "This is \$2:$2"
echo "This is \$3:$3"
echo "This is \$4:$4"
echo "This is \$5:$5"
echo "This is \$6:$6"
echo "This is \$7:$7"
echo "This is \$8:$8"
echo "This is \$9:$9"
echo "This is \$#:$#"
echo "This is \$*:$*"
echo "This is \$?:$?"
echo "$ is exec complete!"
: Wq
chmod +x position.sh
/root/bin/position.sh a b c d E F g h x y Z
Vim bak.sh
#!/bin/bash
Tf= "/data/benfen-$ (date +%f). tar.gz"
Tar zcf $TF $* 1>/dev/null
echo "\$0:exec $ script."
echo "\$#:total bakup $#."
echo "\$*:bakup $*"
echo "* * * 7/root/bin/bak.sh $*" >>/var/spool/cron/root
: Wq
chmod +x bak.sh
/root/bin/bak.sh/etc/yum.repos.d//etc/sysconfig/etc/hosts # #备份并观察 $0,$ #的作用
Write a MySQL backup script
1. Prepare the database:
Vim sql.sh # #在mysql (192.168.100.150) The script is written on the server
#!/bin/bash
# by Linuxfan.cn 2016.1.1
Mysql-uroot-p123123 <<end
Create Database Studydb;
Create Database Coursedb;
Grant Select,lock tables on studydb.* to [email protected] ' 192.168.100.151 ' identified
By ' 123123 ';
Grant Select,lock tables on coursedb.* to [email protected] ' 192.168.100.151 ' identified
By ' 123123 '
Show grants for [email protected] ' 192.168.100.151 ';
END
: Wq
Complete the following actions before you execute the script
NETSTAT-UTPLN |grep 3306
Mysql-uroot-p123123
chmod +x sql.sh
The use of variables in the above script will likely follow the host's IP, database name, password, user name set to a variable and replace the corresponding location
2. Write scripts for backup data
On the 192.168.100.151 server, complete the following:
Vim dbbak.sh
#!/bin/bash
# by linuxfan.cn
# 2016.1.1
mkdir/opt/dbbak/
/usr/bin/mysqldump-uoperator-p123123-h 192.168.100.150--databases Coursedb
>/opt/dbbak/coursedb-$ (date +%f-%h:%m). SQL >/dev/null
/usr/bin/mysqldump-uoperator-p123123-h 192.168.100.150--databases Studydb
>/opt/dbbak/studydb-$ (date +%f-%h:%m). SQL >/dev/null
/bin/tar jcf/opt/dbbak/coursedb-$ (Date +%f-%h:%m). tar.xz
/opt/dbbak/coursedb-$ (date +%f-%h:%m). SQL--remove &>/dev/null
/bin/tar jcf/opt/dbbak/studydb-$ (Date +%f-%h:%m). tar.xz
/opt/dbbak/studydb-$ (date +%f-%h:%m). SQL--remove &>/dev/null
: Wq
Complete the following actions before you execute the script
Mount/dev/cdrom/mnt
yum-y install MySQL # #安装mysql客户端
Mysql-uoperator-p123123-h 192.168.100.150 # #登录测试
Mysqldump-uoperator-p123123-h 192.168.100.150--databases Studydb
>test.sql;ls # #查看是否能成功备份
In the above script use variables, the user, database name, password, host IP, time, backup SQL file name, the name of the compressed file is defined as variables, and replace the corresponding location.
chmod +x dbbak.sh
Script test:
Sh-x dbbak.sh
ls/opt/dbbak/# #查看是否只有xz后缀的文件
Add a scheduled task after the test is successful:
echo "2 * * */root/bin/dsbak.sh" >>/var/spool/cron/root # #每天的两点半开始备份
This article is from the "Lp-linux" blog, make sure to keep this source http://linuxlp.blog.51cto.com/11463376/1774039
Use of shell scripts---the use of special variables and scripts