本文地址html
點擊關注微信公衆號 wenyuqinghuailinux
分享提綱:正則表達式
1. shell中的函數shell
2. shell中的數組vim
4. shell中的運算符bash
5. Linux的一些命令微信
6. 幾個自定義的腳本(能夠添加到命令中)ide
1.1)【定義shell函數(define function)】函數
[ function ] funname [()] { action; [return int;] }
說明:
實例(testfun1.sh):
1 #!/bin/sh 2 3 fSum 3 2; 4 function fSum() 5 { 6 echo $1,$2; 7 return $(($1+$2)); 8 } 9 fSum 5 7; 10 total=$(fSum 3 2); 11 echo $total,$?; 12 13 sh testfun1.sh 14 testfun1.sh: line 3: fSum: command not found 15 5,7 16 3,2 17 1 18 5
從上面這個例子咱們能夠獲得幾點結論:
1.2)【函數做用域,變量做用範圍】
先咱們看一個實例(testfun2.sh ):
1 #!/bin/sh 2 3 echo $(uname); 4 declare num=1000; 5 6 uname() 7 { 8 echo "test!"; 9 ((num++)); 10 return 100; 11 } 12 testvar() 13 { 14 local num=10; 15 ((num++)); 16 echo $num; 17 18 } 19 20 uname; 21 echo $? 22 echo $num; 23 testvar; 24 echo $num; 25 26 sh testfun2.sh 27 Linux 28 test! 29 100 30 1001 31 11 32 1001
咱們一塊兒來分析下上面這個實例,能夠獲得以下結論:
2.1)【數組的定義】
兩種方法
arr=(1 2 3 4 5) # 注意是用空格分開,不是逗號!!
和
array array[0]="a" array[1]="b" array[2]="c"
2.2)【數組的遍歷】
for var in ${ arr[@] }; do echo $var done
2.3)【數組其餘】
-- 【讀取數組】
valuen=${array_name[n]} #獲取單個元素 echo ${array_name[@]} #獲取全部元素 # 取得數組元素的個數 length=${#array_name[@]} # 或者 length=${#array_name[*]} # 取得數組單個元素的長度 lengthn=${#array_name[n]}
3.1)定義
your_name="runoob.com"
-- 不能有空格
--能夠使用表達式賦值
for file in `ls /etc`
3.2)【使用變量】
your_name="qinjx" echo $your_name echo ${your_name}
-- 能夠加 {}, 也能夠不加
--【只讀變量】 readonly myUrl
--【刪除變量】 unset variable_name
3.3)【Shell 字符串】
-- 能夠單引號或者雙引號,雙引號會解析變量
--獲取字符串長度
string="abcd" echo ${#string} #輸出 4
-- 截取字符串
string="runoob is a great site" echo ${string:1:4} # 輸出 unoo
-- 查找字符串
string="runoob is a great company" echo `expr index "$string" is` # 輸出 8
4.1)【基本運算符】
原生bash不支持簡單的數學運算,可是能夠經過其餘命令來實現,例如 awk 和 expr,expr 最經常使用。
#!/bin/bash val=`expr 2 + 2` echo "兩數之和爲 : $val"
4.2)【關係運算符】
lt : litter than
gt: grater than
運算符 | 說明 | 舉例 |
---|---|---|
-eq | 檢測兩個數是否相等,相等返回 true。 | [ $a -eq $b ] 返回 false。 |
-ne | 檢測兩個數是否相等,不相等返回 true。 | [ $a -ne $b ] 返回 true。 |
-gt | 檢測左邊的數是否大於右邊的,若是是,則返回 true。 | [ $a -gt $b ] 返回 false。 |
-lt | 檢測左邊的數是否小於右邊的,若是是,則返回 true。 | [ $a -lt $b ] 返回 true。 |
-ge | 檢測左邊的數是否大於等於右邊的,若是是,則返回 true。 | [ $a -ge $b ] 返回 false。 |
-le | 檢測左邊的數是否小於等於右邊的,若是是,則返回 true。 | [ $a -le $b ] 返回 true。 |
4.3)【字符串運算符】
-z | 檢測字符串長度是否爲0,爲0返回 true。 | [ -z $a ] 返回 false。 |
-n | 檢測字符串長度是否爲0,不爲0返回 true。 | [ -n $a ] 返回 true。 |
str | 檢測字符串是否爲空,不爲空返回 true。 | [ $a ] 返回 true。 |
4.4)【文件測試運算符】
文件測試運算符用於檢測 Unix 文件的各類屬性。
屬性檢測描述以下:
操做符 | 說明 | 舉例 |
---|---|---|
-b file | 檢測文件是不是塊設備文件,若是是,則返回 true。 | [ -b $file ] 返回 false。 |
-c file | 檢測文件是不是字符設備文件,若是是,則返回 true。 | [ -c $file ] 返回 false。 |
-d file | 檢測文件是不是目錄,若是是,則返回 true。 | [ -d $file ] 返回 false。 |
-f file | 檢測文件是不是普通文件(既不是目錄,也不是設備文件),若是是,則返回 true。 | [ -f $file ] 返回 true。 |
-g file | 檢測文件是否設置了 SGID 位,若是是,則返回 true。 | [ -g $file ] 返回 false。 |
-k file | 檢測文件是否設置了粘着位(Sticky Bit),若是是,則返回 true。 | [ -k $file ] 返回 false。 |
-p file | 檢測文件是不是有名管道,若是是,則返回 true。 | [ -p $file ] 返回 false。 |
-u file | 檢測文件是否設置了 SUID 位,若是是,則返回 true。 | [ -u $file ] 返回 false。 |
-r file | 檢測文件是否可讀,若是是,則返回 true。 | [ -r $file ] 返回 true。 |
-w file | 檢測文件是否可寫,若是是,則返回 true。 | [ -w $file ] 返回 true。 |
-x file | 檢測文件是否可執行,若是是,則返回 true。 | [ -x $file ] 返回 true。 |
-s file | 檢測文件是否爲空(文件大小是否大於0),不爲空返回 true。 | [ -s $file ] 返回 true。 |
-e file | 檢測文件(包括目錄)是否存在,若是是,則返回 true。 | [ -e $file ] 返回 true。 |
5.1)【一些不太常見的命令】
-- 查看當前發行版本能夠使用的shell:cat /etc/shells
5.2)【正則表達式】
5.3)【awk命令】
http://blog.csdn.net/shanyongxu/article/details/46563997
5.4)【xargs命令】
http://blog.csdn.net/shanyongxu/article/details/46859829
1)【實現scp下載遠程文件到本地】
-- down.sh 放到 ~/my_cmd
#!/bin/bash if [ -z $1 ] then echo "請輸入要下載的遠程文件" else scp -P5860 root@10.x.xx.xx:$1 ~/Downloads echo "文件下載到了 ~/Downloads" fi
而後在 vim ~/.bashrc 加上
alias down='sh ~/my_cmd/down.sh'
就能夠使用 down yourRemoteFile 命令下載遠程文件到本地了