linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

效果圖

linux中的shell腳本編程基本知識

2.diff比較和patch打補丁

diff 命令是用來比較兩個文件或目錄的不同

vim westos 寫入:hello world westosvim westos1寫入:hello world xbwdiff westos westos1 ###比較這兩個文件 
linux中的shell腳本編程基本知識

diff -u westos westos1 ###-u,以合併的方式來顯示文件內容的不同diff -u westos westos1 > westos.path ###把比較結果重定向到 westos.pathls cat westos.path ###查看此文件
linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

PATCH:用於文件不同文件打布丁

yum install patch -ypatch westos westos.path ####不會保留原文件lscat westoscat westos1
linux中的shell腳本編程基本知識

patch -b westos westos.path ###-b,加上後會保留原文件內容到westos.origcat westoscat westos1cat westos.orig
linux中的shell腳本編程基本知識

2.比較目錄mkdir linuxmkdir unixlstouch linux/filediff -r linux unix
linux中的shell腳本編程基本知識

3.cut 命令多用與字符截取

cut -d指定分隔符cut -f 1,7|1-7 指定截取的列(1,7表示1列和7列、1-7表示1到7列)cut -c 1,4|1-4 指定截取的字符位置cut -d : -f 1 passwd
linux中的shell腳本編程基本知識

cut -d : -f 1-3 passwd
linux中的shell腳本編程基本知識

cut -d : -f 1,3 passwd
linux中的shell腳本編程基本知識

cut -d : -f 3- passwd
linux中的shell腳本編程基本知識

cut -c 1,3 passwd
linux中的shell腳本編程基本知識

1.寫一個腳本,執行後顯示本機ip,腳本名為ip_show.shvim ip_show.shsh ip_show.sh ###查看結果

以下兩種寫法都可以實現

linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

2.寫一個腳本,ping主機,主機開著就顯示is up,沒有或關著就顯示is downvim check_ip.shsh check_ip.sh 172.25.254.111 ###驗證結果
linux中的shell腳本編程基本知識

4.排序sort

vim xbwsort -n xbw ##純數字排序sort -rn xbw ##倒序sort -u xbw ##去掉重複數字sort -o ###輸出到指定文件中sort -t ###指定分隔符sort -k ###指定要排序的列sort -t : -k 2 -rn xbw ###將xbw中冒號分割符第2列排倒序
linux中的shell腳本編程基本知識

sort -t : -k 1 -n xbw ###將xbw中冒號分割符第1列排序
linux中的shell腳本編程基本知識

sort -t : -k 1 -n xbw -o sort ###將排序結果放入sort文件中cat sort
linux中的shell腳本編程基本知識

sort -n xbw | uniq -c ###每行顯示一次並統計重複次數
linux中的shell腳本編程基本知識

sort -n xbw | uniq -d ###顯示重複的行
linux中的shell腳本編程基本知識

sort -n xbw | uniq -u ###顯示唯一的行
linux中的shell腳本編程基本知識

1.寫一個腳本,找出/mnt裡最大的文件vim find_big.shsh find_big.shtest 命令

下圖中這兩種寫法都可以實現

linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

test 命令

test 命令和 [] 等同test "$A" == "$B" 等同 [ "$A" == "$B" ][ "$A" = "$B" ] ###等於[ "$A" != "$B" ] ###不等於[ "$A" -eq "$B" ] ###等於[ "$A" -ne "$B" ] ###不等於[ "$A" -le "$B" ] ###小於等於[ "$A" -lt "$B" ] ###小於["$A" -ge "$B" ] ###大於等於["$A" -gt "$B" ] ###大於["$A" -ne "$B" -a "$A" -gt "$B" ] ###既滿足不等於也滿足大於(與的關係)["$A" -ne "$B" -o "$A" -gt "$B" ] ###滿足等於或大於(或的關係)[-z "$A" ] ###檢測是否為空[-n "$A" ] ###檢測是否不為空&& ###表示結果為真|| ###表示結果為假
linux中的shell腳本編程基本知識

