I have a command lline script that works perfectly.

cat <some_filename> | awk '$9 == 200' | awk '$10 == 10623720' | awk -F\" '{print $6}' | sort | uniq -c | sort -r

I want to put this in a shell script so that we do this for multiple files(all read from a file list) and write each output to different files or folders. The shell script I am trying is as follows.

#!/bin/bash

FILENAME=$1
OUTDIR1=outdir1
OUTDIR2=outdir2
OUTDIR3=outdir3
count=0
cat $FILENAME | while read LINE
do
       let count++
       echo "$count $LINE" >> res.txt
       # Full and download of data
       cmd="cat $LINE | awk '\$9 == 200' | awk '\$10 == 10623720' | awk -F"\" '{print \$6}' | sort | uniq -c | sort -r >> $OUTDIR1/$LINE" 
       eval $cmd
done

I am not sure what exactly I am doing wrong. This is the error I get.

trends.sh: line 16: unexpected EOF while looking for matching `"'
trends.sh: line 19: syntax error: unexpected end of file

Let me know what I am doing wrong.

hi,

first, piping a command to another instance of itself is not good.

the error message comes from the separator declaration in the nth awk ;), and because you're using a variable to store the pipeline (which is silly).
either, never use eval inside script unless you're facing difficulties that shell alone can't handle, and you know exactly the output.
and cat is unnecessary.

also, uppercase variable names, by convention, are for environmental variables.

Edited 4 Years Ago by Watael

The short answer to your question is that you define cmd like this:
cmd="something "\" something"
You can't put quotes inside of quotes like that. Use single quotes.
Also the backslash is a special character. You can probably fix that by doubling it '\\', but since you're passing it through eval, you probably need four, '\\\\'. I say probably because complex backslash use in shell code is often non-obvious.

Also, I notice the LINE content is being used as a filename. Maybe that's what you intend, but it looks like those filenames will contain at least nine spaces. Those are awful filenames to work with, and you probably need to quote it so the shell uses the whole name.

You can quote it this way. The single quotes don't inhibt variable expansion because they're regular characters when they're inside the double quotes:
"...sort -r >> '$OUTDIR1/$LINE'"

Edited 4 Years Ago by KenJackson

This article has been dead for over six months. Start a new discussion instead.