2

I created a script by using the command line initially:

grep -c "Author" reviews_folder/* | cut -d \/ -f 2 | sort -nt':' -k2 | sed 's/.dat:/ /g'

It worked, however I then needed to make it a shell script which allowed the user to enter a directory for it to search, so I changed it to:

grep -c "Author" $1/* | cut -d \/ -f 2 | sort -nt':' -k2 | sed 's/.dat:/ /g'

After this was complete, rather than the script working, it just printed lots of blank lines.

Ravexina
  • 57,256

1 Answers1

1

I guess you are running your script like: script.sh dir/ or bash script.sh dir/.

The way you pass the directory is causing the problem.

In your script you got a grep command:

grep -c "Author" $1/*

If you pass the directory with tailing slash, the grep command would be run as:

grep -c "Author" dir//*

Example:

grep -c "Author" dir//somefile.dat dir//someother-file.dat

And its result would be similar to:

dir//somefile.dat:5

And your cut command would only return an empty string and pass it to sort:

dir//somefile.dat:5   # < You are asking for second part which is empty

So either run your script like: script.sh dir or change it to something like this:

#!/bin/bash

DIR=${1%/}
grep -c "Author" ${DIR}/* | cut -d \/ -f 2 | sort -nt':' -k2 | sed 's/.dat:/ /g'
Ravexina
  • 57,256