Shell脚本应用(一)

一:第一个shell脚本

[root@localhost ~]# cat /etc/shells

[root@localhost ~]# vi first.sh

cd /boot/

pwd

ls -lh vml*

[root@localhost ~]# chmod +x first.sh

[root@localhost ~]# ./first.sh

[root@localhost ~]# vi first.sh

#!/bin/bash

#this is my first shell script

cd /boot/

echo “当前的目录位于:”

pwd

echo “其中以vml开头的文件包括:”

ls -lh vml*

[root@localhost ~]# ./first.sh

[root@localhost ~]# sh first.sh

[root@localhost ~]# bash first.sh

[root@localhost ~]# . first.sh

[root@localhost ~]# source first.sh

注释:“./”、sh、bash是相同的执行方式,“.”和source是相同的

source和“.”执行脚本时,将脚本中语句在本shell中执行,

sh、bash和./是在执行脚本时先启动一个新的shell,然后让脚本中的语句在新的shell中执行,执行完后就退出。

二:重定向与管道操作

1:重定向

1):重定向输出

[root@localhost ~]# uname -p >
kernel.txt

[root@localhost ~]# cat kernel.txt

[root@localhost ~]# uname -r >>
kernel.txt       \追加并保存,不覆盖原有的数据

[root@localhost ~]# cat kernel.txt

扩展:

[root@localhost ~]# sed -i ‘1a\aaa’ 123.txt   #在第一行的下一行添加文字

[root@localhost ~]# sed -i ‘1i\aaa’ 123.txt    #在第一行的上一行添加文字

2):重定向输入

[root@localhost ~]# vi pass.txt

添加

123456

[root@localhost ~]# useradd aaa

[root@localhost ~]# passwd --stdin aaa
<pass.txt       \selinux要设置为disabled

3):重定向错误

[root@localhost ~]# tar jcf
/nonedir/etc.tgz /etc/ 2>error.log

[root@localhost ~]# cat error.log

案例:

[root@localhost ~]# vi httpd_install.sh

#!/bin/bash

自动编译安装httpd服务器脚本

cd /opt

tar zxvf httpd-2.2.17.tar.gz &>/dev/null

cd /opt/httpd-2.2.17

./configure --prefix=/usr/local/httpd --enable-so &>/dev/null

make &>/dev/null

make install &> /dev/null

/usr/local/httpd/bin/apachectl start

firefox 127.0.0.1

[root@localhost ~]# chmod +x
httpd_install.sh

[root@localhost ~]# ./httpd_install.sh

2:管道操作

[root@localhost ~]# grep
“/bin/bash$” /etc/passwd          
\以bash结尾的行

[root@localhost ~]# grep
“/bin/bash$” /etc/passwd | awk -F: ‘{print $1,$7}’

[root@localhost ~]# df -hT

[root@localhost ~]# df -hT | grep
“/$” | awk ‘{print $6}’    #根目录下磁盘利用率

三:使用shell变量

变量名不能用数字开头

变量名中不能有小数点

变量名不能用纯数字

变量名可以是字母开头再加数字

变量名中不能有斜杠“/”、“$”、“#”、“@”等特殊符号

1:自定义变量

[root@localhost ~]# product=benet

[root@localhost ~]# version=5.0

2:查看和引用变量的值

[root@localhost ~]# echo $product

benet

[root@localhost ~]# echo $product $version

benet 5.0

[root@localhost ~]# echo $product4.5           \错误的引用

.5

[root@localhost ~]# echo ${product}4.5

benet4.5

注意:引用变量时如果变量名后有其他的字符,要将变量名应大括号引起来,用以定界

3:变量赋值的特殊操作

1):双引号

双引号可用于字符串的声明,连续的字符可以省略双引号,字符串中有空格的话就不能省略,另外,声明的变量值中如果要引用另一个变量,也需要双引号。

[root@localhost ~]# benet=benet 5.0        \错误的赋值

bash: 5.0: command not found

[root@localhost ~]# benet=“benet
5.0”

[root@localhost ~]# echo $benet

[root@localhost ~]# accp=“accp
$version”

[root@localhost ~]# echo $accp

accp 5.0

2):单引号

单引号用于原样输出的变量声明,意思是变量的值中需要保留显示$符号

