grep never exiting when it should be searching through a small tempfile

204 Views Asked by At

I have a problem with mktemp and grep.

OUT=$(mktemp /tmp/output.XXXXXXXXXX) || { echo "Failed to create temp file"; exit 1; }
awk -F: '{print $7}' /etc/passwd >> $OUT
grep -c $1 $OUT

In grep line, code not exits, not prints value of grep Please, help me to solve that problem.

1

There are 1 best solutions below

2
On

BlackPearl above probably is correct - $1 likely is empty during your program execution. As a result, the grep command looks like this: grep -c $OUT which tells grep to look for $OUT in the stdin. stdin is the keyboard (I suspect) so grep will wait forever (well, until you press Ctrl-D or Ctrl-C).

To solve your problem, specify a parameter when you execute your script.

You can also avoid the problem entirely by counting all of the unique values in your passwd file like this:

OUT=$(mktemp /tmp/output.XXXXXXXXXX) || { echo "Failed to create temp file"; exit 1; }
awk -F: '{print $7}' /etc/passwd >> "$OUT"
sort "$OUT" | uniq -c  # count each unique value in passwd file column 7