shell脚本的应用

1.查看当前系统支持的shell脚本种类。
[root@centos01 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
2.创建第一个脚本文件:first.sh
[root@centos01 ~]# vim first.sh
#!/bin/bash
#This is my first Shell-Script.
cd /boot/
echo “当前的目录位于:”
pwd
echo “其中以vml开头的文件包括:”
ls -lh vml*
[root@centos01 ~]# chmod +x first.sh
3.执行脚本文件
[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
4.重定向输出:将执行命令正确的结果输出到指定的文件中
[root@centos01 ~]# uname -p > kernel.txt
[root@centos01 ~]# cat kernel.txt
x86_64
将命令正确的结果追加到文件中。
[root@centos01 ~]# uname -r >> kernel.txt
[root@centos01 ~]# cat kernel.txt
x86_64
3.10.0-693.el7.x86_64
5.重定向输入:将指定文件中的内容输入到键盘输入的命令中。
[root@centos01 ~]# vim pass.txt
[root@centos01 ~]# passwd --stdin jerry < pass.txt
[root@centos01 ~]# cat pass.txt
123456
6.错误重定向:将使用tar命令进行备份时出现的错误信息保存到error.log。
[root@centos01 ~]# tar jcf /nonedir/etc.tgz /etc/ 2> error.log
[root@centos01 ~]# cat error.log
tar (child): tar: /nonedir/etc.tgz:无法 open从成员名中删除开头的“/”
: 没有那个文件或目录
tar (child): Error is not recoverable: exiting now
7.管道操作:使用grep命令查询使用/bin/bash作为shell的用户名称时,会输出符合条件的整行内容。使用awk和管道符过滤后只输出用户名和登录shell列。
[root@centos01 ~]# grep “/bin/bashKaTeX parse error: Expected 'EOF', got '#' at position 107: …oot@centos01 ~]#̲ grep "/bin/bas…” /etc/passwd | awk -F: '{print $1,KaTeX parse error: Expected 'EOF', got '}' at position 2: 7}̲ ' 以:作为分隔符,提…" | awk ‘{print $6}’ f 提取以/结尾的行并只查看第六列
5%
8.定义新变量
[root@centos01 ~]# Product=Python
[root@centos01 ~]# Version=2.7.13
[root@centos01 ~]# echo $Product
Python
[root@centos01 ~]# echo $Version
2.7.13
9.变量赋值的特殊操作
[root@centos01 ~]# PYTHON=Python 2.7.13 错误赋值
bash: 2.7.13: 未找到命令…
[root@centos01 ~]# PYTHON=“Python 2.7.13” 有空格双引号赋值
[root@centos01 ~]# echo $PYTHON
Python 2.7.13


[root@centos01 ~]# SQLServer="SQLServer $Version " 以变量的值进行赋值
[root@centos01 ~]# echo $SQLServer
SQLServer 2.7.13


[root@centos01 ~]# SQLServer='SQLServer V e r s i o n ′ 单 引 号 特 殊 符 号 不 能 使 用 Version' 单引号特殊符号不能使用 Version使
[root@centos01 ~]# echo $SQLServer
SQLServer $Version 显示原样


[root@centos01 ~]# ls -lh which useradd 在一行命令中查找useradd命令程序的位置并列出详细
先通过which useradd命令查找出useradd命令的程序位置,再根据查找结果列出文件属性
-rwxr-x—. 1 root root 116K 11月 6 2016 /usr/sbin/useradd


[root@centos01 ~]# read -p “请指定备份存放目录:” benet
请指定备份存放目录:aaa
[root@centos01 ~]# echo KaTeX parse error: Expected 'EOF', got '#' at position 41: …oot@centos01 ~]#̲ echo "Product KaTeX parse error: Expected 'EOF', got '#' at position 41: …oot@centos01 ~]#̲ bash [root@cen…Product $Version"

[root@centos01 ~]# exit
exit
[root@centos01 ~]# export Product Version 将两个变量设为全局变量
[root@centos01 ~]# bash
[root@centos01 ~]# echo “$Product $Version”
Python 2.7.13


[root@centos01 ~]# export FQDN=“www.jb-aptech.com.cn” 导出全局变量的同事为变量赋值
[root@centos01 ~]# echo $FQDN
www.jb-aptech.com.cn
11.数值变量的运算
[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
12.环境变量
[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
13.位置变量
[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
14.预定义变量
[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-*
ls: 无法访问beifen-*: 没有那个文件或目录

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值