[root@localhost ~]# accp=‘accp $version’

[root@localhost ~]# echo $accp

accp $version                     \单引号中不能再引用变量,因此此处是错误的输出

3):反撇号

反引号用于命令的引用,相当于 ( ) , 区 别 在 于 反 引 号 不 能 嵌 套 , 而 (),区别在于反引号不能嵌套,而 ()()可以嵌套

[root@localhost ~]# ls -lh which useradd

[root@localhost ~]# aaa=grep -v "^#" /etc/profile

[root@localhost ~]# echo $aaa

[root@localhost ~]# rpm -qc $(rpm -qf
$(which useradd))

4):read命令

[root@localhost ~]# read todir1

/opt/backup/                \用户输进去的

[root@localhost ~]# echo $todir1

/opt/backup/

[root@localhost ~]# read -p “请制定备份存放的目录:” todir2

请制定备份存放的目录:/opt/backup

[root@localhost ~]# echo $todir2

/opt/backup

综合案例

[root@localhost ~]# vi httpd_install.sh

#!/bin/bash

自动编译安装httpd服务器脚本

read -p 请输入源码包的目录: mydir

cd $mydir

read -p 请输入软件包的名字: pack

tar zxvf ${pack}*.tar.gz &>/dev/null

cd / m y d i r / mydir/ mydir/{pack}*

./configure --prefix=/usr/local/$pack &>/dev/null

make &>/dev/null

make install &> /dev/null

/usr/local/httpd/bin/apachectl start

firefox 127.0.0.1

[root@localhost ~]# chmod +x httpd_install.sh

[root@localhost ~]# ./httpd_install.sh

4:设置变量的作用范围

1):例1

[root@localhost ~]# echo “product
$version”

product 5.0

[root@localhost ~]# bash

[root@localhost ~]# echo “product
$version”

product

[root@localhost ~]# exit

exit

[root@localhost ~]# echo “product
$version”

product 5.0

2):例2

[root@localhost ~]# echo “product
$version”

product 5.0

[root@localhost ~]# echo “$product
$version”

benet 5.0

[root@localhost ~]# export product version

[root@localhost ~]# bash

[root@localhost ~]# echo “$product
$version”

benet 5.0

[root@localhost ~]# exit

exit

3):例3

[root@localhost ~]# export
fqdn=“www.benet.com”

[root@localhost ~]# echo $fqdn

5:数值变量的运算

[root@localhost ~]# x=35

[root@localhost ~]# y=16

[root@localhost ~]# expr x + x+ x+y

35+16

[root@localhost ~]# expr $x + $y

51

[root@localhost ~]# expr $x - $y

19

[root@localhost ~]# expr $x * $y

560

[root@localhost ~]# expr $x / $y

2

[root@localhost ~]# expr $x % $y

3

[root@localhost ~]# Ycube=expr $y \* $y \* $y

[root@localhost ~]# echo $Ycube

4096

[root@localhost ~]# a=2

[root@localhost ~]# echo $((a*3))

6

[root@localhost ~]# echo $((a**3))   #3次方

8

四:特殊的shell变量

1:环境变量

set命令也可以显示环境变量,它显示的是是系统中所有的环境变量,包括全局变量和局部变量

env只显示全局变量

[root@localhost ~]# env

[root@localhost ~]# ls -lh /root/first.sh

[root@localhost ~]# echo $PATH

[root@localhost ~]# first.sh

[root@localhost ~]# first.sh

bash: first.sh: command not found

[root@localhost ~]#
PATH="$PATH:/root"

[root@localhost ~]# echo $PATH

[root@localhost ~]# first.sh

/boot

[root@localhost ~]# vi /etc/profile

修改

export HISTORYSIZE=20

[root@localhost ~]# history | wc -l

89

[root@localhost ~]# source /etc/profile

[root@localhost ~]# history | wc -l

20

2:位置变量

[root@localhost
~]# vi adder2num.sh

#!/bin/bash

SUM=expr $1 + $2

echo “$1 + $2 = $SUM”

[root@localhost ~]# chmod +x adder2num.sh

[root@localhost ~]# ./adder2num.sh 12 14

12 + 14 = 26

3:预定义变量

$# 传送给命令Shell的参数个数

$- 在Shell启动或使用set命令时提供选项

$? 上一条命令执行后返回的值

