She’ll脚本的基本应用

**shell脚本的基本应用

1.默认shell脚本
在这里插入图片描述
2.创建第一个脚本文件

[root@dns1 ~]# vim first.sh
#!/bin/bash
#This is my firat Shell-Script,
cd /boot
echo “当前的目录位于:”
pwd
echo “其中以vml开头的文件包括:”
ls -lh vml*

3.添加可运行条件
[root@centos01 ~]# chmod +x first.sh
4.直行脚本文件
[root@centos01 ~]# ./first.sh
当前目录位于/boot
5.通过/bin/sh来解释脚本
[root@centos01 ~]# sh first.sh
6.通过点好来加载脚本
[root@centos01 ~]# . first.sh
7.重定向输出:将执行命令正确的结果输出到指定文件
[root@centos01 boot]# uname -p > kernel.txt
[root@centos01 boot]# cat kernel.txt
8.追加内容部署全覆盖
[root@centos01 boot]# uname -r >> kernel.txt
[root@centos01 boot]# cat kernel.txt
9.重定向输入
[root@centos01 boot]# vim pass.txt
[root@centos01 boot]# passwd --stdin jerry < pass.txt
[root@centos01 boot]# cat pass.txt
123456
10.错误重定向:将使用tar命令进行备份时出现错误信息保存到error.log
[root@centos01 boot]# tar jcf /nonedir/etc.tgz /etc/ 2> error.log
[root@centos01 boot]# cat error.log
tar: 从成员名中删除开头的“/”
tar (child): /nonedir/etc.tgz:无法 open: 没有那个文件或目录
tar (child): Error is not recoverable: exiting now
[root@centos01 boot]#
11.自动安装httpd编译
#!/bin/bash
#自动编译安装httpd服务器的脚本
cd /usr/src/httpd-2.4.25/
./configure --prefix=/usr/local/httpd --enable-so &> /dev/null
make &> /dev/null
make install &> /dev/null
12.管道操作
[root@centos01 boot]# grep “/bin/bash$” /etc/passwd | awk -F: ‘{print $1,$7}’
root /bin/bash
test /bin/bash
jerry /bin/bash
[root@centos01 boot]#
13.定义新的变量
[root@centos01 boot]# Product=Python
[root@centos01 boot]# Version=2.7.13
[root@centos01 boot]# echo KaTeX parse error: Expected 'EOF', got '#' at position 35: …@centos01 boot]#̲ echo {Product}2.5
{Python}2.5
[root@centos01 boot]# echo $Product
Python
[root@centos01 boot]# echo $Product $Version
Python 2.7.13
[root@centos01 boot]#
14.变量赋值的特殊操作
[root@centos01 boot]# PYTHON=“Python 2.7.13”
[root@centos01 boot]# echo $PYTHON
Python 2.7.1
[root@centos01 boot]#
15.以变量的值进行赋值
[root@centos01 boot]# SQLServer=“SQLServer $Version”
[root@centos01 boot]# echo $SQLServer
SQLServer 2.7.13
[root@centos01 boot]# SQLServer='SQLServer V e r s i o n ′ / / Version'// Version//符号不能在引用变量
[root@centos01 boot]# echo $SQLServer//原样输出字符串
SQLServer $Version
16.反撇号() [root@centos01 boot]# ls -lhwhich useradd`
-rwxr-x—. 1 root root 116K 11月 6 2016 /usr/sbin/useradd
[root@centos01 ~]# export FQDN=“www.jb-aptech.com.cn” 导出全局变量的同事为变量赋值
[root@centos01 ~]# echo $FQDN
www.jb-aptech.com.cn
17.数值变量的运算
[root@centos01 ~]# X=35
[root@centos01 ~]# Y=16
[root@centos01 ~]# expr $X + $Y
51
[root@centos01 ~]# expr $X - $Y
19
[root@centos01 ~]# expr $X * $Y
560
[root@centos01 ~]# expr $X / $Y
2
[root@centos01 ~]# expr $X % $Y
3
[root@centos01 ~]# Ycube=expr $Y * $Y * $Y
[root@centos01 ~]# echo $Ycube
4096
环境变量
[root@centos01 ~]# ls -lh /root/first.sh
-rw-r–r-- 1 root root 112 12月 9 13:10 /root/first.sh
[root@centos01 ~]# echo KaTeX parse error: Expected ‘EOF’, got ‘#’ at position 83: …oot@centos01 ~]#̲ first.sh bash:…PATH:/root" 将/root添加到搜索路径PATH
[root@centos01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root
[root@centos01 ~]# first.sh
-bash: /root/first.sh: 权限不够
[root@centos01 ~]# chmod +x first.sh
[root@centos01 ~]# first.sh
当前的目录位于:
/boot
其中以vml开头的文件包括;
-rwxr-xr-x. 1 root root 5.7M 11月 23 19:34 vmlinuz-0-rescue-2cea6d42f4ac45e7bb2d9192c38aefa2
-rwxr-xr-x. 1 root root 5.7M 8月 23 2017 vmlinuz-3.10.0-693.el7.x86_64
位置变量
[root@centos01 ~]# vim adder2num.sh
[root@centos01 ~]# cat adder2num.sh
#/bin/bash
SUM=expr $1 + $2
echo “$1 + 2 = 2 = 2=SUM”
[root@centos01 ~]# chmod +x adder2num.sh
[root@centos01 ~]# ./adder2num.sh 12 34
12 + 34 =46
[root@centos01 ~]# ./adder2num.sh 56 78
56 + 78 =134
预定义变量
[root@centos01 ~]# vim mybak.sh
[root@centos01 ~]# chmod +x mybak.sh
[root@centos01 ~]# ./mybak.sh /boot/grup
已执行 ./mybak.sh 脚本,
共完成 1 个对象的备份
具体内容包括: /boot/grup
[root@centos01 ~]# ./mybak.sh /etc/passwd /etc/shadow
已执行 ./mybak.sh 脚本,
共完成 2 个对象的备份
具体内容包括: /etc/passwd /etc/shadow
[root@centos01 ~]# ls -lh beifen-*

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值