日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux shell 脚本 supress,《linux Shell 脚本攻略》进阶学习(第一部分)

發布時間:2023/12/2 linux 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux shell 脚本 supress,《linux Shell 脚本攻略》进阶学习(第一部分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第二章命令之樂

cat 不僅可以讀取文件并且連接數據,它還能從標準輸入中進行讀取

要從標準輸入中讀取,就要使用管道操作符

echo 'Text through stdin' | cat - file.txt。這里的-被作為來之stdin 文本的文件名稱

實例 在file.txt 中寫入dfagfmirgjriogjrogijdfgio后

$ echo 'Text through stdin' | cat - file.txt

Text through stdin

dfagfmirgjriogjrogijdfgio

$ echo 'Text through stdin' | cat < file.txt

dfagfmirgjriogjrogijdfgio

$ echo 'Text through stdin' | cat ?file.txt

dfagfmirgjriogjrogijdfgio

cat補充內容

-s, --squeeze-blank #壓縮連續的空白行

suppress repeated empty output lines

tr

-s, --squeeze-repeats#將多個連續的字符壓縮成單個字符

replace each input sequence of a repeated ?character ?that ?is

listed in SET1 with a single occurrence of that character

如:file.txt 內容

line

line

line

line

line

line

line

line

$ cat file.txt | tr -s '\n'

line

line

line

line

line

line

line

line

cat -n stream

-n, --number #給內容之前加上行號

number all output lines

錄制與回放終端回話,很實用

開始錄制回話

$ script -t 2>time.log -a output.session

Script started, file is output.session

type commend

.....

exit

兩個配置文件被同時當做script命令的參數。其中一個文件(timing.log)用于存儲時序信息,描述每一個命令在何時運行,另一個文件(output.session)用于存儲命令輸出,-t選項用于將時序數據導入stderr。2>則用于將stderr重定向到timeing.log。

$ scriptreplay time.log output.session

find文件查找

-print 文件跟文件夾會以\n分隔

find sourcedOf -iname "example" -print(忽略字母大小寫)

匹配多個

find \( -name "*".txt -o -name ?"*.pdf" \) -print

否定參數!

find . ! -name "*.txt" -print 不以.txt 結尾的

-exec 結合多個命令

-exec ./command.sh {} \;

-exec printf "text file: %s\n" {} \;

$ find . -exec printf "text file is: %s\n" {} \;

text file is: .

text file is: ./file.txt

text file is: ./time.log

text file is: ./output.session

玩轉xargs

xargs 也可以將單行或多行文本輸入轉換成其他格式,例如單行變多行或是多行變單行

bash***都喜歡單行命令

command | xargs

xargs 是一種替換方式,類似與find命令的-exec參數

實戰演練

1.將多行輸入裝換成單行輸出

cat file.txt | xargs

line line line line line line line line

2.將單行輸入裝換成多行輸出

指定每行最大值 -n max,每行max個參數,每個參數都是由" " 空格隔開的字符串。空格是默認的定界符

cat file.txt | xargs -n 3

line line line

line line line

line line

更多xargs使用baidu

使用tr進行轉換

tr 可以進行替換,刪除,壓縮

tr 只能通過stdin,而無法通過命令行參數來接受輸入。他的調用格式如下

tr [options] set1 set2

echo "Hello who IS ThIs" | tr 'A-M,n-z' 'a-m,N-Z'

hellO WhO iS ThiS

使用tr刪除字符

tr -d 'set1' #只使用set1 ,而不使用set2

echo "hello 0980world" | tr -d '0-9'

hello world

字符集的補集

tr -c [set1] [set2]

-c, -C, --complement 補數

use the complement of SET1

zhangjianlin@zhangjianlin:~/mytest/linuxtest/linuxshellscrill/second$ echo -e ?"hello 0980world" | tr -d -c '0-9 \n'

0980

zhangjianlin@zhangjianlin:~/mytest/linuxtest/linuxshellscrill/second$ echo -e ?"hello 0980world" | tr -d -c '0-9'

算數運算

echo {1..50} | echo $[ $( tr ' ' '+' ) 0 ]

1725

$[ operation ] 執行算術運算

相當于$[ 1+...+50+0 ]

文件名的處理

$name ${name%$1}$2 ?#${name%\.*} "%"號除去后綴名,只取文件名

${file_name#*.} 只留擴展名或后綴

校驗和核實

校驗對與編寫備份腳本或系統維護腳本來說都非常重要

最知名且最為廣泛的校驗和技術是md5sum和shalsum

實戰演練

計算md5sum,使用下面命令

md5sum filename

$ md5sum file.txt

221950d53cb7cc10da1c3e7e7ec4e0d5 ?file.txt

$ md5sum file.txt > file.md5

$ md5sum -c file.md5

file.txt: 確定

sha1sum 跟 md5sum類似

對目錄進行校驗

校驗和是從文件中計算得來的。對目錄計算校驗和意味著我們需要對目錄中的所有文件以遞歸的方式進行計算

它可以使用命令md5deep或sha1deep來實現

md5deep -rl directory_path >directory.md5

#-r 使用遞歸的方式

#-l 使用相對路徑。默認md5會輸出文件的絕對路徑

用下面命令進行核實

$ md5sum -c directory.md5

批量重命名和移動批量重命名文件名稱(包括后綴名)#a!/bin/bash

functionlistfiles(){

forfilein*; # * is all of pwd'files

do

echo$file

done

}

functioninputRule(){

read-p "please input the fromsuffix as *.txt:"fromsuffix;

read-p "please input the tosuffix as .txt:"tosuffix;

read-p "please input the part of file name:"headname;

}

functionchangename(){

count=1;

forfromname in$fromsuffix;

do

new="${headname}${count}${tosuffix}"

mv"$fromname""$new"2> /dev/null;

if[ $? -eq0 ];

then

echo"Renameing $fromname to $new";

letcount++

fi

done

}

inputRule;

changename;

#listfiles;

批量更改名稱后綴,不包含名稱#!/bin/bash

functioninputDialog(){

read-p "input fromsuffix:"fromsuffix

read-p "input tosuffix:"tosuffix

}

functionchangeSuffix(){

forfromname in*${fromsuffix};

do

#echo $fromname \n

#echo ${fromname%$fromsuffix}$tosuffix

mv"$fromname""${fromname%$fromsuffix}$tosuffix"#不加""mv: 目標"*.png" 不是目錄

if[ $? -eq0 ];

then

echo"$fromname"rename to "${fromname%$fromsuffix}$tosuffix"

fi

done

}

inputDialog;

changeSuffix;

echo -e "\e[1;42m Green Background \e[0m" ?彩色背景打印

echo $var 或 echo ${var}var=value 一個賦值

var = value 一個相等操作

賦值時雙引號的使用(value是否包含空格)

若無空格,特殊字符,加不加無所謂

如:var=value 或 var="value"

但var="value cat" 必須加""

length=${#var}獲取變量值的長度

如:

zhangjianlin@zhangjianlin:~$ echo ${#PATH}

288

算數運算

zhangjianlin@zhangjianlin:~/mytest/linuxtest/linuxshell$echo "4 * 0.56" | bc

2.24

數組

array_var=(1212 1233 424 565)

#!/bin/bash

array_var=(1212 3434 546 7687)

echo ${array_var[*]} {}命令塊,能執行命令,處理特殊字符

echo ${#array_var[*]} #4#!/bin/bash

array_var=(1212 3434 546 7687)

echo ${array_var[*]}

echo ${#array_var[*]}

alias rmback='cp $@ ~/backup; rm $@' 刪除時備份

終端工具

tput和stty是兩款終端處理工具

tput cols,lines,longname,cpu 100 100,

輸入密碼時,不能讓輸入的內容顯示出來。用stty#!/bin/bash

#Filename:password.sh

function enterpassword(){

echo -e "Enter password"

stty -echo

read password

stty echo

echo password read

}

enterpassword

在腳本中生成延時#!/bin/bash

#Filename: sleep.sh

function delaytime(){

echo -n Count:

tput sc #store cursor

count=0;

while true;

do

if [ $count -lt 40 ];

then let count++;

sleep 1;

tput rc #recover cursor

tput ed #clear the content from the head to tail of cursor

echo -n $count;

else exit 0;

fi

done

}

delaytime;

遞歸函數

bash同樣支持遞歸函數

F(){echo $1;F hello;sleep 1;}

Fork×××":(){ :|:& };:"這個遞歸能調用自身,不斷產生新的進程 :勿用

利用子shell 生成一個獨立進程

#!/bin/bash

pwd

(cd /bin; ls);

pwd

通過引用子shell的方式保留空格和換行符

假設我們使用子shell或反引號的方法將命令的輸出讀入一個變量中,可以將它放入雙引號中,以保留空格和換行符(\n)

out="$(cat text.txt)" 保留空格跟換行符號

out=$(cat text.txt)

用不回顯的方式讀取密碼read

read -s var

在特定時限內讀取輸入

read -t 2 var

read -t 2 -s var

使用定界符結束輸入,不使用Enter結束輸入

read -d ":" var ?達到能輸入換行的母的

zhangjianlin@zhangjianlin:~/mytest/linuxtest/linuxshellscrill/first$ read -d ":" var

dfdufdif

fdfidjf

gfgig

:

考慮CSV的數據情況#!/bin/bash

function changeIFS(){

oldIFS=$IFS #IFS的默認值是空白字符(換行符,制表符或者空格)

IFS=,

for item in $data;

do

echo item: $item

done

IFS=$oldIFS

}

data="name,sex,rollno,location" #使用IFS讀取變量的每一個條目

changeIFS;

#IFS默認空格,這里把他變成","

輸出結果

item: name

item: sex

item: rollno

item: location

實戰演練#!/bash/bin

#user of shell

function usesh(){

oldIFS=$IFS;

IFS=":";

count=0;

for item in $line;

do

[ $count -eq 0 ] && user=$item;

[ $count -eq 6 ] && shell=$item;

let count++;

done

IFS=$oldIFS

}

line="root:x:0:0:root:root:/bin/bash"

usesh;

echo $user\'s shell is $shell

for循環

for var in list

do

conmend

done

echo {1..30};echo {a..z};echo {A..Z};echo {a..h};

for i in {a..z};

do

commend

done

for((i=0;i<10;i++))

{

commend

}

比較與測試

邏輯運算將它變得簡潔

[ condition ] && action; 如果是真執行action

[ condition ] || action; 如果是假真執行action

算術比較(數字比較)

[ $var -eq 0 ] or [ $var -ne 0 ]

其他的操作符

-gt 大于

-lt 小于

-ge 大于或等于

le 小于或等于

-eq 等于

文件相關的測試

[ -f $file_var ] 是否是文件

-x 可執行

-d 目錄

-e存在

-c 字符設備路徑

-b 塊設備

-w 文件可寫

-r 文件是否可讀

-L 符號連接

判斷輸入的是否為文件或文件夾

#!/bin/bash

functionfileordir(){

if[ -e $fpath ];then# exist

if[ -d $fpath ] ;then#directory

echo$fpath is a dir;

exit

fi

if[ -f $fpath ] ;then

echo$fpath is a file;

exit

fi

elseecho-e "$fpath not exit";exit;

fi

echo"unknow file type"

}

fpath=$1

fileordir;

zhangjianlin@zhangjianlin:~/mytest/linuxtest/linuxshellscrill/first$ bash filetest.sh .

字符串比較

使用字符串比較時,最好用雙括號,因為有時候采用單引號會產生錯誤,所以最好避開他們。

[[ $str1 == $str2 ]]

[[ $str1 != $str2 ]]

[[ $str1 > $str2 ]]

[[ $str1 < $str2 ]]

[[ $str1 -z $str2 ]] 空字竄

[[ $str1 -n $str2 ]] 非空字竄

if [[ $str1 < $str2 ]] && [[ $str1 == $str2 ]]

test命令執行條件檢測

test有助于避免使用過多的括號

之前的[]測試條件同樣可以用于test命令

if [ $var -eq 0 ];then echo "True";fi

if test $var -eq 0 ;then echo "True";fi

第二章命令之樂

cat 不僅可以讀取文件并且連接數據,它還能從標準輸入中進行讀取

要從標準輸入中讀取,就要使用管道操作符

echo 'Text through stdin' | cat - file.txt。這里的-被作為來之stdin 文本的文件名稱

實例 在file.txt 中寫入dfagfmirgjriogjrogijdfgio后

$ echo 'Text through stdin' | cat - file.txt

Text through stdin

dfagfmirgjriogjrogijdfgio

$ echo 'Text through stdin' | cat < file.txt

dfagfmirgjriogjrogijdfgio

$ echo 'Text through stdin' | cat ?file.txt

dfagfmirgjriogjrogijdfgio

cat補充內容

-s, --squeeze-blank #壓縮連續的空白行

suppress repeated empty output lines

tr

-s, --squeeze-repeats#將多個連續的字符壓縮成單個字符

replace each input sequence of a repeated ?character ?that ?is

listed in SET1 with a single occurrence of that character

如:file.txt 內容

line

line

line

line

line

line

line

line

$ cat file.txt | tr -s '\n'

line

line

line

line

line

line

line

line

cat -n stream

-n, --number #給內容之前加上行號

number all output lines

錄制與回放終端回話,很實用

開始錄制回話

$ script -t 2>time.log -a output.session

Script started, file is output.session

type commend

.....

exit

兩個配置文件被同時當做script命令的參數。其中一個文件(timing.log)用于存儲時序信息,描述每一個命令在何時運行,另一個文件(output.session)用于存儲命令輸出,-t選項用于將時序數據導入stderr。2>則用于將stderr重定向到timeing.log。

$ scriptreplay time.log output.session

find文件查找

-print 文件跟文件夾會以\n分隔

find sourcedOf -iname "example" -print(忽略字母大小寫)

匹配多個

find \( -name "*".txt -o -name ?"*.pdf" \) -print

否定參數!

find . ! -name "*.txt" -print 不以.txt 結尾的

-exec 結合多個命令

-exec ./command.sh {} \;

-exec printf "text file: %s\n" {} \;

$ find . -exec printf "text file is: %s\n" {} \;

text file is: .

text file is: ./file.txt

text file is: ./time.log

text file is: ./output.session

玩轉xargs

xargs 也可以將單行或多行文本輸入轉換成其他格式,例如單行變多行或是多行變單行

bash***都喜歡單行命令

command | xargs

xargs 是一種替換方式,類似與find命令的-exec參數

實戰演練

1.將多行輸入裝換成單行輸出

cat file.txt | xargs

line line line line line line line line

2.將單行輸入裝換成多行輸出

指定每行最大值 -n max,每行max個參數,每個參數都是由" " 空格隔開的字符串。空格是默認的定界符

cat file.txt | xargs -n 3

line line line

line line line

line line

更多xargs使用baidu

使用tr進行轉換

tr 可以進行替換,刪除,壓縮

tr 只能通過stdin,而無法通過命令行參數來接受輸入。他的調用格式如下

tr [options] set1 set2

echo "Hello who IS ThIs" | tr 'A-M,n-z' 'a-m,N-Z'

hellO WhO iS ThiS

使用tr刪除字符

tr -d 'set1' #只使用set1 ,而不使用set2

echo "hello 0980world" | tr -d '0-9'

hello world

字符集的補集

tr -c [set1] [set2]

-c, -C, --complement 補數

use the complement of SET1

zhangjianlin@zhangjianlin:~/mytest/linuxtest/linuxshellscrill/second$ echo -e ?"hello 0980world" | tr -d -c '0-9 \n'

0980

zhangjianlin@zhangjianlin:~/mytest/linuxtest/linuxshellscrill/second$ echo -e ?"hello 0980world" | tr -d -c '0-9'

算數運算

echo {1..50} | echo $[ $( tr ' ' '+' ) 0 ]

1725

$[ operation ] 執行算術運算

相當于$[ 1+...+50+0 ]

文件名的處理

$name ${name%$1}$2 ?#${name%\.*} "%"號除去后綴名,只取文件名

${file_name#*.} 只留擴展名或后綴

校驗和核實

校驗對與編寫備份腳本或系統維護腳本來說都非常重要

最知名且最為廣泛的校驗和技術是md5sum和shalsum

實戰演練

計算md5sum,使用下面命令

md5sum filename

$ md5sum file.txt

221950d53cb7cc10da1c3e7e7ec4e0d5 ?file.txt

$ md5sum file.txt > file.md5

$ md5sum -c file.md5

file.txt: 確定

sha1sum 跟 md5sum類似

對目錄進行校驗

校驗和是從文件中計算得來的。對目錄計算校驗和意味著我們需要對目錄中的所有文件以遞歸的方式進行計算

它可以使用命令md5deep或sha1deep來實現

md5deep -rl directory_path >directory.md5

#-r 使用遞歸的方式

#-l 使用相對路徑。默認md5會輸出文件的絕對路徑

用下面命令進行核實

$ md5sum -c directory.md5

批量重命名和移動批量重命名文件名稱(包括后綴名)#a!/bin/bash

functionlistfiles(){

forfilein*; # * is all of pwd'files

do

echo$file

done

}

functioninputRule(){

read-p "please input the fromsuffix as *.txt:"fromsuffix;

read-p "please input the tosuffix as .txt:"tosuffix;

read-p "please input the part of file name:"headname;

}

functionchangename(){

count=1;

forfromname in$fromsuffix;

do

new="${headname}${count}${tosuffix}"

mv"$fromname""$new"2> /dev/null;

if[ $? -eq0 ];

then

echo"Renameing $fromname to $new";

letcount++

fi

done

}

inputRule;

changename;

#listfiles;

批量更改名稱后綴,不包含名稱#!/bin/bash

functioninputDialog(){

read-p "input fromsuffix:"fromsuffix

read-p "input tosuffix:"tosuffix

}

functionchangeSuffix(){

forfromname in*${fromsuffix};

do

#echo $fromname \n

#echo ${fromname%$fromsuffix}$tosuffix

mv"$fromname""${fromname%$fromsuffix}$tosuffix"#不加""mv: 目標"*.png" 不是目錄

if[ $? -eq0 ];

then

echo"$fromname"rename to "${fromname%$fromsuffix}$tosuffix"

fi

done

}

inputDialog;

changeSuffix;

總結

以上是生活随笔為你收集整理的linux shell 脚本 supress,《linux Shell 脚本攻略》进阶学习(第一部分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。