1

I have a script where I output three things into a file. I'd like to call the > operator only once. Is there a way to describe a block of instructions? Should I use a function?

#!/bin/sh

for i in $(ls src)
 do
  f=${i%.*}
  echo 'first bit' > dist/$i.htm
  perl myScriptThatOutputsSecondBit.pl >> dist/$i.htm
  echo 'third bit' >> dist/$i.htm
 done
4

2 回答 2

3

使用compound/group命令:

要在当前 shell 中运行它,

 {echo 'first bit';perl myScriptThatOutputsSecondBit.pl;echo 'third bit';} > dist/$i.htm 


要在子 shell 中运行它,

 (echo 'first bit';perl myScriptThatOutputsSecondBit.pl;echo 'third bit') > dist/$i.htm 
于 2012-05-18T09:09:12.013 回答
0

写一个简单的日志函数,这样会更有效率。例如

log(){
   echo $1 >> dist/$i.htm
}

然后从脚本中调用它:

for i in $(ls src)
do
    f=${i%.*}
    log 'first bit'
    perl myScriptThatOutputsSecondBit.pl >> dist/$i.htm
    log 'third bit'
done
于 2012-05-23T09:30:22.053 回答