新手小白的第乙個shell指令碼

2021-10-03 10:01:37 字數 2777 閱讀 8413

獲取cpu、記憶體基本資訊匯入到資料庫指令碼(第一次寫,希望大家給指點一下)

#!/bin/bash

#使用前請先變更password變數裡的密碼

hostname=「localhost」 #資料庫資訊

port=「3306」

username=「root」

password=「qianfeng@123」

#免密登陸資料庫

echo [mysql] >>/etc/my.cnf

echo user=root>>/etc/my.cnf

echo password=』$password』 >>/etc/my.cnf

#安裝dmi

yum -y install dmidecode &>/dev/null

#建庫建表

mysql -e 「create database if not exists phy_server;」

mysql -e "use phy_server;create table if not exists server(id int not null auto_increment primary key,hostname varchar(10),ip varchar(50),cpu_name varchar(30),cpu_py int,cpu_core_num int) "

mysql -e 「use phy_server;create table if not exists memory(id int not null auto_increment primary key ,locator varchar(30),size varchar(20),speed varchar(20),type varchar(10),server_id int,foreign key(server_id) references server(id));」

#獲取ip位址

ipadd=$(ip a | awk 『/inet / && ! /127.0.0.1/ 』)

#獲取cpu_name

cpu_name=$(grep 『model name』 /proc/cpuinfo | uniq|awk 『』)

#獲取cpu_顆數

cpu_py=$(grep 『physical id』 /proc/cpuinfo | sort -u | wc -l)

#獲取cpu_核心數

cpu_core_num=$(grep 『cpu cores』 /proc/cpuinfo | uniq |awk 『』)

#獲取主機名

hostname=$(cat /etc/hostname )

#獲取locator

locator=$(dmidecode -q -t memory|awk -f: 『/locator/ && ! /bank/ 』)

#獲取記憶體size

size=$(dmidecode -q -t memory|awk -f: 『/size/ 』)

#獲取記憶體speed

speed=$(dmidecode -q -t memory|awk -f: 『/speed/ && ! /configured memory/』)

#獲取type

type=$(dmidecode -q -t memory|awk -f: 『/type:/ && ! /error correction/』)

mysql -e 「insert into phy_server.server(hostname,ip,cpu_name,cpu_py,cpu_core_num) values(『hos

tnam

e′,′

hostname','

hostna

me′,

′ipadd』,『cpu

name

′,

′cpu_name','

cpun​a

me′,

′cpu_py』,『cpu

core

num′

);"m

ysql

−e"i

nser

tint

ophy

serv

er.m

emor

y(lo

cato

r,si

ze,s

peed

,typ

e,se

rver

id)v

alue

s(

′cpu_core_num');" mysql -e "insert into phy_server.memory(locator,size,speed,type,server_id) values('

cpuc​o

ren​

um′)

;"my

sql−

e"in

sert

into

phys

​erv

er.m

emor

y(lo

cato

r,si

ze,s

peed

,typ

e,se

rver

i​d)

valu

es(′

locator』,『siz

e′,′

size','

size′,

′speed』,』$type』,1);」

mysql -e 「select *from phy_server.server;」

mysql -e 「select *from phy_server.memory;」

第乙個Shell指令碼

開啟文字編輯器,新建乙個檔案,擴充套件名為sh sh代表shell 副檔名並不影響指令碼執行,見名知意就好,如果你用php寫shell 指令碼,副檔名就用php好了。輸入一些 bin bash echo hello world 是乙個約定的標記,它告訴系統這個指令碼需要什麼直譯器來執行,即使用哪一種...

第乙個shell指令碼

首先宣告,我的linux系統是centeros 64bit的 開啟終端,cd mnt hgfs share 進入以下目錄 mkdir shellfolder 建立資料夾shellfolder touch runstusys.sh 建立乙個shell程式 vi runstusys.sh 進行編輯,並輸...

第乙個shell指令碼

hostname term hello shell histsize ssh client world cvs rsh path 通過shell讀取文字檔案,並到系統變數中查詢是否存在 shell命令如下 bin sh filename env var.txt num cat filename wc...