shell 總結3

$HOME/.config/user-dirs.dirs 爲gnome的默認軟件目錄 可以自己設置

 

雙屏下切換焦點不用來回的移動鼠標了

#! bin/python
import subprocess
# just a helper function
get = lambda cmd: subprocess.check_output(cmd).decode("utf-8")
# get the current mouse position
current = [int(n) for n in [it.split(":")[1] for it in get(["xdotool", "getmouselocation"]).split()[:2]]]
# get the x/y size of the left screen
screendata = [(s.split("x")[0], s.split("x")[1].split("+")[0]) for s in get(["xrandr"]).split() if "+0+0" in s ][0]
xy = [int(n) for n in screendata]
# see if the mouse is on the left- or right screen
if current[0] < xy[0]:
    # if the mouse currently is on the left screen, move it to the right (from the middle of the left screen)
    command = ["xdotool", "mousemove", "--sync", str(current[0]+xy[0]), str(xy[1]/2)]
else:
    # if the mouse currently is on the left screen, move it to the right (from the middle of the left screen)
    command = ["xdotool", "mousemove", "--sync", str(current[0]-xy[0]), str(xy[1]/2)]

subprocess.Popen(command)
# optional: click after the mouse move: comment out if not needed / wanted
subprocess.Popen(["xdotool", "click", "1"])

 

雙屏同步切換工作區 需要tweak設置下

 

 

是否忍受夠了不停的alt tab來切換 打開的應用,尤其是打開的應用多的時候,雖然dock+workspace可以緩解一下,

但我想要我需要的窗口立馬到我面前來so 解決辦法是 wmctrl 

wmctrl -a fire 會自動匹配 wmctrl -a chrom

wmctrl  -c fire 自動關閉firefox 

 

短的命令用別名

過長的命令用腳本封裝,放到PATH裏面

 

tar 打包時忽略某些文件或者路徑

tar -zcvf xxx.tgz --exclude path --exclude path xxx

切記 path 後面沒有/ 即格式不能爲 path/

且exclude 在文件前面  xxx

 

tar 打包時不保留原路徑 往往tar打包後,會發現會帶着原路徑

 tar -zcvf   xxx.tgz  -C  .cfg/abc/xxx   . 

這樣就不會保留原路徑 注意 最後面有個 . 即說明在相對路徑

 

show create table 展示創建表的信息

 

grep xxx * 
grep -n xxx * 
直接在grep查文件

. \. 轉意

\mysql –uroot –p123456 -Dtest<C:\test.sql  命令行
Mysql>source 【sql腳本文件的路徑全名】 或 Mysql>\. 【sql腳本文件的路徑全名】mysql終端

 

發現了一個很有趣的命令 open 

它會調用默認的軟件去打開相應的文件

比如 open xxx.pdf 就會調用 evince 去打開

open . 會打開當前文件夾

更讚的是會在後臺運行  而不是,一旦終端推出,就會停掉

更能直接打開 網址連接

open https://www.baidu.com 

就會自動調用默認瀏覽器打開鏈接

 

/etc/shells 包含了一些shell 包括 bash fish zsh tmux 等 如果沒有收到添加 /bin/bash

 

cat xxx | sort | uniq -c shell wordcount 

 

du -sh . 和 du -sh *顯示的大小不同 今天被這個給困惑住了,因爲 du -sh 是當前目錄下全部文件的大小包括隱藏文件 .*

du -sh * 只是可見文件的大小

 

for n in {11..15} 等同於 for n in $(seq 11 15)

 

shell 字符串截取

str="i,am,a,good,man"

 echo ${str#*,} //返回第一個分隔符之後的內容

am,a,good,man 

echo ${str##*,} //返回分隔符最後的內容

man

echo ${str%,*} //從右往左返回第一個分隔符之後的內容

i,am,a,good

echo ${str%%,*} //從右往左返回最後一個分隔符之後的內容

 

shell字符串直接連起來就是拼接 aaabbb 但是需要注意的地方是 如果引用變量如下面這種清空

a="sfgsdf"

$a_sdfgh

也就是說如果遇到下劃線或者其他的特殊字符這種引用方式就不生效了

需要 ${a}_sdns需要對引用變量加上大括號

 

shell數組 

與大部分編程語言類似,數組元素的下標由0開始。

Shell 數組用括號來表示,元素用"空格"符號分割開,語法格式如下

myArray=("a","b","c")

${array_name[index]}

使用@或者*可以獲取數組中的全部元素

${#myArray[@]} 獲取長度

shell 分割字符串轉換成數組

    方法一: 藉助於{str//,/}來處理

    [root@host ~]# str="ONE,TWO,THREE,FOUR"
    [root@host ~]# arr=(${str//,/})
    [root@host ~]# echo ${arr[@]}
    ONE TWO THREE FOUR

    方法二: 藉助於tr命令來處理

    [root@host ~]# str="ONE,TWO,THREE,FOUR"
    [root@host ~]# arr=(`echo $str | tr ',' ' '`) 
    [root@host ~]# echo ${arr[@]}
    ONE TWO THREE FOUR

    方法三: 藉助於awk命令來處理

    [root@host ~]# str="ONE,TWO,THREE,FOUR"
    [root@host ~]# arr=($(echo $str | awk 'BEGIN{FS=",";OFS=" "} {print $1,$2,$3,$4}'))
    [root@host ~]# echo ${str[*]}

    方法四: 藉助於IFS來處理分隔符

    [root@host ~]# str="ONE,TWO,THREE,FOUR"
    [root@host ~]# IFS=","
    [root@host ~]# arr=(str)
    [root@host ~]# echo ${str[@]}
————————————————
 

shell 中數學運算需要((1+2))兩個括號

 

() 數組 內部元素由空格隔開從零開始

{}

$() 結果賦值 和 `` 斜單引號作用一樣  a=$(ls /root )  a=`ls /root`

${} a=1  變量引用 一般直接 $a 但是如果有下劃線需要加上 ${a}

((1+2)) 數學運算 

使用@或者*可以獲取數組中的全部元素

${myArray[@]} 全部元素 for f in 

${#myArray[@]} 獲取長度

 

str=H9.jpg
echo ${str:0:1}
H 取第一個字符

wget -q 靜默不輸出下載信息

wget xxx -P path 下載到指定目錄

wget xxx -O file 保存具體文件名稱

awk -F '_' '{print $1"_"$2}'` awk 拼接直接在中間雙引號即可

currentDate=`date "+%Y_%m_%d"`

 

    nohup java -jar xxx & 如果需要殺死這個程序 需要 ps -aux | grep xxx 或者 ps -ef | grep xxx 得到第二列的pid 將其殺死 kill -9 $pid 
    kill -9 ` ps -aux | grep xxx | awk '{print $2}'`
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章