1.寫一個腳本,判斷主機是否開啟,若沒有寫主機ip,則提示加上主機ipvim check_ip.shsh check_ip.sh ###給提示加上ipsh check_ip.sh 172.25.254.111 ###顯示upsh check_ip.sh 172.25.254.333 ###顯示down
linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

2.寫一個腳本,判斷一個數在0到10之間,若沒寫數字,提示給個數字vim num_check.shsh num_check.sh ##提示給數字sh num_check.sh 8 ###顯示結果正確sh num_check.sh 20 ###顯示結果錯誤
linux中的shell腳本編程基本知識

["file1" -ef "file2" ] ###判斷兩個文件是否節點一致,是否為硬鏈接touch fileln /mnt/file /mnt/file1 ###作鏈接ls -li file*[ "/mnt/file" -ef "/mnt/file1" ] && echo yes || echo no[ "/mnt/file" -ef "/etc/passwd" ] && echo yes || echo no
linux中的shell腳本編程基本知識

["file1" -nt "file2" ] ###判斷file1是否比file2新建立["file1" -ot "file2" ] ###判斷file1是否比file2遲建立[ "/mnt/file" -ot "/mnt/file2" ] && echo yes || echo no
linux中的shell腳本編程基本知識

[-e "file" ] ###測試文件是否存在[-f "file" ] ###測試文件是否為普通文件[-L "file" ] ###測試文件是否為軟鏈接 [-S "file" ] ###測試文件是否為套接字[-b "file" ] ###測試文件是否為塊設備[-d "file" ] ###測試文件是否為目錄[-c "file" ] ###測試文件是否為字符設備1.vim file.sh ####為做實驗先寫一個腳本進行驗證寫入:#!/bin/bash[ "$1" "/mnt/file" ] && echo yes || echo notouch filesh file.sh -esh file.sh -f
linux中的shell腳本編程基本知識

ln -s /mnt/file llsh file.sh -L
linux中的shell腳本編程基本知識

rm -fr fileyum install mariadb-server.x86_64 -ylssystemctl start mariadbrsync -D /var/lib/mysql/mysql.sock /mnt/filellsh file.sh -S
linux中的shell腳本編程基本知識

rm -fr filersync -D /dev/vdb /mnt/filellsh file.sh -b
linux中的shell腳本編程基本知識

rsync -D /dev/pts/1 /mnt/filellsh file.sh -c
linux中的shell腳本編程基本知識

rm -fr filemkdir filesh file.sh -d
linux中的shell腳本編程基本知識

1.寫一個腳本,查看文件是否存在,存在的話屬於什麼文件類型vim file_check.shsh file_check.sh 文件名 ####驗證
linux中的shell腳本編程基本知識

驗證普通文件和塊設備

linux中的shell腳本編程基本知識

驗證套接字和目錄

linux中的shell腳本編程基本知識

驗證軟鏈接

linux中的shell腳本編程基本知識

tr大小寫轉換

vim hello.sh寫入:#!/bin/bash[ "$1" = "hello" ] &&{ echo yes}||{ echo no}sh test.sh hello ####小寫可以識別sh test.sh HELLO ###大寫就不會識別,會報錯
linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

tr 'a-z' 'A-Z' < hello.sh tr 'A-Z' 'a-z' < hello.sh
linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

寫入:#!/bin/bashWORD=$(echo $1 | tr 'A-Z' 'a-z')[ "$WORD" = "hello" ] &&{ echo yes}||{ echo no}此腳本就會自動將大寫轉換為可識別的小寫
linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

1.寫一個腳本,判斷一個用戶是否存在,存在就不管,若沒有就建立並設置密碼vim user_create.shsh user_create.sh user 123sh user_create.sh xbw 234下圖兩種寫法都可以實現腳本功能
linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識

linux中的shell腳本編程基本知識


分享到:


相關文章: