整理一下日常開發(fā)中l(wèi)inux下的常用命令:
-
根據(jù)進(jìn)程名找到進(jìn)程id 并且kill掉
$ps ux | grep demo userzhao+ 378 0.0 0.0 10540 552 tty1 S 21:30 0:00 ./demo1 # 找到進(jìn)程id $ps ux | grep demo | grep -v grep | awk '{print $2}' 378 # 然后kill掉 xargs 是個(gè)好東西 # xargs -t 表示輸出對應(yīng)的命令 $ps ux | grep demo | grep -v grep | awk '{print $2}' | xargs -t kill -9 kill -9 378
-
grep某個(gè)區(qū)間時(shí)間段的日志
$ grep "2019-05-21 14:42:4[5-6]" dpkg.log 2019-05-21 14:42:45 status not-installed linux-headers-generic:amd64 <none> 2019-05-21 14:42:45 status installed linux-headers-4.15.0-50-generic:amd64 4.15.0-50.54 2019-05-21 14:42:45 remove linux-headers-4.15.0-50-generic:amd64 4.15.0-50.54 <none> 2019-05-21 14:42:45 status half-configured linux-headers-4.15.0-50-generic:amd64 4.15.0-50.54 2019-05-21 14:42:45 status half-installed linux-headers-4.15.0-50-generic:amd64 4.15.0-50.54 2019-05-21 14:42:46 status config-files linux-headers-4.15.0-50-generic:amd64 4.15.0-50.54 2019-05-21 14:42:46 status config-files linux-headers-4.15.0-50-generic:amd64 4.15.0-50.54 2019-05-21 14:42:46 status config-files linux-headers-4.15.0-50-generic:amd64 4.15.0-50.54 2019-05-21 14:42:46 status not-installed linux-headers-4.15.0-50- generic:amd64 <none> 2019-05-21 14:42:46 status installed linux-headers-4.15.0-50:all 4.15.0-50.54
# 查詢cpu 80~89之間的日志 grep "cpu : 8[0-9]%" *.log
cat out.log [20210708 23:29:01.645568] cpu usage: 0% [20210708 23:29:06.642943] cpu usage: 1% [20210708 23:29:11.643244] cpu usage: 2% [20210708 23:29:16.640893] cpu usage: 3% [20210708 23:29:21.642805] cpu usage: 4% [20210708 23:29:26.646420] cpu usage: 5% // 查看cpu操過3%的記錄 grep "cpu" out.log | awk -F'%' '{print $1}'|awk '{if ($NF > 3) print $0}'
其他的grep,find的相關(guān)的alias:
http://www.reibang.com/p/ea22a699e456 -
awk使用substr打印前n個(gè)字符
$ cat /etc/passwd | awk -F : '{print substr($1,0,3)}' roo dae bin sys syn gam man
-
性能指標(biāo)查詢的相關(guān)命令
top -b > profile.log # 記錄某個(gè)時(shí)間的cpu和內(nèi)存狀態(tài) top -H -p pid # 查看某個(gè)進(jìn)程下面的線程cpu狀態(tài) free -g # 查看內(nèi)存 iotop # 查看io pidstat -p pid # 查看某個(gè)進(jìn)程
-
mysql 在shell上面直接執(zhí)行查詢
mysql -uroot -e "use information_schema;select * from COLUMNS limit 1;"
-
查找相關(guān)的命令 grep的結(jié)果高亮顯示 --color=auto
// 查找文件中包含某個(gè)字符串的 grep "xxxxx" xxx.log --color=auto grep -rn "xxxx" xxx.log // 查找包含某個(gè)字符串的文件名 find . -name "table*" // # 尋找是table開頭的文件名 find . -name "*table*" // # 尋找包含table的文件名
-
統(tǒng)計(jì)文件中單詞的數(shù)量
$ cat test.txt welcome shell welcome to shell shell #方法1: $ cat test.txt| awk '{for(i=1; i<=NF;i++){a[$i]++}} END{for(key in a){print key" "a[key]}}' to 1 welcome 2 shell 3 #方法2: $ cat test.txt | tr " " "\n" | awk '{if($1 != ""){a[$1]++}} END{for(key in a){print key" "a[key]}}' to 1 welcome 2 shell 3
-
sort 排序
$ cat out.txt 1 1000 40 2 900 20 3 600 80 # 默認(rèn)從小到大 $ sort out.txt 1 1000 40 2 900 20 3 600 80 # 相反的排序 $ sort -r out.txt 3 600 80 2 900 20 1 1000 40 # 根據(jù)第3列排序 $ sort -k 3n out.txt 2 900 20 1 1000 40 3 600 80
-
uniq去重
cat testfile hello world friend hello world hello #cat testfile | sort |uniq friend hello world
shell的基本語法
- 定義變量的時(shí)候购笆,不能加空格
a=100 # 不能加空格
- 條件判斷必須要加空格
#判斷等于
if [ $a1 = $b1 ] #必須加上空格
then
echo "eq"
else
echo "not eq"
fi
- 其他的語法:
https://github.com/zhaozhengcoder/CoderNoteBook/blob/master/note/bash%26shell%E7%BC%96%E7%A8%8B.md
- 簡單的shell操作
# 這基本上是變量的替換
path="/var/www"
cd $path
cp -r $src_code_path $build_name
- shell操作數(shù)據(jù)庫
# 使用shell再數(shù)據(jù)庫上面創(chuàng)建database
src_db_user=root
build_db_name=test
mysql -u $src_db_user << EOF 2>/dev/null
CREATE DATABASE $build_db_name
EOF
if [ $? -eq 0 ]
then
echo "created DB"
else
echo "DB already exists, sh exit"
exit 1;
fi
Shell中通常將 EOF與 << 結(jié)合使用减宣,表示后續(xù)的輸入作為子命令或子Shell的輸入悟衩,直到遇到EOF為止狰晚,再返回到主調(diào)Shell盏筐。 可以把EOF替換成其他東西,意思是把內(nèi)容當(dāng)作標(biāo)準(zhǔn)輸入傳給程序晋柱。
回顧一下< <的用法恩脂。當(dāng)shell看到< <的時(shí)候,它就會知道下一個(gè)詞是一個(gè)分界符趣斤。在該分界符以后的內(nèi)容都被當(dāng)作輸入,直到shell又看到該分界符(位于單獨(dú)的一行)黎休。這個(gè)分界符可以是你所定義的任何字符串浓领。
參考:https://blog.csdn.net/wyl9527/article/details/72655277
- shell結(jié)合sed命令
src_db_host=10.12.235.102
build_db_host=10.12.235.102
# sed 替換到匹配到的內(nèi)容
sed -i "s/host=${src_db_host}/user=${build_db_host}/g" file
# sed 替換到匹配到這一行
sed -i "/${src_db_host} /c ${build_db_host}" file
- shell拿到命令執(zhí)行的結(jié)果
# 打印一個(gè)目錄下面所有的php文件
for file in `ls | grep .php`
do
echo $file
done
- shell中常見到的
0:表示鍵盤輸入(stdin)
1:表示標(biāo)準(zhǔn)輸出(stdout),系統(tǒng)默認(rèn)是1
2:表示錯誤輸出(stderr)
1>/dev/null:表示標(biāo)準(zhǔn)輸出重定向到空設(shè)備文件玉凯。
也就是不輸出任何信息到終端,不顯示任何信息。
2>&1:表示標(biāo)準(zhǔn)錯誤輸出重定向等同于標(biāo)準(zhǔn)輸出联贩。
因?yàn)橹皹?biāo)準(zhǔn)輸出已經(jīng)重定向到了空設(shè)備文件,所以標(biāo)準(zhǔn)錯誤輸出也重定向到空設(shè)備文件漫仆。
-
shell 結(jié)合ini文件
example.ini:DBNAME=test DBUSER=scott DBPASSWORD=tiger
example.sh
#!/bin/bash #Including .ini file . example.ini echo "${DBNAME} ${DBUSER} ${DBPASSWORD}"
shell的符號
$# 是傳給腳本的參數(shù)個(gè)數(shù)
$0 是腳本本身的名字
$1 是傳遞給該shell腳本的第一個(gè)參數(shù)
$2 是傳遞給該shell腳本的第二個(gè)參數(shù)
$@ 是傳給腳本的所有參數(shù)的列表
$* 是以一個(gè)單字符串顯示所有向腳本傳遞的參數(shù),與位置變量不同泪幌,參數(shù)可超過9個(gè)
$$ 是腳本運(yùn)行的當(dāng)前進(jìn)程ID號
$? 是顯示最后命令的退出狀態(tài)盲厌,0表示沒有錯誤,其他表示有錯誤
shell函數(shù)
- shell函數(shù)的例子
#!/bin/sh
# Define your function here
Hello () {
echo "Hello World"
}
# Invoke your function
Hello
- shell函數(shù)處理輸入和返回輸出
# input /var/www/
# output \/var\/www\/
handle_path_str()
{
echo $1 | sed 's#\/#\\\/#g'
}
build_path= /var/www/
build_path_str="$(handle_path_str $build_path)"