linux實用操作積累

shell腳本

由多個linux指令集成在一個命令文件中

  1. 腳本執行命令:./shellname.sh
  2. 文件理論上可以使用任何後綴名,但shell腳本規範爲.sh後綴
  3. 可以使用任何編輯器編輯
  4. 腳本以#!/bin/bash開頭,表示使用/bin目錄下的bash程序來解釋執行腳本
  5. 編寫好後需要使用chmod +x filename給相應腳本文件執行權限,否則無法使用常規命令執行腳本

示例

#!/bin/bash

while [ $x -ne 1000 ]
	do
		echo "這是第$x秒,1000秒時結束計數!";
		x=$(($x+1)) #這樣纔不會被理解爲字符串
		sleep 1s  #延遲1s
	done

查看文件夾大小

du -d 1 -h :查看深度爲1的文件夾大小


Bash字符串處理

#基於Pattern Matching的子串替換
${STR/$OLD/$NEW}
#替換第一個。
${STR//$OLD/$NEW}
#替換所有。
${STR/#$OLD/$NEW}
#替換開頭。如果STR以OLD串開頭,則替換。
${STR/%$OLD/$NEW}
#替換結尾。如果STR以OLD串結尾,則替換。

[user@laptop ~]# STR=”Hello World”
[user@laptop ~]# echo ${STR/o/O}
HellO World
[user@laptop ~]# echo ${STR//o/O}
HellO WOrld
[user@laptop ~]# STR=”Hello World”
[user@laptop ~]# echo ${STR/#He/he}
hello World
[user@laptop ~]# echo ${STR/#o/he}
Hello World
[user@laptop ~]# echo ${STR/%He/he}
Hello World
[user@laptop ~]# echo ${STR/%ld/lD}
Hello WorlD

#注意:不能使用正則表達式,只能使用?*的Shell擴展。只能用shell通配符如 * ?  [list] [!list] [a-z]。
#如果被替換串包含/字符,那麼要轉義,寫成\/。

統計命令 wc

Usage: wc [選項] [輸入流文件]
Count lines, words, and bytes for each FILE (or stdin)

	-c	Count bytes
	-l	Count newlines
	-w	Count words
	-L	Print longest line length

管道命令

命令1 | 命令2 :把命令1的輸出作爲命令2的輸入。

  1. 例如opkg list *zh*| grep luci | grep base,這個命令使用了通配符*和兩個管道命令,從而顯示出opkg安裝列表中同時包含zh、luci、base
    三個子串的軟件包。
  2. ls -al | wc -l 統計當前文件夾下文件個數並輸出

文件查找

find命令

  • find [路徑] [表達式選項] [動作表達式]
  • 例如:find /home 3.jpg (動作表達默認爲print)

使用shell腳本操作數據庫

腳本代碼:顯示數據庫、數據表、屬性

#! /bin/bash

mysql -uroot -p123 -e "
show databases;
use MQTT;
show tables;
show columns from sht31;
"

效果

geek@geek-PC:~/hexo$ ../code/sqlopt.sh
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| MQTT               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
+----------------+
| Tables_in_MQTT |
+----------------+
| sht31          |
+----------------+
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| sht_time | datetime    | YES  |     | NULL    |       |
| sht_temp | smallint(6) | YES  |     | NULL    |       |
| sht_hum  | smallint(6) | YES  |     | NULL    |       |
| id       | int(11)     | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
ERROR 1064 (42000) at line 6: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'quit' at line 1


批量修改文件後綴名

運行命令:./scriptName [原後綴] [新後綴]

#!/bin/bash

for filename in `ls ./ |grep $1 ` #篩選出需要修改的文件
    do
        echo "原文件名:$filename"
        newname=${filename/%$1/$2}
        echo "新文件名:$newname"
        mv $filename $newname
    done

批量修改圖片分辨率

由於體育課要提交大量照片,源圖片太大需要壓縮一下。參考上面批量修改文件名的腳本可以輕鬆得到此效果

#!/bin/bash

for filename in `ls ./ |grep $1 ` #篩選出需要修改的文件
    do
        echo "文件名:$filename"
        convert $filename -resize 1080x440 $filename
    done

scp發送文件到服務器

scp -P 1022 '/home/hch/Downloads/libmosquitto-nossl_1.4.14-1_mips_24kc.ipk' [email protected]:/tmp
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章