$$ 当前shell的进程号

$! 上一个子进程的进程号

$@ 所有的参数,每个都用双括号括起

$* 所有参数,用双括号括起

$n 位置参数值,n表示位置

$0 当前shell名

[root@localhost ~]# vi mybak.sh

#!/bin/bash

TARFILE=beifen-date +%s.tgz

tar zcf $TARFILE $* &> /dev/null

echo “已执行 $0 脚本,”

echo “共完成 $# 个对象的备份”

echo “具体内容包括: $*”

[root@localhost ~]# chmod +x mybak.sh

[root@localhost ~]# ./mybak.sh /boot/grub

已执行 ./mybak.sh 脚本,

共完成 1 个对象的备份

“具体内容包括: /boot/grub”

[root@localhost ~]# ./mybak.sh /etc/passwd
/etc/shadow

已执行 ./mybak.sh 脚本,

共完成 2 个对象的备份

“具体内容包括: /etc/passwd /etc/shadow”

[root@localhost ~]# ls -lh beifen-*

-rw-r–r--. 1 root root 101K 12月  1
22:36 beifen-1448980601.tgz

-rw-r–r--. 1 root root 1.1K 12月  1
22:37 beifen-1448980640.tgz

五:shell脚本与计划任务

1:确定备份方案

mysql> grant select,lock tables on
mysql.* to ‘root’@‘192.168.4.110’ identified by ‘pwd123’;

mysql> grant select,lock tables on
test.* to ‘root’@‘192.168.4.110’ identified by ‘pwd123’;

[root@localhost mysql-5.5.22]# mysqldump -u
root -p -h localhost --databases test > test.sql

[root@localhost mysql-5.5.22]# mysqldump -u
root -p -h localhost --databases mysql > mysql.sql

[root@localhost mysql-5.5.22]# ls -lh
test.sql

-rw-r–r--. 1 root root 0 12月  1
22:46 test.sql

2:编写mysql备份脚本

[root@localhost mysql-5.5.22]# mysqladmin
-u root -p password ‘aptech’

[root@localhost mysql-5.5.22]# mkdir -p
/opt/qnzx_dbbak/

[root@localhost mysql-5.5.22]# vi
qnzx_dbbak.sh

#!/bin/bash

这是一个简化的MySQL数据库逻辑备份脚本

1.定义数据库的连接、目标库信息

MY_USER=“root”

MY_PASS=“aptech”

MY_HOST=“localhost”

MY_CONN="-u M Y U S E R − p MY_USER -p MYUSERpMY_PASS -h
$MY_HOST"

MY_DB1=“mysql”

MY_DB2=“test”

2.定义备份目录、工具、时间、文件名主体

BF_DIR="/opt/qnzx_dbbak"

BF_CMD="/usr/local/mysql/bin/mysqldump"

BF_TIME=date +%Y%m%d-%H%M

NAME_1=“ M Y D B 1 − MY_DB1- MYDB1BF_TIME”

NAME_2=“ M Y D B 2 − MY_DB2- MYDB2BF_TIME”

3.先导出为.sql脚本,然后再进行压缩(打包后删除原文件)

cd $BF_DIR

$BF_CMD $MY_CONN --databases $MY_DB1 >
$NAME_1.sql

$BF_CMD $MY_CONN --databases $MY_DB2 >
$NAME_2.sql

/bin/tar zcf $NAME_1.tar.gz $NAME_1.sql
–remove &> /dev/null

/bin/tar zcf $NAME_2.tar.gz $NAME_2.sql
–remove &> /dev/null

[root@localhost mysql-5.5.22]# chmod +x
qnzx_dbbak.sh

[root@localhost mysql-5.5.22]# vi
qnzx_dbbak.sh

[root@localhost mysql-5.5.22]#
./qnzx_dbbak.sh

[root@localhost mysql-5.5.22]# ls -lh
/opt/qnzx_dbbak/*.gz

3:设置计划任务

[root@localhost mysql-5.5.22]# mv
qnzx_dbbak.sh /opt/qnzx_dbbak/

[root@localhost mysql-5.5.22]# crontab -e

添加

30 2 * * * /opt/qnzx_dbbak/qnzx_dbbak.sh

[root@localhost mysql-5.5.22]# service
crond status

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值