shell 練習

   



符號
做用
= 賦值符號
「」 弱引用 其內部的變量引用會被替換爲變量值
‘’ 強引用 其變量的變量引用會保持原有字符
.
字符匹配,這是做爲正則表達式的一部分,用來匹配任何單個字符。

逗號連接一系列的算術操做,雖然裏面的內容所有運行,但只有最後一項被返回
\
轉義字符,如\X  等價於"X"或'X'
:
空命令,等價於"NOP"(no  op,一個什麼也不幹的命令).也能夠被認爲與shell 的內建命令
·· 命令引用 (true)做用相同.":"命令是一
#!
個  bash 的內建命令,它的返回值爲0,就是shell 返回的true.
#
註釋,用於幫助信息或者忽略暫時不執行的語句
${}
變量正規表達式,避免變量名提早截斷
$num 位置參數 $0,$1,…,${10};(注:如多於兩位數需加中括號)
$? 狀態斷定參數 通常0表示正確(真),其它數字表錯誤(假)
$!
最後一個命令執行的後臺命令的ID
$$
運行腳本進程的ID
$#
傳遞給腳本或函數的參數的個數
$*和$@
引用傳遞給腳本或函數的參數列表
()
指令羣組,將一串指令括起來,執行時shell會產生subshell來執行它們
(())
bash的內建功能,用於算數運算
[] 中括號 1  在流程控制中表示判斷式;  2  在正則表達式中表示"範圍"或"集合"
[[]]
中括號的增強版,當中容許使用||和&&,而且可使用正則表達式
{} 花括號 指令羣組,相似於(),但在當前shell中執行,還能夠用於字符串的組合















整數測試
隱含着作數值大小比較,因此不要給變量引用加引用
-gt
是否大於,是則爲真 ,不然爲假
-ge
是否大於等於
-lt
是否小於
-le
是否小於等於
-eq
是否等於
-ne
是否不等於
字符串測試 ASCII數值越大,字符比較時其值越大,(注:使用時應用[[]])
>
是否大於
<
是否小於
==
是否等於
!= 
是否不等於
-z
是否爲空,空則爲真,不然爲假
-n
是否不空,不空爲真,不然爲假
文件測試
測試文件的存在性以及屬性
-e $FILE
是否存在,存在爲,真不然爲假
-f $FILE
文件是否存在且爲普通文件
-d
存在且爲目錄
-h
存在且爲符號連接文件
-b
存在且爲塊設備文件
-c
存在且爲字符設備文件
-s
存在且爲套接字文件
-p
存在且爲管道文件
-r
當前用戶對文件是否擁有讀權限
-w
當前用戶對文件是否擁有寫權限
-x
當前用戶對文件是否擁有執行權限
-u
文件是否擁有suid權限
-g
文件是否擁有sgid權限
-k
文件是否擁有sticky權限
-O
當前用戶是否爲指定文件的屬主
-G
當前用戶是否爲指定文件的屬組




寫一個腳本:若是某路徑不存在,則將其建立爲目錄;不然顯示其存在,並顯示內容類型正則表達式

#!/bin/bashshell

#bash

if [ $# -lt 1 ]; thenide

echo "Plz enter a path:"函數

exit 1測試

fiui


if [ -e $1 ]; thenspa

file $1進程

elseci

mkdir -p $1

fi


~

測試腳本:

[root@www bin]# bash  test20.sh

Plz enter a path:

[root@www bin]# bash -x test20.sh abc

+ '[' 1 -lt 1 ']'

+ '[' -e abc ']'

+ mkdir -p abc

[root@www bin]# bash -x test20.sh abc

+ '[' 1 -lt 1 ']'

+ '[' -e abc ']'

+ file abc

abc: directory


寫一個腳本,完成以下功能;判斷給定的兩個數值,孰大孰小;給定數值的方法:腳本參數,命令交互;


#!/bin/bash

#

if (($# < 2)); then

echo "Plz enter Two integer Numbers!"

exit 1

fi


if (($1 > $2)); then

echo "$1 > $2"

elif

(($1 < $2)); then

echo "$1 < $2"

elif

(($1 == $2)); then

echo "$1 = $2"

else

echo "disparate"

fi

~


測試腳本

[root@www bin]# bash test21.sh

Plz enter Two integer Numbers!

[root@www bin]# bash test21.sh 4

Plz enter Two integer Numbers!

[root@www bin]# bash test21.sh 4 5

4 < 5

[root@www bin]# bash test21.sh 5 5

5 = 5

[root@www bin]# bash test21.sh 5 4

5 > 4


求100之內全部奇數之和(至少用3種方法)

  1. 使用for

#!/bin/bash

#

for i in $(seq 100); do

if [ $[$i%2] -ne 0 ]; then

h=$[$h+$i]

fi

done


echo "sum:$h"

測試腳本:

[root@www bin]# bash test22.sh

sum:2500


2.使用while

#!/bin/bash

#

h=1

j=0

while [ $h -le 100 ]; do

if [ $[$h%2] -ne 0 ]; then

j=$[$h+$j]

fi

let h++

done

echo $j


測試腳本:

[root@www bin]# bash test23.sh

2500


3.暫時想不到



寫一個腳本實現以下功能:

(1) 傳遞兩個文本文件路徑給腳本;

(2) 顯示兩個文件中空白行數較多的文件及其空白行的個數;

(3) 顯示兩個文件中總行數較多的文件及其總行數

#!/bin/bash

#


if [ $# -lt 2 ]; then

echo "Plz enter Two path:"

exit 1

fi


if [ ! -f $1 ]; then

echo "$1 not a common file!"

exit 1

fi

if [ ! -f $2 ]; then

echo "$2 not a common file!"

exit 1

fi

j=$(grep -c "^$" $1)

h=$(grep -c "^$" $2)

m=$(cat $1 |wc -l)

n=$(cat $2 |wc -l)

if [ $j -ge $h ]; then

echo "$1 is max ,the spaceline total:$j"

else

echo "$2 is max, the spaceline total:$h"

fi

if [ $m -ge $n ]; then

echo "$1 is max, the lines total:$m"

else

echo "$2 is max,the lines total:$n"

fi


測試腳本:

[root@www bin]# bash -x test24.sh /etc/fstab /etc/yum.conf

+ '[' 2 -lt 2 ']'

+ '[' '!' -f /etc/fstab ']'

+ '[' '!' -f /etc/yum.conf ']'

++ grep -c '^$' /etc/fstab

+ j=1

++ grep -c '^$' /etc/yum.conf

+ h=3

++ wc -l

++ cat /etc/fstab

+ m=10

++ wc -l

++ cat /etc/yum.conf

+ n=26

+ '[' 1 -ge 3 ']'

+ echo '/etc/yum.conf is max, the spaceline total:3'

/etc/yum.conf is max, the spaceline total:3

+ '[' 10 -ge 26 ']'

+ echo '/etc/yum.conf is max,the lines total:26'

/etc/yum.conf is max,the lines total:26

[root@www bin]# bash test24.sh

Plz enter Two path:

[root@www bin]# bash test24.sh abc

Plz enter Two path:

[root@www bin]# bash test24.sh abc cba

abc not a common file!


九、寫一個腳本

(1) 提示用戶輸入一個字符串;

(2) 判斷:

若是輸入的是quit,則退出腳本;

不然,則顯示其輸入的字符串內容

#!/bin/bash

#

read -p "Plz input a string:" -t 5 a

if [ -z $a ]; then

echo "Plz enter string!"

exit 1

fi

if [[ $a == "quit" ]]; then

exit 1

else

echo "$a"

fi

相關文章
相關標籤/搜索