Shell

Shell

1.什么时shell

shell时用户和内核交互的工具,用户通过对shell发送指令,来指导内核完成操作,来调用相应的程序来完成工作

2.什么时shell脚本

安装一定逻辑关系记录明令的文件
在此文件有可执行权限的情况下可以用文件名称发起脚本内记录明令的执行
shell脚本是一种解释形语言,文件内记录的动作需要解释器shell

3.为什么要写shell脚本

通过编写shell脚本可以批量的完成想要的操作,而不需要通过输入很多的命令,只要一次编写脚本,就可以多次调用,来完成工作,从而提高工作效率

4.每个脚本第一行的作用

#!/bin/bash
脚本开始的写法,这是脚本使用的解释器,也就是默认脚本运行时开启的子shell

5.脚本的编写格式

1)脚本中应添加脚本的说明信息
Author        :lee
Email        :lee@westos.com
Create_Date    :2017-08-21
Vesion        :1.0
Description    :test scripts

2)脚本中尽量不要使用中文,哪怕用拼音代替
3)脚本中出现的()|[]|{}|<>等等成对出现的符号要一次打出,并且
内容中的字符与这些符号要有空格
4)脚本中的语句要一次写完在丰富内容
5)语句中使用的动作要缩进写入,使脚本易读

6.如何运行脚本

脚本执行的方式
方法一:
    sh 脚本名称
方法二:
    chmod +x 脚本
    脚本名称调用

给脚本中加入固定的头部说明,使脚本看起来更规范

vim /etc/vimrc

65map <F4> ms:call WESTOS()<cr>'s
66 function WESTOS()
67         call append(0,"######################################")
68         call append(1,"#Author        :xxx                  #")
69         call append(2,"#Email         :xxx@westos.com       #")
70         call append(3,"#Version       :1.0                  #")
71         call append(4,"#Create_Date:".strftime("%Y-%m-%d"."              #"))
72         call append(5,"#Description   :xxx                  #")
73         call append(6,"######################################")
74         call append(7,"   ")
75         call append(8,"#!/usr/bin/env bash")
76 endfunction


编辑脚本
vim /shell/shell.sh
按下F4,系统会调用刚才写的WESTOS定义函数,就在脚本里插入刚才所编辑的文本
F4
######################################
#Author        :xxx                  #
#Email         :xxx@westos.com       #
#Version       :1.0                  #
#Create_Date   :2017-12-11           #
#Description   :xxx                  #
######################################

#!/usr/bin/env bash

转义字符
\:单个字符转义
例:[root@localhost shell]#echo \#\#\#
   ###
"":弱引用
例:[root@localhost shell]# echo "my hostname is `hostname`"  
   my hostname is localhost.localdomain
'':强引用   
例:[root@localhost shell]# echo 'my hostname is `hostname`'
   my hostname is `hostname`

打补丁
yum install patch -y   


[root@localhost shell]# vim hello             ##编辑hello文件
hello world
[root@localhost shell]# vim hello.new         ##编辑hello.new文件
hello world
456


[root@localhost shell]# diff -u hello hello.new > hello.path   
[root@localhost shell]# patch hello hello.path
patching file hello
[root@localhost shell]# cat hello
hello world
456
[root@localhost shell]# cat hello.new
hello world
456

此时发现,通过同步,hello文件中和hello.new文件的内容相同
sed命令


sed 's/sbin/westos' passwd   ##将passwd文件中sbin全部改为westos


sed '1,5s/sbin/westos/g' passwd   ##将passwd文件中第1行到第5行中的sbin全部改为westos


sed '/lp/,/mail/s/sbin/westos/g' passwd   ##将passwd文件中从用户lp开始到用户mail,将sbin全部改为westos

编写脚本,通过修改配置文件的方式来修改httpd服务端口

vim apach.sh
#!/usr/bin/env bash
Port_Opt=`grep "^Listen"`    ##设置Port_Opt变量,变量内容为过滤以Listen开头的行的内容
sed "s/$Port_Opt/Listen\ $1/g" -i /etc/httpd/conf/httpd.conf  
##将http默认配置文件中原有的端口改为新设置的端口,其实是将Listen 80改为Listen 8080,是对以Listen开头的整行内容进行修改
echo change $Port_Opt to Listen $1      ##回显将原有的端口改成新的端口
systemctl restart httpd

使用另外一个文件中的内容来建立用户
vim username
  user1
  user2
  user3


vim create_user.sh
  #!/usr/bin/env bash
  for name in `cat username `
  do  
    useradd $name
  done


sh -x create_user.sh username  ##使用create_user.sh脚本,利用username中的用户名称来建立文件,-x显示建立过程
++ cat username
+ for name in '`cat username `'
+ useradd user1
+ for name in '`cat username `'
+ useradd user2
+ for name in '`cat username `'
+ useradd user3

明令别名设定
alias
当前环境配置:alias xie='vim'  
这种只在当前环境下生效,当退出当前环境或者重启之后,设置就会失效


当前用户配置vim ~/.bashrc
alias xie='vim'
source ~/.bashrc
由于是修改用户家目录下的配置文件,所以只对当前用户生效,当切换到其他用户,设置就会失效



所有用户配置:vim /etc/bashrc
alias xie='vim'
source /etc/bashrc
这种是修改系统配置文件,所以是对所有用户生效,所有用户都可以使用别名


取消别名
unalias xie

利用脚本设置用户名密码
vim create_user.sh
#!/usr/bin/env bash
useradd $1
echo $2 | passwd --stdin $1


sh create_user.sh hello 123

使用脚本建立用户,设置密码,删除用户,交互式的,可多次创建或者删除,直到手动退出,才会结束
vim create_user.sh
#!/usr/bin/env bash
read -p "what do you want to do: " ACTION
[ $ACTION == "create" ]&&(                                ##建立用户,设置密码
read -p "please input your username:" USERNAME
read -p "please input your password:" -s PASSWORD
echo ""
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
)
[ $ACTION == "delete" ]&&(                                ##删除用户
read -p "please input your username:" USERNAME
userdel -r $USERNAME
)
[ $ACTION == "exit" ]&&(                                  ##退出
exit
)


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值