shell-script(command groups)

 shell-script��Qshell(�@�e�V傅�linux下的script)

�P�I字:test,if-else, fo-do-done,case

shell-script的��用非常�V泛,各�N�I域都有shell-script的��用,�@�e我��主要�v�v日常��用。

�P於�绦校�一般用source、 sh、  ./相�β��/文件名、 /�^�β��/文件名。�@是一般常用的�绦蟹绞健�

首先�W�shell-script的test,主要�z�y系�y上面文件或者文件的�傩浴>唧wtest的�y��苏I和代表的意�x如下:

第一、�P於文件�型的�z�y。

-e:文件是否存在。

-f:文件存在是否�橐话阄募�

-d:文件存在是否�槟夸�

-b:文件存在是否��K�O�洌�block device)

-c:文件存在是否�樽址��O�洌�character device)

-S:文件存在是否��Socket文件

-p:文件存在是否��FIFO(pipe)文件

-L:文件存在是否�橐���B接文件(�o�hard link or symbol link)

第二、�P於文件的�嘞�z�y。

-r:文件存在且具有可�x�嘞�

-w:文件存在且具有可���嘞�

-x:文件存在且具有可�绦�嘞�

-u:文件存在且具有SUID�傩�

-g:文件存在且具有SGID�傩�

-k:文件存在且具有Sticky bit�傩�

-s:文件存在且�榉强瘴募�

第三、���文件的比�^

-nt:(newer than)文件一比文件二新?

-ot:(older than)文件一比文件二�f?

-ef:文件一和文件二是否�橥�一文件,如hard link ,是否指向同一inode?

第四、整�档谋容^

-eq:值相等(equal)

-ne:值不等(not equal)

-gt:number1大於number2(greater than)

-lt:number1小於number2(less than)

-ge:number1大於等於number2(greater than or equal)

-le:number1小於等於number2(less than or equal)

第五、string的判��

test -z string:string��0?空��true

test -n string:string��0?空��false

test str1=str2:str1等於str2,��true

test str1 != str2 :不等於,��true

第六、多重判�啵ㄒ部沙�檫��判�啵�

-a:�煞N��r同�r��true,�t��true。eg:test -r file -a -x file,file既有可�x和可�绦�r,才��true。

-o:任意一�l��true,�t��true。eg:test -r file -a -x file,file可�x或者可�绦�r都��true,��file同�r不可�x,不可�绦�r才��false。

!:��非。

 

利用[]可以省略test,�烧叩男Ч�一�印5�是在shell-script中使用[]�r,[]�蛇�一定要使用空格。�有就是[]�Y面的�量,最好都用�p引�括起�恚��量用�我��和�p引�括起�矶伎梢浴_@�c很重要。

shell script 的default�量($0,$1,$2...),script�仁褂玫奶厥庾�量如下:

$#:表示後面的�����担� $4代表$#是4。

$@:表示 "$1" "$2" "$3" ,每���量是��立的(用�p引�括起�恚�

$*:表示 "$1 $2 $3"。

其��$@和$*差不多,一般情�r下直接使用或者���$@即可。

使script��灯�移的指令shift,在script中加入shift後��使script的��灯�移。。

 

 

下面�W�if...then...fi,   if...then...else...fi,  if...then...elif...then... else...fi

使用if�l件判��r,�l件判�嗍褂玫倪���\算&&(and)和||(or)相��於test中的-a和-o��怠�

case...in...esac

function功能:function name(){ 程序 }

shell-script中的循�h:有while...do...done, until...do...done(不定循�h), while [ condition ] do programming done ��condition成立�r�_始循�h,知道condition不成立�K止循�h,until [ condition ] do programming done, ��condition不成立�r�_始循�h和while相反,指��condition成立�r�K止循�h。

固定循�h:for...do...done, for variable in val1 val2 val3 do programming done。  for的另一�N用法和C�Z言一�樱�for (( init val; end val; increment val))  do  programming  done。

 

��^�商斓耐耆��W�,�K於�W�完shell-script了。�是重在���`啊。。。

你可能感兴趣的:(shell,职场,script,休闲)