zoukankan      html  css  js  c++  java
  • shell(一)

    1.写脚本之前的思路

    考虑

    脚本统一管理

    权限:用户执行权限

    清空错误文件

    错误提示

    脚本通用性

    2.2 统一脚本目录

    1 mkdir /server/scripts
    2 cd /server/scripts

    2.3 使用vim创建脚本
    文件名规范,名字要有意义,结尾以.sh结尾
    vim oldboyedu.sh
    规范的shell脚本,指定解释器

     1 1.    放在统一目录
     2 2.    脚本以.sh结尾
     3 3.    开头指定脚本解释器
     4 4.    开头加版权等信息,可以配置~/.vimrc文件自动添加
     5 5.    脚本不要用中文注释,尽量用英文注释
     6 6.    代码书写优秀习惯
     7     a.    成对的内容一次性写出来,防止遗漏,[] 、 '' "" 8     b.    [] 两端有空格 先输入[] 输入退格 两个空格 出退格
     9     c.    流程控制语句一次写完,再添加内容
    10     d.    通过缩进让代码易读
    11     e.    脚本中的引号都是英文状态下的引号,其他字符也是英文状态,好的习惯可以让我们避免很多不必要的麻烦,提高工作效率

     2.4 执行脚本的方法

    1.    sh /server/scritps/test.sh
    2.    chmod +x /server/scripts/test.sh && /server/scripts/test.sh
    3.    source /server/scripts/test.sh
    4.     .  /server/scripts/test.sh . 与 source 等价

    结论:sh命令在当前shell(进程A)执行脚本会创建一个新的shell(进程B),然后执行脚本内容,最后将脚本的最终结果传给当前shell(进程A) 
    source直接在当前shell(进程A)中执行脚本,所以脚本的变量在当前shell(进程A)中生效
    使用场景
    #引入文件时
        source /etc/profile
        source /etc/init.d/functions
    其他 sh /server/scripts/test.sh

    2.5 全局变量

    1.    全局变量,到处可用。在当前shell及所有子shell窗口全部局生效。在新开的shell窗口后效,需要写入到文件中。
    2.    通过export 添加环境变量

    1 #此处为临时添加
    2 #方法1
    3 export HELLO="hello World"
    4 #方法2
    5 HELLO ="hello World"
    6 export HELLO
    View Code

    3.    到处生效 写入配置文件中 echo 'export hello=hello World' >>/etc/profile
    4.    unset 删除变量

    2.8 环境变量配置文件
    全局环境变量设置文件 /etc/profile /etc/bashrc
    局部环境变量设置文件 ~/.bash_profile ~/.bashrc
    开机后环境变量启动顺序
    #将环境变量时间放到文件结尾

    1 [root@python-memcache ~]# env | egrep "[Bb]ash|profile"|awk -F "=" 'NR>1{print $1,$2}'|sort -nk2|column
    2 -t
    3 profile           1512188041
    4 bashrc            1512188042
    5 rootBashrc        1512188043
    6 rootBash_profile  1512188044
    View Code

     #将环境变量时间放到文件开头,

    1 [root@python-memcache ~]# env | egrep "[Bb]ash|profile"|awk -F "=" 'NR>1{print $1,$2}'|sort -nk2|column -t
    2 profile           1512193808
    3 rootBash_profile  1512193809
    4 rootBashrc        1512193810
    5 bashrc            1512193811
    View Code

     #利用文件属性查看加载过程

    1 for file in /etc/profile /etc/bashrc /root/.bashrc /root/.bash_profile ; do
    2     stat ${file} | awk 'NR==5{print $2$3};NR==1{print  $0 }'
    3 done
    View Code

     2.9 老shell漏洞检测
    以下代码在系统中运行,如果出现两行及以上,则存在shell漏洞,需要升级shell

    1 env x='() { :;}; echo be careful' bash -c "echo this is a test"
    View Code

     3.0 top免交互

    top -bn 2 | head -n 60 

  • 相关阅读:
    linux常用命令
    mysql 开发基础系列20 事务控制和锁定语句(上)
    sql server 性能调优之 资源等待 CXPACKET
    mysql 开发基础系列19 触发器
    mysql 开发基础系列18 存储过程和函数(下)
    mysql 开发基础系列17 存储过程和函数(上)
    sql server 性能调优之 资源等待PAGEIOLATCH
    mysql 开发基础系列16 视图
    mysql 开发基础系列15 索引的设计和使用
    sql server 性能调优之 当前用户请求分析 (1)
  • 原文地址:https://www.cnblogs.com/anyux/p/7976654.html
Copyright © 2011-2022 走看看