2

I have this script I'm studying and I would like to know what is cat doing in this section.

if cat downloaded.txt | grep "$count" >/dev/null
    then
        echo "File already downloaded!"
    else
        echo $count >> downloaded.txt
        cat $count | egrep -o "http://server.*(png|jpg|gif)" | nice -n -20 wget --no-dns-cache -4 --tries=2 --keep-session-cookies --load-cookies=cookies.txt --referer=http://server.com/wallpaper/$number -i -
        rm $count   
fi
dominique120
  • 1,170
  • 4
  • 18
  • 31

3 Answers3

5

Like most cats, this is a useless cat.

Instead of:

if cat downloaded.txt | grep "$count" >/dev/null

It could have been written:

if grep "$count" download.txt > /dev/null

In fact, because you've eliminated the pipe, you've eliminated issues with which exit value the if statement is dealing with.

Most Unix cats you'll see are of the useless variety. However, people like cats almost as much as they like using a grep/awk pipe, or using multiple grep or sed commands instead of combining everything into a single command.

The cat command stands for concatenate which is to allow you to concatenate files. It was created to be used with the split command which splits a file into multiple parts. This was useful if you had a really big file, but had to put it on floppy drives that couldn't hold the entire file:

split -b140K -a4 my_really_big_file.txt my_smaller_files.txt.

Now, I'll have my_smaller_files.txt.aaaa and my_smaller_files.txt.aaab and so forth. I can put them on the floppies, and then on the other computer. (Heck, I might go all high tech and use UUCP on you!).

Once I get my files on the other computer, I can do this:

cat my_smaller_files.txt.* > my_really_big_file.txt

And, that's one cat that isn't useless.

David W.
  • 105,218
  • 39
  • 216
  • 337
0

cat prints out the contents of the file with the given name (to the standard output or to wherever it's redirected). The result can be piped to some other command (in this case, (e)grep to find something in the file contents). Concretely, here it tries to download the images referenced in that file, then adds the name of the file to downloaded.txt in order to not process it again (this is what the check in if was about).

http://www.linfo.org/cat.html

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
0

"cat" is a unix command that reads the contents of one or more files sequentially and by default prints out the information the user console ("stdout" or standard output).

In this case cat is being used to read the contents of the file "downloaded.txt", the pipe "|" is redirecting/feeding its output to the grep program, which is searching for whatever is in the variable "$count" to be matched with.

leofelipe
  • 33
  • 1
  • 1
  • 7