shell script 編程入門

參考 《linux shell scripting cookbook》linux

控制檯輸出

結構化輸出

#!/bin/bash
#Filename: printf.sh
printf "%-5s %-10s %-4s\n" No Name Mark
printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456
printf "%-5s %-10s %-4.2f\n" 2 James 90.9989
printf "%-5s %-10s %-4.2f\n" 3 Jeff 77.564

結果:shell

No    Name       Mark
1     Sarath     80.35
2     James      91.00
3     Jeff       77.56

輸出彩色文字

echo -e "\e[1;33m This is red text \e[0m"

\e[1;31m 是設置顏色爲紅色 \e[0m 是重置顏色 , 這樣就不會影響後面的輸出數組

前景色:bash

reset = 0, black = 30, red = 31,
green = 32, yellow = 33, blue = 34, magenta = 35, cyan = 36, and white = 37cookie

背景色:app

reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44,
magenta = 45, cyan = 46, and white=47dom

字符替換

cat c1.shthis

eqeqeqweqe eqeweqwewqe eqwewqeqweq   wqeqweqwewqe 
eqwewqe;wqewqewqewqqdfewfr;erqrqrqwrwq;

空格或者; 替換爲換行符加密

cat c1.sh | tr ' |;' '\n'
eqeqeqweqe
eqeweqwewqe
eqwewqeqweq


wqeqweqwewqe

eqwewqe
wqewqewqewqqdfewfr
erqrqrqwrwq

字符串的長度

root@kali:~# content='hello world'
root@kali:~# echo ${#content}
11

識別當前的shell

root@kali:~# echo $SHELL
/bin/bash
root@kali:~# echo $0
bash

檢測當前腳本執行用戶是否是超級用戶

#!/bin/bash
if [ $UID -ne 0 ]; then
 echo Non root user. Please run as root.
else
 echo Root user
fi
root@kali:~/shell# ./c1.sh
Root user

kali@kali:/root/shell$ ./c1.sh 
Non root user. Please run as root.

文件操做

0: stdin (standard input)
1: stdout (standard output)
2: stderr (standard error)

正常的輸出重定向到文件code

root@kali:~# echo aa > stdout.txt 
root@kali:~# cat stdout.txt 
aa

異常的輸出重定向到文件
這種寫不進去

root@kali:~# ls + > stderr.txt 
ls: cannot access '+': No such file or directory
root@kali:~# cat stderr.txt 
root@kali:~#

這樣能夠

root@kali:~# ls + 2> stderr.txt 
root@kali:~# cat stderr.txt 
ls: cannot access '+': No such file or directory

也能夠

root@kali:~# ls + &>> stderr.txt 
root@kali:~# cat stderr.txt 
ls: cannot access '+': No such file or directory
ls: cannot access '+': No such file or directory

輸入重定向

root@kali:~/shell# cat domains.txt 
www.baidu.com
root@kali:~/shell# exec 3< domains.txt 
root@kali:~/shell# cat <&3 
www.baidu.com
root@kali:~/shell#

輸出重定向

root@kali:~/shell# exec 4> output.txt 
root@kali:~/shell# 
root@kali:~/shell# cat output.txt 
root@kali:~/shell# echo haha >&4 
root@kali:~/shell# cat output.txt 
haha
root@kali:~/shell#

數組

root@kali:~/shell# array_var=(a b c d e f)
root@kali:~/shell# 
root@kali:~/shell# echo ${array_var[*]}
a b c d e f
root@kali:~/shell# echo ${array_var[2]}
c
root@kali:~/shell# echo ${#array_var[*]}
6
root@kali:~/shell# echo ${array_var[@]}
a b c d e f
root@kali:~/shell#

別名

del 替換 rm 防止誤操做

alias rm='echo "rm is disabled, use del instead."'
alias del='/bin/rm'

若是要在其餘窗口生效,添加到~/.bashrc

編寫高質量的 shell 腳本

cat 輸出

root@kali:~/shell# cat in.txt 
iinnniniii
root@kali:~/shell# ls -l |cat - in.txt
total 12
-rwxr-xr-x 1 root root 110 Oct 17 11:34 c1.sh
-rw-r--r-- 1 root root  11 Oct 17 15:59 in.txt
-rw-r--r-- 1 root root  28 Oct 17 15:55 out.txt
iinnniniii

find 正則查找

root@kali:~/shell# find . -regex ".*\(\.txt\|\.sh\)$" 
./c1.sh
./in.txt
./out.txt

文件的類型:

時間類型,單位(天)
Access time (-atime): 訪問時間
Modification time (-mtime): 修改時間(內容)
Change time (-ctime): 修改時間(元數據,權限什麼的)
單位(分鐘)
-amin (access time)
-mmin (modification time)
-cmin (change time)

查找類型爲f,最近訪問在7天以前的

find . -type f -atime +7

文件大小

b: 512 byte blocks
c: Bytes
w: Two-byte words
k: Kilobyte (1024 bytes)
M: Megabyte (1024 kilobytes)
G: Gigabyte (1024 megabytes)

find . -type f -atime +7 -size +10M

查找並刪除

find . -name out.txt -delete

xargs 改變輸出的排版

root@kali:~/shell# cat in.txt 
iinnniniii

sds


dsds


dsd
dsds

ds


dsd

1 2
3 4 5 678 99 0 0 -fg  gd gdfg dfh 


dsfdsgdsg  ghgf g f
root@kali:~/shell# cat in.txt | xargs 
iinnniniii sds dsds dsd dsds ds dsd 1 2 3 4 5 678 99 0 0 -fg gd gdfg dfh dsfdsgdsg ghgf g f
root@kali:~/shell# cat in.txt | xargs -n 3
iinnniniii sds dsds
dsd dsds ds
dsd 1 2
3 4 5
678 99 0
0 -fg gd
gdfg dfh dsfdsgdsg
ghgf g f
root@kali:~/shell# echo "splitXsplitXsplitXsplit" | xargs -d X
split split split split

前面的輸出做爲後面的輸入

find . -type f -name "*.txt"  -print | xargs rm -f

tr 轉換

大小寫

root@kali:~/shell# echo "HELLO WHO IS THIS" | tr 'A-Z' 'a-z'
hello who is this

替換和刪除

root@kali:~/shell# echo "HELLO WHO IS THIS 123" | tr 'A-Za-z'   ' '
                  123
root@kali:~/shell# echo "HELLO WHO IS THIS 123" | tr -d 'A-Za-z' 
    123
root@kali:~/shell# echo "HELLO WHO IS THIS 123" | tr -d 'A-Za-z ' 
123

互補性刪除(下面就是刪除非 0-9 的字符 )

root@kali:~/shell# echo "HELLO WHO IS THIS 123" | tr -d -c '0-9\n'
123

壓縮空格

root@kali:~/shell# echo "HELLO   WHO IS  THIS   123" | tr -d ' '
HELLOWHOISTHIS123
root@kali:~/shell# 
root@kali:~/shell# 
root@kali:~/shell# echo "HELLO   WHO IS  THIS   123" | tr -s ' '
HELLO WHO IS THIS 123

累加

root@kali:~/shell# cat sum.txt 
1
2
3
4
5

root@kali:~/shell# cat sum.txt | echo $[ $(tr '\n' '+' ) 0 ]
15

文件加密解密

base64 sum.txt > sum.txt.b64

root@kali:~/shell# cat sum.txt.b64 
MQoyCjMKNAo1Cgo=

root@kali:~/shell# base64 -d sum.txt.b64 
1
2
3
4
5

文件操做

排序

root@kali:~/shell# cat a.txt 
apple
orange
gold
silver
steel
iron
root@kali:~/shell# sort a.txt 
apple
gold
iron
orange
silver
steel

找到公共的內容
首先要把內容排序

root@kali:~/shell# source a.txt -o  a.txt 
root@kali:~/shell# sort b.txt -o  b.txt
root@kali:~/shell# comm a.txt b.txt 
        
apple
        carrot
        cookies
                    gold
iron
                    orange
silver
steel

只顯示公共部分

root@kali:~/shell# comm a.txt b.txt -1 -2

gold
orange

不顯示公共部分

root@kali:~/shell# comm a.txt b.txt -3
apple
        carrot
        cookies
iron
silver
steel
相關文章
相關標籤/搜索