前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
expr命令是一個手工命令行計數器,用于在UNIX/LINUX下求表達式變量的值,一般用于整數值,也可用于字符串。
語法
expr 表達式
表達式
表達式說明:
- 用空格隔開每個項;
- 用 / (反斜杠) 放在 shell 特定的字符前面;
- 對包含空格和其他特殊字符的字符串要用引號括起來
實例
1、計算字串長度
> expr length “this is a test”14
expr length “this is a test”14
2、抓取字串
> expr substr “this is a test” 3 5
is is
expr substr “this is a test” 3 5
is is
3、抓取第一個字符數字串出現的位置
> expr index "sarasara" a2
expr index "sarasara" a2
4、整數運算
> expr 14 % 95> expr 10 + 1020> expr 1000 + 9001900> expr 30 / 3 / 25> expr 30 \* 3 (使用乘號時,必須用反斜線屏蔽其特定含義。因為shell可能會誤解顯示星號的意義)90> expr 30 * 3expr: Syntax error
> expr 14 % 95> expr 10 + 1020> expr 1000 + 9001900> expr 30 / 3 / 25> expr 30 \* 3 (使用乘號時,必須用反斜線屏蔽其特定含義。因為shell可能會誤解顯示星號的意義)90> expr 30 * 3expr: Syntax error
---------------------------------------------? 我 是 分 隔 線 -------------------------------------------------------
?
?
Linux wc命令用于計算字數。
利用wc指令我們可以計算文件的Byte數、字數、或是列數,若不指定文件名稱、或是所給予的文件名為"-",則wc指令會從標準輸入設備讀取數據。
語法
wc [-clw][--help][--version][文件...]
[-clw][--help][--version][文件...]
參數:
- -c或--bytes或--chars 只顯示Bytes數。
- -l或--lines 只顯示行數。
- -w或--words 只顯示字數。
- --help 在線幫助。
- --version 顯示版本信息。
實例
在默認的情況下,wc將計算指定文件的行數、字數,以及字節數。使用的命令為:
<span style="color:#000000">wc testfile </span>
先查看testfile文件的內容,可以看到:
$ cat testfile
Linux networks are becoming more and more common, but scurity is often an overlooked
issue. Unfortunately, in today’s environment all networks are potential hacker targets,
fro0m tp-secret military research networks to small home LANs.
Linux Network Securty focuses on securing Linux in a networked environment, where the
security of the entire network needs to be considered rather than just isolated machines.
It uses a mix of theory and practicl techniques to teach administrators how to install and
use security applications, as well as how the applcations work and why they are necesary.
Linux networks are becoming more and more common, but scurity is often an overlooked
issue. Unfortunately, in today’s environment all networks are potential hacker targets,
fro0m tp-secret military research networks to small home LANs.
Linux Network Securty focuses on securing Linux in a networked environment, where the
security of the entire network needs to be considered rather than just isolated machines.
It uses a mix of theory and practicl techniques to teach administrators how to install and
use security applications, as well as how the applcations work and why they are necesary.
使用 wc統計,結果如下:
$ wc testfile # testfile文件的統計信息
3 92 598 testfile # testfile文件的行數為3、單詞數92、字節數598
# testfile文件的統計信息
3 92 598 testfile # testfile文件的行數為3、單詞數92、字節數598
其中,3 個數字分別表示testfile文件的行數、單詞數,以及該文件的字節數。
如果想同時統計多個文件的信息,例如同時統計testfile、testfile_1、testfile_2,可使用如下命令:
wc testfile testfile_1 testfile_2 #統計三個文件的信息
#統計三個文件的信息
輸出結果如下:
$ wc testfile testfile_1 testfile_2 #統計三個文件的信息
3 92 598 testfile #第一個文件行數為3、單詞數92、字節數598
9 18 78 testfile_1 #第二個文件的行數為9、單詞數18、字節數78
3 6 32 testfile_2 #第三個文件的行數為3、單詞數6、字節數32
15 116 708 總用量 #三個文件總共的行數為15、單詞數116、字節數708
#統計三個文件的信息
3 92 598 testfile #第一個文件行數為3、單詞數92、字節數598
9 18 78 testfile_1 #第二個文件的行數為9、單詞數18、字節數78
3 6 32 testfile_2 #第三個文件的行數為3、單詞數6、字節數32
15 116 708 總用量 #三個文件總共的行數為15、單詞數116、字節數708
---------------------------------------? 我 是 分 隔 線 --------------------------------------------------
?
let 命令是 BASH 中用于計算的工具,用于執行一個或多個表達式,變量計算中不需要加上 $ 來表示變量。如果表達式中包含了空格或其他特殊字符,則必須引起來。
語法格式
let arg [arg ...]
arg [arg ...]
參數說明:
arg:要執行的表達式
實例:
自加操作:let no++
自減操作:let no--
簡寫形式?let no+=10,let no-=20,分別等同于?let no=no+10,let no=no-20。
以下實例計算 a 和 b 兩個表達式,并輸出結果:
#!/bin/bashlet a=5+4
let b=9-3
echo $a $b
let a=5+4
let b=9-3
echo $a $b
以上實例執行結果為:
9 6
6
又如:
?
i=0
while((i<=5))
do echo $i let i++
done
=0
while((i<=5))
do echo $i let i++
done
?