10個實戰及面試常用的shell腳本--1
阿新 • • 發佈:2018-04-21
shell###10個實戰及面試常用的shell腳本
- 寫腳本之前的一些
註意事項
- 1.開頭加解釋器:
#!/bin/bash
- 2.語法縮進,使用4個空格,多加註視說明
- 3.命名規則:
變量名大寫 局部變量小寫 函數名小寫 名字體現出實際作用
- 4.默認變量是全局的,在函數中的變量local指定為局部變量,避免汙染其他作用域
- 5.兩個命令幫助調試腳本:
set -e 遇到執行非0時退出腳本 set -x 打印執行過程
- 6.寫完後要測試再到生產
- 1.開頭加解釋器:
- 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