首页 > 代码库 > csh与bash比较

csh与bash比较

csh与bash比较:
一、csh的while循环控制结构及if then:
#!/bin/csh -f
while ($#argv >= 1)
  if ("$1" == "-s") then
    shift
    if ($#argv >= 1) then
      set source = $1
      shift
    endif
  else if ("$1" == "-c") then
    set complex = "-text"
    shift
  else
    if ($?text == "0") then
      set text = $1
    endif
    shift
  endif
end

而bash的for结构的if then :
#!/bin/sh
for file in *
do
   if grep -q POSIX $file
   then
      echo $file
   fi
done
exit 0
即c语言风格的csh,如if endif, while end结对,而linux下的bash形式为 if  fi,  for do done.

比较二:
csh的判断文件存在:
if (-e $MGDATA/${text}.chunks) then
  set input_files = `cat $MGDATA/${text}.chunks`
endif

而bash则是:
if test -f fred.c
then
...
fi
或者使用
if [ -f fred.c ]
then
...
fi
即方括号[]相当test命令的效果,注意:如果需要把then放在if的同一行,需要在方括号[]后加一个分号;
if [ -f fred.c ]; then
...
fi


root@host% tnpdump 
   Name                TNPaddr   MAC address    IF     MTU E H R
cluster1.node0     0x1100001 02:00:00:01:00:04 em0    1500 2 0 3
node0.fpc3         0x1100013 02:00:00:01:00:13 em0    1500 5 0 3
node0.fpc5         0x1100015 02:00:00:01:00:15 em0    1500 4 0 3
node0.fpc11        0x110001b 02:00:00:01:00:1b em0    1500 5 0 3
node0.fpc3.pic0    0x1100113 02:00:00:01:01:13 em0    1500 2 0 3
node0.fpc5.pic0    0x1100115 02:00:00:01:01:15 em0    1500 2 0 3
node0.fpc3.pic1    0x1100213 02:00:00:01:02:13 em0    1500 3 0 3
node0.fpc5.pic1    0x1100215 02:00:00:01:02:15 em0    1500 3 0 3
cluster1.node1     0x2100001 02:00:00:02:00:04 em0    1500 0 0 3
cluster1.node1     0x2100001 02:00:01:02:00:04 em1    1500 0 1 3
node1.re0          0x2100004 02:00:00:02:00:04 em0    1500 0 0 3
node1.re0          0x2100004 02:00:01:02:00:04 em1    1500 0 1 3
node1.fpc3         0x2100013 02:00:00:02:00:13 em0    1500 4 0 3
node1.fpc5         0x2100015 02:00:00:02:00:15 em0    1500 4 0 3
node1.fpc11        0x210001b 02:00:00:02:00:1b em0    1500 5 0 3
node1.fpc3.pic0    0x2100113 02:00:10:02:01:13 em0    1500 3 0 3
node1.fpc5.pic0    0x2100115 02:00:00:02:01:15 em0    1500 3 0 3
node1.fpc3.pic1    0x2100213 02:00:10:02:02:13 em0    1500 2 0 3
node1.fpc5.pic1    0x2100215 02:00:00:02:02:15 em0    1500 3 0 3
node1.fpc3.pic2    0x2100313 02:00:10:02:03:13 em0    1500 3 0 3
node1.fpc3.pic3    0x2100413 02:00:10:02:04:13 em0    1500 2 0 3
cluster1.master    0xf100001 02:00:00:02:00:04 em0    1500 0 0 3
cluster1.master    0xf100001 02:00:01:02:00:04 em1    1500 0 1 3
bcast             0xffffffff ff:ff:ff:ff:ff:ff em0    1500 0 0 3
bcast             0xffffffff ff:ff:ff:ff:ff:ff em1    1500 0 1 3

 

root@host% cat p.sh 
#!/bin/csh

foreach pic (`tnpdump | awk ‘{print $1}‘ | grep pic`) 
        echo $pic 
end

 

root@host% ./p.sh 
node0.fpc3.pic0
node0.fpc5.pic0
node0.fpc3.pic1
node0.fpc5.pic1
node1.fpc3.pic0
node1.fpc5.pic0
node1.fpc3.pic1
node1.fpc5.pic1
node1.fpc3.pic2
node1.fpc3.pic3

csh与bash比较