您好,登錄后才能下訂單哦!
一、環(huán)境
系統(tǒng):Centos6.6 x64
shell:bash、sh
[centos@Shell ~]$ hostname
Shell
[centos@Shell ~]$ lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description: CentOS release 6.6 (Final)
Release: 6.6
Codename: Final
[centos@Shell ~]$ echo $SHELL
/bin/bash
[centos@Shell ~]$ ifconfig eth0 |grep "t addr"| awk -F '[: ]+' '{print $4}'
192.168.101.110
二、腳本示例
1、終端打印
#!/bin/bash
#The printf of Linux termnal,Include "echo,printf"
#author by woon
echo "The shell\`s name is $0"
#examples of echo,printf
#默認(rèn)打印。輸出換行
echo "Hello World!"
echo 'Hello World!'
#echo -n參數(shù)接受不換行輸出
echo -n "Hello World"
echo "Hello World"
#-e參數(shù)接受雙引號(hào)內(nèi)字符串的轉(zhuǎn)移列表
echo "Hello\tWorld!"
echo -e "Hello\tWorld!"
#printf使用文本或由空格分割的參數(shù),可指定輸出的寬度、對(duì)其方式等,可以格式化輸出,默認(rèn)情況下,printf不輸出換行
printf "%-5s %-10s %-4s\n" No. NAME Mark
printf "%-5s %-10s %-4s\n" 1 Lee 80
printf "%-5s %-10s %-4.2f\n" 2 Woon 90.456
printf "%-5s %-10s %-4.2f\n" 3 James 85.654321
printf "%-5s %-10s %-5.3f\n" 4 Jeff 85.123789
運(yùn)行結(jié)果:
The shell`s name is shell_print.sh
Hello World!
Hello World!
Hello WorldHello World
Hello World!
-e Hello World!
No. NAME Mark
1 Lee 80
2 Woon 90.46
3 James 85.65
4 Jeff 85.124
2、環(huán)境變量
#!/bin/bash
#author by woon
#env#獲取全局環(huán)境變量
env > /tmp/env_$(date +%Y%m%d).$$
head -n 10 /tmp/env_$(date +%Y%m%d).$$
echo "^^^^^^^^^^^^^^^^^分隔符^^^^^^^^^^^^^^^^^^^^^^"
#獲取單個(gè)全局變量的值
echo $PATH
echo "^^^^^^^^^^^^^^^^^分隔符^^^^^^^^^^^^^^^^^^^^^^"
#自定義變量和自定義全局變量,刪除環(huán)境變量
var="Hello World"
echo -n "$var"
echo -n $var
echo ${var}
#設(shè)置全局環(huán)境變量
export VAR
#獲取當(dāng)前的shell版本
echo $SHELL
#or
echo $0
#獲取變量的長度
var="Hello World"
echo ${#var}
#變量內(nèi)容的刪除
path=$PATH
echo $path
echo "最短刪除,非貪婪模式,由前向后刪除"
var1=${path#/*:}
echo $var1
echo "貪婪模式,刪除匹配到最長的,由前向后刪除"
var2=${path##/*:}
echo $var2
echo "非貪婪模式%,由后向前刪除"
var3=${path%:*bin}
echo $var3
echo "貪婪模式,由后向前刪除最長的"
var4=${path%%:*bin}
echo $var4
3、shell數(shù)學(xué)運(yùn)算
#!/bin/bash
#author by woon.
#數(shù)學(xué)運(yùn)算
sum_jia=0
sum_cheng=1
i=1
while [ $i -le 10 ];
do
let "sum_jia+=i"
let "sum_cheng*=i"
let "i += 2"
done
sum_jian1=$[ sum_cheng - sum_jia]
sum_jian2=$(( sum_cheng - sum_jia ))
echo $sum_jia
echo $sum_cheng
echo $sum_jian1
echo $sum_jian2
運(yùn)行結(jié)果
$ ./shell_num.sh
25
945
920
920
4、文件查找和文件列表幾操作
#!/bin/bash
#author by woon
#find 可以基于名字、類型、時(shí)間、大小、目錄深度、大小權(quán)限、用戶等查找并執(zhí)行動(dòng)作
#example
sudo find /etc/ -iregex ".*\(\.py\|\.sh\)" -type f -atime -1 -user root -size -2k -perm 644 -print
#sudo find /etc/ -iregex ".*\(\.py\|\.sh\)" -type f -atime -1 -user root -size -2k -perm 644 -delete
sudo find /etc/ -iregex ".*\(\.py\|\.sh\)" -type f -atime -1 -user root -size -2k -perm 644 -exec ll {} \;
sudo find /etc/ -iregex ".*\(\.py\|\.sh\)" -type f -atime -1 -user root -size -2k -perm 644 -print0 | xargs -0 ls -l
5、tr轉(zhuǎn)換
tr參數(shù)屬于集合映射關(guān)系
tr刪除
[centos@Shell scripts]$ uuidgen | tr -d [a-z]
37643-2050-48-25-547825
tr替換
本替換其實(shí)值將0-a、1-b …… 9-j的映射關(guān)系替換
[centos@Shell scripts]$ echo | md5sum |tr [0-9] [a-z]
gibdcjdajijdedeajjchdiadfcbjcjea -
6、校驗(yàn)和核實(shí)
md5sum
[centos@Shell scripts]$ ls
dialog_t.md5 os_monitor-dialog.sh shell_print.sh test.sh
dialog_t.sh pcpu_usage.sh shell_search.sh top10_commands.sh
inpath shell_num.sh shell_varable.sh
[centos@Shell scripts]$ md5sum os_monitor-dialog.sh >os_monitor-dialog.md5
[centos@Shell scripts]$ md5sum -c os_monitor-dialog.md5
os_monitor-dialog.sh: 確定
腳本示例
#!/bin/bash
#為/etc/passwd生成一個(gè)MD5并校驗(yàn),在生成MD5前檢查/etc/passwd文件是否已經(jīng)被排過序
sort -C /etc/passwd
if [ $? -eq 0 ]; then
echo "Sorted"
else
echo "Unsorted!"
md5sum /etc/passwd > passwd.md5
fi
md5sum -c passwd.md5
7、生成隨機(jī)數(shù)
$RANDOM生成一個(gè)隨機(jī)數(shù);date +%s%N獲取隨機(jī)數(shù)字字符串
腳本如下:
#!/bin/bash
#創(chuàng)建example目錄,在該目錄下批量生成10個(gè)日志文件,日志文件名包含10個(gè)隨機(jī)小寫字母和固定字符串example;當(dāng)前用戶不具有權(quán)限,需要設(shè)置權(quán)限
if [ ! -d /example ]; then
echo "centos"|sudo -S mkdir /example
echo "centos" | sudo -S chown centos:centos /example -R
fi
#產(chǎn)生隨機(jī)數(shù)$RANDOM,或uuidgen命令或者用MD5sum,然后替換
for num in $(seq 1 10)
do
touch /example/$(echo $RANDOM | md5sum |tr "0-9" "a-z"|cut -c 1-10)_example.log
done
8、根據(jù)擴(kuò)展名切分文件名
#!/bin/bash
#shell name:shell_split.sh
#切分文件名并批量重命名或移動(dòng),比如圖形文件等
num=1;
for img in *.jpg *.img
do
mv $img image-$num.{img##*.} 2>/dev/null
if [ $? -eq 0 ]; then
echo "Rename $img to image-$num.${img##*.}"
let num++
fi
done
9、列舉文件類型統(tǒng)計(jì)信息
給出一個(gè)路徑,統(tǒng)計(jì)該路徑下各文件類型的數(shù)量
#!/bin/bash
#shell name:shell_filestat.sh
#列舉文件類型數(shù)量
if [ $# -ne 1 ];
then
echo -e "$0 path\nexample:$0 /etc/"
else
path=$1
declare -A array;
while read line;
do
#echo $(file -b $line)
ftype=$(file -b $line)
let array["$ftype"]++;
done< <(find $path -type f -print)
fi
echo ========== File types and counts ===========
for ftype in "${!array[@]}"
do
echo $ftype : ${array["$ftype"]}
done
10、判斷當(dāng)前系統(tǒng)是否支持該命令
#!/bin/sh
in_path()
{
cmd=$1 path=$2 retval=1
oldIFS=$IFS IFS=":"
for directory in $path
do
if [ -x $directory/$cmd ] ; then
retval=0
fi
done
IFS=$oldIFS
return $retval
}
checkForCmdInPath()
{
var=$1
if [ "$var" != "" ] ; then
if [ "${var%${var#?}}" = "/" ] ; then
if [ ! -x $var ] ; then
return 1
fi
elif ! in_path $var $PATH ; then
return 2
fi
fi
}
if [ $# -ne 1 ] ; then
echo "Usage: $0 command" >&2 ; exit 1
fi
checkForCmdInPath "$1"
case $? in
0 ) echo "$1 found in PATH" ;;
1 ) echo "$1 not found or not executable" ;;
2 ) echo "$1 not found in PATH" ;;
esac
exit 0
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。