一 什麼是kernel , shell ,shell script
Kernel是linux管理整個電腦硬件系統,並向上層應用提供接口來調用硬件。
shell是用戶和linux交互來控制kenel的一種應用程序。
系統中的shell類型有node
[root@localhost ~]# cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin /bin/tcsh /bin/csh
查看當前使用的shelllinux
[root@localhost ~]# echo $SHELL /bin/bash
shell script 是把命令堆砌在一塊兒,來實現特定功能的程序化腳本。shell
二 變量:可變化的量,命名內存空間
bash環境:
本地變量:當前shell進程;
環境變量:當前shell進程及其子進程;
局部變量:某個函數執行過程;
位置參數變量:在腳本中引用傳遞給腳本的參數;在函數中引用傳遞給函數的參數;
特殊變量:$?, $*, $@, $#, $$vim
定義變量
name=value
name: 變量名
=:賦值符號
value:值
變量名:只能包含數字、字母和下劃線;且不能以數字開頭;
引用變量:${name}, $name
bash
[root@localhost ~]# x=myx #設置x的值爲myx [root@localhost ~]# echo $x #顯示x的值,使用$引用 myx 10x=myx #以數字開頭命名變量,顯示錯誤 bash: 10x=myx: 未找到命令...
引用:
弱引用: "", 其內部的變量引用會被替換爲變量值;
強引用:'',其變量的變量引用會保持原有字符;
命令引用:`COMMAND`, $(COMMAND),引用命令的執行結果;
ide
[root@localhost ~]# x=myx [root@localhost ~]# echo "x is $x" #弱引用,$x替換 x is myx [root@localhost ~]# echo 'x is $x' #強引用,$x不替換 x is $x [root@localhost ~]# uname -r 3.10.0-229.el7.x86_64 [root@localhost ~]# cd /lib/modules/`uname -r`/kernel #命令替換 `uname -r`/ 替換爲3.10.0-229.el7.x86_64 [root@localhost kernel]# pwd /lib/modules/3.10.0-229.el7.x86_64/kernel
聲明爲整型:
declare -i name[=value]
let name=value
生命週期:
建立
銷燬:
自動銷燬:shell進程終止;
手動銷燬:unset name函數
[root@localhost ~]# test=mytest #設置變量 [root@localhost ~]# echo $test #變量能夠顯示 mytest [root@localhost ~]# unset test #銷燬變量 [root@localhost ~]# echo $test #在次echo,顯示爲空
環境變量:
被「導出」的本地變量
export name[=value]
declare -x name[=value]
累加變量 『PATH="$PATH":/home/bin』或『PATH=${PATH}:/home/bin』
查看全部環境變量:env, printenv, export測試
三 命令狀態結果:
bash進程用於追蹤執行的命令成功與否的狀態:
0: 成功
1-255:失敗ui
特殊變量:
$?:上一條命令的執行狀態結果;this
布爾型:
「真」:成功
「假」:失敗
自定義腳本的狀態結果:
exit [n]
注意:腳本中任何位置執行了exit命令即會終止當前shell進程;
[root@localhost ~]# cat /etc/passwd [root@localhost ~]# echo $? 0 [root@localhost ~]# vim test.sh #!/bin/bash cat /etc/passwd exit 3 [root@localhost ~]# bash test.sh [root@localhost ~]# echo $? 3
四 條件測試:
界定程序執行環境;
(1) 根據運行的命令的狀態結果;
(2) 測試表達式
test EXPRESSION
[ EXPRESSION ]
` EXPRESSION `
整數測試:隱含着作數值大小比較,因此不要給變量引用加引用;
$A -gt $B:是否大於;是則爲「真」,不然爲「假」;
$A -ge $B: 是否大於等於;
$A -lt $B:是否小於;
$A -le $B: 是否小於等於;
$A -eq $B: 是否等於;
$A -ne $B:是否不等於;
[root@localhost ~]# A=10 [root@localhost ~]# B=20 [root@localhost ~]# C=10 [root@localhost ~]# test $A -gt $B #A 大於B [root@localhost ~]# echo $? #假 1 [root@localhost ~]# test $A -lt $B #A小於B [root@localhost ~]# echo $? #真 0 [root@localhost ~]# [ $A -lt $B ] #用中括號測試 [root@localhost ~]# echo $? 0 [root@localhost ~]# [[ $A -lt $B ]] #雙中知號一樣能夠測試 [root@localhost ~]# echo $? 0 [root@localhost ~]# test $A -lt $C #A小於C [root@localhost ~]# echo $? #假 1 [root@localhost ~]# test $A -ge $C #A大於等於C [root@localhost ~]# echo $? #真 0 [root@localhost ~]# test $A -le $C #A小於等於C [root@localhost ~]# echo $? #真 0 [root@localhost ~]# test $A -eq $C #A等於C [root@localhost ~]# echo $? #真 0 [root@localhost ~]# test $A -ne $C # A不等於C [root@localhost ~]# echo $? #假 1 [root@localhost ~]# test $A -ne $B #A不等於B [root@localhost ~]# echo $? #真 0
字符串測試:ASCII數值越大,字符比較時其值越大;
"$A" > "$B":是否大於;
"$A" < "$B":是否小於;
"$A" == "$B":是否等於;
"$A" != "$B":是否不等於;
-z "$A":是否爲空;空則爲「真」,不然爲「假」
-n "$A":是否不空;不空則「真」,空則爲「假」
注意: 字符串測試應該使用雙中括號 ` EXPRESSION `
[root@localhost ~]# A=pass [root@localhost ~]# B=pbaa [root@localhost ~]# C=pass [root@localhost ~]# test "$A">"$B" #pa 在pb前面 ,因此A大於B [root@localhost ~]# echo $? 0 [root@localhost ~]# test "$A"=="$C" [root@localhost ~]# echo $? 0
文件測試:測試文件的存在性以及屬性;
-e $file: 是否存在;存在則爲「真」,不然爲「假」;
-a $file: 同上;
-f $file:文件是否存在且爲普通文件;
-d $file:文件是否存在且爲目錄;
-h $file:是否存在且爲符號連接文件;
-L $file: 同上
-b $file:是否存在且爲塊設備文件;
-c $file:是否存在且爲字符設備文件;
-S $file:是否存在且爲套接字文件;
-p $file: 是否存在且爲管道文件;
-r $file: 當前用戶對文件是否擁有讀權限;
-w $file:當前用戶對文件是否擁有寫權限;
-x $file:當前用戶對文件是否擁有執行權限;
-u $file:文件是否擁有SUID權限;
-g $file:文件是否擁有SGID權限;
-k $file:文件是否擁有sticky權限;
-O $file: 當前用戶是否爲指定文件的屬主;
-G $file: 當前用戶是否爲指定文件的屬組;
雙目操做符:
$file1 -nt $file2: file1是否新於file2, file1的最近一次的修改時間戳是否晚於file2的;
$file1 -ot $file2: file1是否舊於file2, file1的最近一次的修改時間戳是否早於file2的;
$file1 -ef $file2:file1與file2是否指向了同一個inode;測試兩者是否爲同一個文件的硬連接;
[root@localhost ~]# mkdir /test [root@localhost ~]# cd /test [root@localhost test]# touch test1.txt [root@localhost test]# test -e test1.txt #測試是否存在文件test1.txt [root@localhost test]# echo $? #真 0 [root@localhost test]# test -e test2.txt #測試是否存在文件test2.txt [root@localhost test]# echo $? #假 1 [root@localhost test]# test -a test1.txt #測試是否存在文件test1.txt [root@localhost test]# echo $? #真 0 [root@localhost test]# test -f test1.txt #測試test1.txt是否爲普通文件 [root@localhost test]# echo $? 0 [root@localhost test]# test -O test1.txt #測試test1.txt的屬主是否和當前用戶一至 [root@localhost test]# echo $? #真 0 [root@localhost test]# test -G test1.txt #測試test1.txt的屬主是否和當前用戶一至 [root@localhost test]# echo $? #真 0 [root@localhost test]# useradd cenos [root@localhost test]# [root@localhost test]# su - cenos [cenos@localhost ~]$ cd /test [cenos@localhost test]$ test -O test1.txt #測試test1.txt的屬主是否和當前用戶一至 [cenos@localhost test]$ echo $? #假 1 [cenos@localhost test]$ test -G test1.txt #測試test1.txt的屬主是否和當前用戶一至 [cenos@localhost test]$ echo $? #假 1 [root@localhost test]# touch test2.txt [root@localhost test]# test test2.txt -nt test1.txt #測試test2.txt修改時間是否晚於text1.txt [root@localhost test]# echo $? #真 0 [root@localhost test]# test test2.txt -ot test1.txt #測試test2.txt修改時間是否早於text1.txt [root@localhost test]# echo $? #假 1 [root@localhost test]# ln test1.txt test3.txt [root@localhost test]# test test3.txt -ef test1.txt #測試test3.txt test1.txt是否爲同一文件硬連接 [root@localhost test]# echo $? #真 0
五 選擇執行 if else fi
if condition
then
condition 判斷狀態爲直,$?值爲0
執行 commands
else
condition 判斷狀態爲假,$?值爲1-255
執行 commands
fi
或者
if condition
then
condition 判斷狀態爲直,$?值爲0
執行 commands
elif condition1
then
condition1 判斷狀態爲直,$?值爲0
執行 commands
elif condition2
then
condition2 判斷狀態爲直,$?值爲0
執行 commands
else
condition 判斷狀態爲假,$?值爲1-255
執行 commands
fi
例如
[root@localhost test]# vim test.sh echo "Please choose one" echo "1.show test1" echo "2.show test2" echo -n "Select your os choice [1 or 2]? " read test if [ $test -eq 1 ] ; then echo "choose (test1)" else if [ $test -eq 2 ] ; then echo "choose (test2)" else echo "choose else " fi fi [root@localhost test]# bash test.sh Please choose one 1.show test1 2.show test2 Select your os choice [1 or 2]? 1 choose (test1) [root@localhost test]# bash test.sh Please choose one 1.show test1 2.show test2 Select your os choice [1 or 2]? 2 choose (test2) [root@localhost test]# bash test.sh Please choose one 1.show test1 2.show test2 Select your os choice [1 or 2]? 3 choose else [root@localhost test]# bash test.sh Please choose one 1.show test1 2.show test2 Select your os choice [1 or 2]? test.sh: line 7: [: -eq: unary operator expected test.sh: line 13: [: -eq: unary operator expected choose else
六 腳本參數(位置參數變量):
# ./script.sh /etc/fstab /etc/grub2.cfg
$0 $1 $2
位置參數變量:$1, $2, ...
${10}
特殊變量:
$?: 命令的狀態結果;
$#: 傳遞給腳本或函數的參數的個數;
$*和$@: 引用傳遞給腳本或函數的參數列表;
shift [n]:輪替
[root@localhost test]# vim scrip.sh #!/bin/bash echo "Total number of command line argument are $#" echo "$0 is script name" echo "$1 is first argument" echo "$2 is second argument" echo "All of them are :- $* or $@" [root@localhost test]# bash scrip.sh test1 test2 test3 test4 Total number of command line argument are 4 scrip.sh is script name test1 is first argument test2 is second argument All of them are :- test1 test2 test3 test4 or test1 test2 test3 test4
[root@localhost test]# vim scrip2.sh echo "$1 is first argument" shift echo "$1 is second argument" shift echo "$1 is third argument" [root@localhost test]# bash scrip2.sh test1 test2 test3 test4 test1 is first argument test2 is second argument test3 is third argument
七 循環
循環 for
for { variable } in { list }
do
執行命令,直到list到最後一個
done
LIST的生成方法:
(1) 整數列表
(a) {start..end} {1..10}
(b) $(seq [start `step` end) seq 0 2 10 (0 2 4 6 8 10)
(2) 直接給出列表 user1 user2 user3 user4 user5
(3) glob /var/log/*
(4) 命令生成 $(cut -d: -f1 /etc/passwd)
[root@localhost test]# vim for1.sh for i in 1 2 3 4 5 do echo "Welcome $i times" done [root@localhost test]# bash for1.sh Welcome 1 times Welcome 2 times Welcome 3 times Welcome 4 times Welcome 5 times
循環 while, until
while [ condition ]
do
command1
command2
....
done
while循環
進入條件:當CONDITION爲「真」;
退出條件:當CONDITION爲「假」;
until [ condition ]
do
command1
command2
....
done
unitl循環
進入條件:當CONDITION爲「假」時
退出條件:當CONDITION爲「真」時
[root@localhost test]# vim testwhile.sh #!/bin/bash if [ $# -eq 0 ] then echo "Error - please input like bash testwhile.sh no." exit 1 fi n=$1 i=1 while [ $i -le 10 ] do echo "$n * $i = `expr $i \* $n`" i=`expr $i + 1` done [root@localhost test]# bash testwhile.sh Error - please input like bash testwhile.sh no. [root@localhost test]# bash testwhile.sh 5 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
循環 case
case $variable in
pattern1) command
...
command;;
pattern2) command
...
command;;
patternN) command
...
command;;
*) command
...
command;;
esac
八 函數
定義方法:
(1) function f_name
{
函數體
}
(2) f_name()
{
函數體
}
調用函數:
f_name [argu1, argu2, ...]
九 sed & awk
練習:
五、寫一個腳本:若是某路徑不存在,則將其建立爲目錄;不然顯示其存在,並顯示內容類型;(不要懷疑,就是這麼簡單)
mkdir /test [root@localhost test]# vim /test/homework1.sh #!/bin/bash if [ $# -lt 1 ] then echo "usage: command file" exit 1 fi if [ -e $1 ] then echo "file exist" file $1 else echo " create fold $1" mkdir -p $1 fi [root@localhost test]# cd /test/ [root@localhost test]# bash homework1.sh /test/test create fold /test/test [root@localhost test]# bash homework1.sh /test/test file exist /test/test: directory [root@localhost test]# touch /test/test/test.txt [root@localhost test]# bash homework1.sh /test/test/test.txt file exist /test/test/test.txt: empty
六、寫一個腳本,完成以下功能;判斷給定的兩個數值,孰大孰小;給定數值的方法:腳本參數,命令交互;(使用read,依然如此簡單)
a腳本參數
[root@localhost test]# vim homework2.sh else echo "$1 is equal $2" #!/bin/bash if [ $# -lt 2 ] then echo "usage: command data1 data2" exit 1 fi if [ $1 -gt $2 ] then echo "$1 is big than $2" else if [ $1 -eq $2 ] then echo "$1 is equal $2" else echo "$1 is small than $2" fi fi [root@localhost test]# bash homework2.sh 10 20 10 is small than 20 [root@localhost test]# bash homework2.sh 20 10 20 is big than 10 [root@localhost test]# bash homework2.sh 20 usage: command data1 data2
b命令交互 read
[root@localhost test]# vim homework3.sh #!/bin/bash read -p "Plz enter two integer: " -t 20 num1 num2 if [ $num1 -gt $num2 ] then echo "$num1 is big than $num2" else if [ $num1 -eq $num2 ] then echo "$num1 is equal $num2" else echo "$num1 is small than $num2" fi fi [root@localhost test]# bash homework3.sh Plz enter two integer: 10 20 10 is small than 20 [root@localhost test]# bash homework3.sh Plz enter two integer: 20 10 20 is big than 10 [root@localhost test]# bash homework3.sh Plz enter two integer: 10 10 10 is equal 10
七、求100之內全部奇數之和(至少用3種方法。是的這是咱們的做業^_^)
方法a for 判斷 [root@localhost test]# vim sum100.1.sh #!/bin/bash declare -i sum=0 for i in {1..100} do if [ $[$i%2] -eq 1 ] then sum=$[$sum+$i] fi done echo "Sum is : $sum" [root@localhost test]# bash sum100.1.sh Sum is : 2500
方法b for list步進 [root@localhost test]# vim sum100.2.sh #!/bin/bash declare -i sum=0 for i in $(seq 1 2 100) do sum=$(($sum+$i)) done echo "Sum is : $sum" [root@localhost test]# bash sum100.2.sh Sum is : 2500
方法c while 判斷 [root@localhost test]# vim sum100.3.sh declare -i sum=0 declare -i i=0 while [ $i -le 100 ] do if [ $[$i%2] -eq 1 ] then let sum+=$i fi let i++ done echo "sum is : $sum" [root@localhost test]# bash sum100.3.sh sum is : 2500
八、寫一個腳本實現以下功能:
(1) 傳遞兩個文本文件路徑給腳本;
(2) 顯示兩個文件中空白行數較多的文件及其空白行的個數;
(3) 顯示兩個文件中總行數較多的文件及其總行數;
[root@localhost test]# vim countspace.sh if [ ! -f $1 ] then echo "$1 is not a common file " exit 1 fi if [ ! -f $2 ] then echo "$2 is not a common file" exit 1 fi file1_line=`cat $1 | wc -l` echo $file1_line file2_line=`cat $2 | wc -l` echo $file2_line if [ $file1_line -eq $file2_line ] then echo "both file have $file1_line lines" elif [ $file1_line -gt $file2_line ] then echo " $1 have $file1_line lines more than $2 have $file2_line" else echo " $2 have $file2_line lines more than $1 have $file1_line" fi file1_spaceline=`grep "^$" $1 | wc -l` echo $file1_spaceline file2_spaceline=`grep "^$" $2 | wc -l` echo $file2_spaceline if [ $file1_spaceline -eq $file2_spaceline ] then echo "both file have $file1_spaceline lines" elif [ $file1_spaceline -gt $file2_spaceline ] then echo " $1 have $file1_spaceline space lines more than $2 have $file2_spaceline" else echo " $2 have $file2_spaceline space lines more than $1 have $file1_spaceline" fi [root@localhost test]# bash countspace.sh /test/countspace.sh /etc/fstab 51 16 /test/countspace.sh have 51 lines more than /etc/fstab have 16 9 1 /test/countspace.sh have 9 space lines more than /etc/fstab have 1 [root@localhost test]# bash countspace.sh /test/countspace.sh useage: scrip filepath1 filepath2 [root@localhost test]# bash countspace.sh /test/countspace.sh /etc /etc is not a common file [root@localhost test]# bash countspace.sh useage: scrip filepath1 filepath2
九、寫一個腳本
(1) 提示用戶輸入一個字符串;
(2) 判斷:
若是輸入的是quit,則退出腳本;
不然,則顯示其輸入的字符串內容;
[root@localhost test]# vim quitscript.sh #!/bin/bash read -p "please input a string:" -t 10 str1 if [[ -z "$str1" ]] then echo "please input something" elif [[ "$str1" == "quit" ]] then exit 0 else echo "you inupt is $str1" fi [root@localhost test]# bash quitscript.sh please input a string: please input something [root@localhost test]# bash quitscript.sh please input a string:test you inupt is test [root@localhost test]# bash quitscript.sh please input a string:quit
十、寫一個腳本,打印2^n表;n等於一個用戶輸入的值;(很差意思,我調皮了)
[root@localhost test]# vim power2.sh #!/bin/bash if [ $# -lt 1 ] then echo "usage: command number" exit 1 fi if [ $1 -le 0 ] then echo "please input a number big the 0" elif [ $1 -eq 1 ] then echo "2x1=2" else sum=2 echo -n "2" for i in `seq 2 $1` do let sum*=2 echo -n "X2" done echo "=$sum" fi [root@localhost test]# bash power2.sh usage: command number [root@localhost test]# bash power2.sh 0 please input a number big the 0 [root@localhost test]# bash power2.sh 1 2x1=2 [root@localhost test]# bash power2.sh 2 2X2=4 [root@localhost test]# bash power2.sh 3 2X2X2=8 [root@localhost test]# bash power2.sh 4 2X2X2X2=16 [root@localhost test]# bash power2.sh 8 2X2X2X2X2X2X2X2=256 [root@localhost test]# bash power2.sh 32 2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2X2=4294967296
十一、寫一個腳本,寫這麼幾個函數:函數一、實現給定的兩個數值的之和;函數二、取給定兩個數值的最大公約數;函數三、取給定兩個數值的最小公倍數;關於函數的選定、兩個數值的大小都將經過交互式輸入來提供。
[root@localhost test]# vim testfun.sh #!/bin/bash while [ ture ] do echo "calcule usage is script operater number1 number2" echo "operater have 3 methord:" echo -e "1.summary usage example: \033[31msum number1 number2\033[0m" echo -e "2.Least Common Multiple (LCM) usage example: \033[31mlcm number1 number2\033[0m" echo -e "3.Greatest Common Divisor(GCD) usage example: \033[31mgcd number1 number2\033[0m" echo -e "inupt \033[31mquit\033[0m this script will exit" read -p "please input select: " op num1 num2 function testsum() { clear echo "=================================================" echo -e "\033[31msum is `expr $num1 \* $num2` \033[0m" echo "=================================================" } function gongyue() { a=$num1 local b=$num2 local tmp=0 if [ $a -lt $b ] then tmp=$a a=$b b=$tmp fi while [ $b -ne 0 ] do tmp=$(($a%$b)) a=$b b=$tmp done clear echo "=================================================" echo -e "\033[31mgongyue is $a\033[0m" echo "=================================================" } function gongbei() { clear echo "=================================================" echo -e "\033[31mgongbei is $(($num1*$num2/$a))\033[0m" echo "=================================================" } if [[ "$op" == "quit" ]] then break exit 0 elif [[ "$op" == "sum" ]] then testsum num1 num2 elif [[ "$op" == "lcm" ]] then gongyue num1 num2 elif [[ "$op" == "gcd" ]] then gongbei num1 num2 else echo "input error" fi done [root@localhost test]# bash testfun.sh calcule usage is script operater number1 number2 operater have 3 methord: 1.summary usage example: sum number1 number2 2.Least Common Multiple (LCM) usage example: lcm number1 number2 3.Greatest Common Divisor(GCD) usage example: gcd number1 number2 inupt quit this script will exit please input select: sum 1397 2413 ================================================= sum is 3370961 ================================================= calcule usage is script operater number1 number2 operater have 3 methord: 1.summary usage example: sum number1 number2 2.Least Common Multiple (LCM) usage example: lcm number1 number2 3.Greatest Common Divisor(GCD) usage example: gcd number1 number2 inupt quit this script will exit please input select: quit please input select: lcm 1397 2413 ================================================= gongyue is 127 ================================================= calcule usage is script operater number1 number2 operater have 3 methord: 1.summary usage example: sum number1 number2 2.Least Common Multiple (LCM) usage example: lcm number1 number2 3.Greatest Common Divisor(GCD) usage example: gcd number1 number2 inupt quit this script will exit please input select: gcd 1397 2413 ================================================= gongbei is 26543 ================================================= calcule usage is script operater number1 number2 operater have 3 methord: 1.summary usage example: sum number1 number2 2.Least Common Multiple (LCM) usage example: lcm number1 number2 3.Greatest Common Divisor(GCD) usage example: gcd number1 number2 inupt quit this script will exit please input select: quit