码迷,mamicode.com
首页 > 系统相关 > 详细

10个实战及面试常用的shell脚本--1

时间:2018-04-21 00:24:15      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:shell

###10个实战及面试常用的shell脚本

  • 写脚本之前的一些注意事项
    • 1.开头加解释器:
      #!/bin/bash
    • 2.语法缩进,使用4个空格,多加注视说明
    • 3.命名规则:
      变量名大写
      局部变量小写
      函数名小写
      名字体现出实际作用
    • 4.默认变量是全局的,在函数中的变量local指定为局部变量,避免污染其他作用域
    • 5.两个命令帮助调试脚本:
      set -e  遇到执行非0时退出脚本
      set -x  打印执行过程
    • 6.写完后要测试再到生产
  • 1.获取本机ip地址(有的需要根据机器来修改,不全通用)
    method 1: 
    ifconfig eth0 | grep "inet addr" | awk ‘{ print $2}‘ | awk -F: ‘{print $2}‘
    method 2:
    ifconfig eth0|grep ‘inet addr:‘|cut -d: -f2|cut -d " " -f1
    method 3:
     ifconfig eth0|sed -nr ‘2s#^.*addr:(.*) Bca.*$#\1#g‘p
    method 4:
    ifconfig eth0|sed -n ‘/inet /{s/.*addr://;s/ .*//;p}‘
    method 5:
    ifconfig eth0|awk ‘/inet addr:/ {print $2}‘|awk -F: ‘{print $2}‘
    method 6:
    ip add|awk -F ‘[ /]+‘ ‘NR==8 {print $3}‘

10个实战及面试常用的shell脚本--1

标签:shell

原文地址:http://blog.51cto.com/13713370/2105995

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!