Frage

I'm trying to create a shell script that:

  • copies a file from my log directory to a separate directory for analysis.

  • Gunzips that file and looks for a text pattern

  • Outputs that pattern to a file for further analysis

  • deletes said file

I'm trying to do this in stages. So far I haven't been able to get off the ground. SIGH...

I pulled this example from here and started amending it:

#!/bin/bash


FILES= `ls /opt/dir1/scripts/access_*.gz`



for i in $FILES
  do
    cp $i /tmp/apache
    gunzip $i | grep -i 'Mozilla' >> output.txt
  done

Every time I do this, I get a permission denied message like this:

./test1.sh: line 7: /opt/dir1/scripts/access_log.1.gz: Permission denied

even though I'm running this script as root and if I do these commands manually, I have no problem. Any ideas?

Thanks

War es hilfreich?

Lösung

Root seems not to have write permission in /opt/dir1/scripts/. What you do is not what you intend to do ;)

First, you should create the folder to make sure it exist: mkdir -p /tmp/apache

With cp $i /tmp/apache you copy a file into that directory.

gunzip $i does not extract the file you just copied, but the original file from the line above. I suggest a more easy way to do this using zgrep. zgrep is like grep but also works on gzipped files. Try this for a start:

#!/bin/bash
# create the folder (-p supreses warnings if the folder exists)
mkdir -p /tmp/apache
# create the output file (empty it, if it exists)
echo "" > /tmp/apache/output.txt
zgrep -i 'Mozilla' /opt/dir1/scripts/access_*.gz >> /tmp/apache/output.txt
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top