shell系统学习一--基本知识

来源:互联网 发布:网络爬虫 python 编辑:程序博客网 时间:2024/06/06 03:41
一、学习背景
    做运维很多年,从网管、系统、数据库中间使用了很多脚本,感觉都是片段的学习了shell,
也能够使用和编写一些的脚本,但是总觉得很多不足,这次系统学习一下。
shell是自动化运维的基础
1、vi/vim
2、liunx 150个常用命令熟练使用
3、常见的liux网络服务部署及排错:crond、nfs、rsync、inotify、lanmp、sersync、ssh等


    shell是一个命令解释器:外围应用程序---命令解释器shell---系统核心---硬件
    shell的英文就是贝壳
    Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器)。它类似于DOS下
的command和后来的cmd.exe。它接收用户命令,然后调用相应的应用程序。


二、实例--清空系统log:


1、简单的情况var下的log
cd /var/logs
cat /dev/null>messages
echo "logs cleaned up."
提示:/var/log/messages是系统的日志文件,需要使用root。


2、完善以上脚本
#!/bin/bash
#清除日志脚本
LOG_DIR=/var/log
#$UID为0的时候,用户才具有root用户权限
ROOT_UID=0
#使用root用户运行
if [ "$UID" -ne "$ROOT_UID" ]
then
   echo "Must be root to run this script."
   exit 1
fi
   cd $LOG_DIR || {
     echo "Cannot change to necessary diectory." >&2
     exit 1
   }
   cat /dev/null > messages && echo "Logs cleaned up."
   exit 0
#退出返回0表示成功。
[root@node01 ~]# sh clear.sh 
Logs cleaned up.
[root@node01 ~]# cat /var/log/messages 
[root@node01 ~]# 
或者
[root@node01 ~]# chmod +x clear.sh


非root用户运行
[wolf@node01 /]$ sh clear.sh 
Must be root to run this script.


3、清空log的3种方法:
echo >/var/log/messages
>/var/log/messages
cat /dev/null>/var/log/messages


4、dmesg命令
dmesg - print or control the kernel ring buffer


三、shell擅长处理纯文本类型的数据。
1、配置文件
2、log
3、安装各种服务
shell在运维工作中的地位很高,除非不用linux。


四、脚本语言种类
1、shell
[root@node01 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
shell是一个弱类型语言,较为通用的shell有标准的Bourne shell(sh)和C shell


2、其他人员
php--也是脚本语言,专注于web语言开发,也可以处理系统log、配置文件等。
perl--比shell强大很多,语法灵活、复杂,实现方式很多,不易读,团队协作困难。
python--近几年比较火的语言,脚本开发,web开发等。


3、shell与php、perl、python优势
    shell的优势在于处理操作系统底层的业务,因为有大量的系统命令作为支撑,2000多个命令
都是shell编程的有力支撑,特别是grep、awk、sed等。如:一些软件安装、优化、监控脚本。
shell的优势在简单,高效。
   php和python优势在于开发运维工具。web界面的管理工具,以及web业务的开发等。
   
4、常见系统默认shell
   linux默认的是bash(Bourne Again shell)
   solaris和freebsd默认的是bourne shell(sh)
   AIX默认的是korn shell(ksh)
   HP-UX默认的是POSIX shell(sh)
查看方法:echo $SHELL
[root@node01 ~]# echo $SHELL
/bin/bash


五、shell脚本的建立和执行
   linux shell脚本通常在编辑器vi、vim中编写。
脚本开头第一行(说明由那个程序解释器来执行脚本中的内容。)
#!/bin/bash
或者
#!/bin/sh
   这里#!称为幻数,必须在第一行,如果在第二行或者后面为注释。如果不写,系统默认是什么
shell就用那个shell来解释。
[root@node01 ~]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 Jul  7 07:30 /bin/sh -> bash
sh是bash的软连接,建议写#!/bin/bash,当然这个属于个人习惯。


bash版本:
[root@node01 ~]# bash --version
bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>


This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


有一个bash漏洞--破壳
破壳漏洞修复:yum update bash


shell脚本的执行
    当shell脚本以非交互的方式运行时(文件方式),他会先查找环境变量ENV,改变量指定了一个
环境文件(通常是.bashrc、.bash_profile、/etc/bashrc、/etc/profile),然后从该变量文件开始
执行,当读取了ENV文件后,SHELL才开始执行shell中的内容。


    在计划任务crontab中执行时,需要把系统环境变量再脚本中从新定义。


shell脚本的执行三种方式:
a、bash script-name或者sh script-name   可以没有执行权限
b、path/script-name或者./script-name     +x 需要执行权限
c、source script-name或者.script-name    用source或者.执行,可以把脚本里的变量保留当前环境。
   c举两个例
[root@node01 ~]# vi test.sh 
user='whoami'
[root@node01 ~]# sh test.sh 
[root@node01 ~]# echo $user
[root@node01 ~]# . test.sh
[root@node01 ~]# echo $user
root


很多系统脚本就是在脚本里加上.或者source,使得直接生效
[root@node01 ~]# vi /etc/rc.d/init.d/functions
#by wolf
wolf(){
echo "this is wolf,you are $1."    $1是传参数
}
[root@node01 ~]# vi 1.sh
#!/bin/sh
source /etc/init.d/functions
wolf yujing
[root@node01 ~]# sh 1.sh 
this is wolf,you are yujing.


传参
[root@node01 ~]# vi 1.sh
#!/bin/sh
source /etc/init.d/functions
wolf $1                            传参
[root@node01 ~]# sh 1.sh yujing
this is wolf,you are yujing.
[root@node01 ~]# sh 1.sh zhangs
this is wolf,you are zhangs.


六、优秀习惯
a、开头指定解释器
/bin/sh
/bin/bash
b、开头加版权信息(可配置vim编辑文件时自动加上以上信息,方法是修改~/.vimrc配置文件
#Date
#Author
#Mail
#Functions
#Version
c、脚本中不用中文注解
  尽量用英文注解,防止本机或者切换系统环境后中文乱码的困扰。
d、脚本以.sh为扩展名。
e、代码书写有些习惯
   成对符号内容的一次写出来:如[] {} '' ""  防止遗漏。
   []中括号两端要有空格,书写时即可留出空格[    ],然后在退出格书写内容
   流程控制语句一次书写完,在添加内容:如:
       if 条件内容
          then
            内容
       fi
       for循环格式一次完成(while、until、case语句也是一样)
       for
       do
          内容
       done  
f、通用缩进代码易读
 if 条件内容
    then
      内容
 fi