在嵌套目录中运行脚本,而不会在执行目录中产生任何影响

时间:2013-08-08 16:49:36

标签: linux shell

就像标题所说的那样,我希望在嵌套目录结构中运行一个脚本,而不会在执行目录中产生影响(输出进入它所运行文件的结构中的子文件夹中)。

例如:

home/   (where we use the script)
home/data1/file1.txt
home/data2/file2.txt
home/data3/file3.txt

当我创建一个脚本时,我的输出转到:

home/

但我希望输出如此:

home/data1/output1
home/data2/output2
home/data3/output3

实现这一目标的最佳方法是什么?我很感激任何意见。

我的脚本如下,如果您更喜欢特定情况而不是一般情况。

执行脚本的脚本:

#!/bin/bash
# iterate over a NUL-delimited stream of directory names
while IFS='' read -r -d '' dirname; do
  # ...then list files in each directory:
  for file in "$dirname"/*; do
    # ignore directory contents that are not files
    [[ -f $file ]] || continue
    # run analysis tool
    if [[ $file == *.dmp ]]; then
      echo $dirname;
      ./gdp $file;
      #ls $dirname;
      echo "complete";
      continue
    fi
  done
done < <(find . -type d -print0)

要执行的脚本:

#!/bin/bash
echo $1;
awk '
    /Internet Protocol Version 4, Src:/{ip=$(NF-4)}
    /    Time to live/{ttl++}
    /    Time to live/{sttl=$NF}
    /    User-Agent/{}
    /    User-Agent/ && NF>6 {os=$(NF-6)" "$(NF-5)}
    /    User-Agent/ && NF<6 {os="\t"}
    /    User-Agent/{brow=$NF}
    /    User-Agent/{agent++}
    /    User-Agent/{stringtemp=sttl"\t"ip"\t"os"\t"brow}
    /    User-Agent/{string=(string)"\n"(stringtemp)}
    /Windows;/{windows++}
    /Linux;/{linux++}
    /Solaris;/{solaris++}
    /Epoch Time: /{count++}
    END{
        sub(/\\r.*$/, "", brow);
        print os;
        print "("count")" > "temp";
        print "TTL\tIP\t\tOS\t\tBROWSER";
        print string;
        print "\nSUMMARY\n\tttl\t=\t"ttl"\n\twindow\t=\t"windows"\n\tlinux\t=\t"linux"\n\tsolaris\t=\t"solaris >> "temp";
    }
' $1 > summary_$1;
sort -k2n summary_$1 | uniq > holder;
cat temp > useragents_$1;
cat holder >>  useragents_$1;
more useragents_$1;

1 个答案:

答案 0 :(得分:1)

在第一个脚本更改中

./gdp $file;

to

./gdp $file $dirname;
在第二个脚本更改中

' $1 > summary_$1;
sort -k2n summary_$1 | uniq > holder;
cat temp > useragents_$1;
cat holder >>  useragents_$1;
more useragents_$1;

to

' $1 > $2/summary_$1;
sort -k2n $2/summary_$1 | uniq > $2/holder;
cat temp > $2/useragents_$1;
cat $2/holder >>  $2/useragents_$1;
more $2/useragents_$1;