1. 程式人生 > >shell腳本6--循環,比較

shell腳本6--循環,比較

-- not passwd let then || com code commands

for循環

for var in list;

do

  commands;#使用變量$var

done

example:

  for i in {a..z}; do actions; done;

後者

for((i=0;i<10;i++))

{

  commands;

}

while condition

do

  commands;

done

until循環

example:

x=0;

until [ $x -eq 9 ];

do

  let x++; echo $x;

done

if比較語句

if contions;

then

  commands;

if

else if和else語句

if condition;

then

  commands;

else if conditon;then

  commands;

else

  commands;

fi

簡潔用法:

[ condition ] && action;

[ condition ] || action;

算數比較:
[ $var -eq 0 ]

[ $var -ne 0 ]

邏輯與和邏輯或

[ $var1 -ne 0 -a $var2 -gt 2 ]

[ $var1 -ne 0 -o $var2 -gt 2 ]

example:

  

#!/bin/bash
#filename:compare.
sh fpath="/etc/passwd" if [ -e $fpath ]; then echo file exits; else echo does not exists; fi

shell腳本6--循環,比較