0

I am trying to create a bash script to search for lines that contain a certain keyword in .log files and sort them at the end. I am trying to save all the lines in an unsorted temporary document and sort them at the end by the following script but it keeps giving me the error of ambiguous redirect. I was wondering if anyone could give me any hint.

#! /bin/bash

MYTMPDIR='(mktemp -d)'

for i in `ls *.log`
do
grep Energy $i | cut -d' ' -f8| tail -1 >$MYTMPDIR/energy
paste energy nr  >>$MYTMPDIR/sort_energies
done
cat $MYTMPDIR/sort_energies| sort -n >sorted_energies

trap 'rm -rf -- "$MYTMPDIR"' EXIT
Zinnia
  • 1
  • One thing: Install the `trap` right after you've created the temporary directory - not _after_ the major part of the script is done. You want it to clean up even if your script ends in mysterious ways. Also, you may want to check for commands returning error codes. One way is to `set -e` - at least while developing the script. The `ls *.log` globber isn't good either btw. – Ted Lyngmo Aug 11 '21 at 20:20
  • `MYTMPDIR='(mktemp -d)'` should be `MYTMPDIR=$(mktemp -d)` – Barmar Aug 11 '21 at 20:24

0 Answers0