Linux Shell 指令碼應用 字串處理

2021-06-21 22:24:44 字數 720 閱讀 3788

1、子串擷取操作

路徑分割:

dirname 命令、basename命令

使用expr 命令:

格式:expr  substr  $var1  起始位置  擷取長度

使用${}表示式:

格式:$

2、字串替換

$//將var1字串中的

第乙個old

替換成new

$// 將var1字串中所有的old替換成new

3、使用隨機字串

/dev/urandom:生成隨機字元(包括可能是亂碼,不適合做口令)——>/usr/bin/md5sum(md5轉換)——>/bin/cut(字串切割)

head  -1 /dev / urandom | md5sum    (隨機字元到ascii字元)

echo  $var1 |  cut   -b  起始位置--結束位置         :使用cut 切割字串

head  -1  / dev /urandom | md5sum |cut -b -8;

18a133cd1

head  -1  / dev /urandom | md5sum |cut -b -8;

df334d90d

兩次執行不同,就可以將結果賦給賬號……

Linux Shell指令碼應用 使用變數

1 變數基本操作 定義及賦值 格式 變數名 變數值例如 title beidaqingniao 引用 變數名 例如 echo title 2 雙引號 單引號 反撇號 雙引號 允許變數引用 轉義 單引號 禁止引用 單引號之中的 符號,不會去反向找變數的值,字面列印 轉義 就是它本身 反撇號 數字1左側...

Shell指令碼應用

shell script shell指令碼與windows dos下的批處理相似,也就是用各類命令預先放入到乙個檔案中,方便一次性執行的乙個程式檔案,主要是方便管理員進行設定或者管理用的。但是它比windows下的批處理更強大,比用其他程式設計程式編輯的程式效率更高,它使用了linux unix下的...

shell指令碼基本應用

chmod x hello.sh 給檔案增加執行許可權 hello.sh 執行指令碼 sh hello.sh 以sh執行指令碼 bash hello.sh 以bash執行指令碼 source hello.sh 以source執行指令碼,該指令碼檔案可以無 執行許可權 if有三種表示式 1.if fi...