Shell

Shell

1.什么时shell

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

2.什么时shell脚本

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

3.为什么要写shell脚本

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

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

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

5.脚本的编写格式

1)脚本中应添加脚本的说明信息
Author        :lee
Email        :[email protected]
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         :[email protected]       #")
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

Shell

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

#!/usr/bin/env bash

Shell

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

Shell

打补丁
yum install patch -y   

Shell
[[email protected] shell]# vim hello             ##编辑hello文件
hello world
[[email protected] shell]# vim hello.new         ##编辑hello.new文件
hello world
456

Shell
[[email protected] shell]# diff -u hello hello.new > hello.path   
[[email protected] shell]# patch hello hello.path
patching file hello
[[email protected] shell]# cat hello
hello world
456
[[email protected] shell]# cat hello.new
hello world
456

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

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

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

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

Shell

编写脚本,通过修改配置文件的方式来修改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

Shell

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

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

Shell
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

Shell

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

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

Shell

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

ShellShell
取消别名
unalias xie

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

Shell
sh create_user.sh hello 123

Shell

使用脚本建立用户,设置密码,删除用户,交互式的,可多次创建或者删除,直到手动退出,才会结束
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
)

ShellShell