Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

UNIX cat command doubts

  1. Oct 18, 2010 #1
    UNIX “cat” command doubts!!

    I’n new to UNIX so please bear with me.

    I wanted to know what does the cat file1.txt << file2.txt command do?

    it doesn’t give an error, just goes accepts the keyboard as stdinput, and on ctrl+d, contents of file1 nor file2 changes.

    Also one more thing in the terminal window itself, the keyboard as the stdinput, when pressing Esc twice it prints all the hidden files(starts with a period), or atleast thats what I think. What does it do.
     
  2. jcsd
  3. Oct 18, 2010 #2
    Re: UNIX “cat” command doubts!!

    Checking the man page for csh or tcsh I find:
    Input/Output:
    << word Read the shell input up to a line which is identical to word. blah blah blah. The resultant text is placed in an anonymous temporary file which is given to the command as standard input.

    So I expect what you should be seeing when you type in your command is a ? showing it is waiting for input. Any lines you type in except file2.txt or ctrl-d will just give you another ? showing it is still reading input. When you finally type file2.txt or ctrl-d on a line then cat will take that as input, cat will I believe ignore this meaningless input, and it will then display file1.txt as you directed and finally terminate. Testing that here appears to work as documented.

    Study the documentation on your shell. Buy a book or two on it. You will be rewarded for that.
     
  4. Oct 18, 2010 #3
    Re: UNIX “cat” command doubts!!

    If memory serves, the "<<" is called a "here" document. It allows one to put what would be input to a command into a script, but I don't think it has any function directly on the command line. I always forget how to use here documents and have to experiment. As Bill said, use the "man" Luke...from my cygwin "bash" man page:

    Here Documents
    This type of redirection instructs the shell to read input
    from the current source until a line containing only word
    (with no trailing blanks) is seen. All of the lines read
    up to that point are then used as the standard input for a
    command.

    The format of here-documents is:

    <<[-]word
    here-document
    delimiter

    You might have been thinking of using a single "<" to redirect stdin from a file?
     
  5. Oct 18, 2010 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Re: UNIX “cat” command doubts!!

    It collects input from the keyboard (or shell if you have redirected stdin) until the you type file2.txt on a single line. All of your typed in input goes to the bit bucket because cat when provided with one or more file names does not read from stdin. cat will type the contents of file1.txt to the screen.


    That is probably not what you wanted, so what are you trying to do here?
     
  6. Oct 18, 2010 #5
    Re: UNIX “cat” command doubts!!

    That's a shell thing. Depending on the terminal program you use, and the shell that you're running, it may be the "tab" key or the "esc" key, or both. It's a feature of auto-complete.

    First off, what it does is handily tries to auto-complete the thing you're typing. So if you wanted to type some crazy command like:

    cd /home/rotter/someenormouslyhugefilenamethatIdrathernottype/

    You can instead type:

    cd /home/rotter/some(tab)

    And it ought to figure out that there's only ONE possible solution to what you're typing-- so it will auto-complete it for you. Or, it will auto-complete as much as it can figure out (which is often most of it).

    Now, sometimes, hitting (tab) or (esc) doesn't do the job, because it just-so-happens that there are MULTIPLE things that match what you're currently typing. For instance, if there were also a file called:

    /home/rotter/someotherfile

    Then typing (tab) or (esc) wouldn't help you to auto-complete, because there are two matches. So! If you hit the (tab) or (esc) a second time, it will tell you all the possible completions that there are. So it might go something like this:

    > cd /home/rotter/some(tab)(tab)
    someenormouslyhugefilenamethatIdrathernottype
    someotherfile
    > cd /home/rotter/some

    Then, if you hit "e", and then (tab) again, it would auto-complete the whole thing.

    There's also an extra caveat, again, depending on the shell you use. If the thing you're typing is at the BEGINNING of the command, it will recognize this as a command. So it will only auto-complete filenames that are *executable* (like directories). This means it will look in your current $PATH for any possible matches. If you're typing something in the middle, or near the end of your command, it will auto-complete any file types, regardless of the permissions, but it will NOT search the entirety of the $PATH-- just the current directory, or directory you started typing.

    DaveE
     
  7. Oct 18, 2010 #6
    Re: UNIX “cat” command doubts!!

    Your command means:
    - Type contents of file1.txt
    - Open the "here document". Accept user input until user input is "file2.txt" or CTRL-D and type the contents to the screen

    The correct "usage for this" may be like cat << END > file2.txt
    You will be prompted for input and until you type END on a single line or CTRL-D it will take your input. Then contents of file 1 will be inserted into file2.txt.

    You maybe wanted to cat file1.txt >> file2.txt
    - Type contents of file1.txt
    - But redirect stdout to (>>) append into file2.txt

    You maybe wanted to cat file1.txt > file2.txt
    - Type contents of file1.txt
    - But redirect stdout to (>) replace into file2.txt

    Often you may see redirection like this (a useful example):
    foo > /dev/null 2>&1
    Run program foo and redirect standard error to standard output and redrect resulting standard output to /dev/null (black hole).
     
  8. Oct 19, 2010 #7
    Re: UNIX “cat” command doubts!!

    Thank you for your replies, it has been really helpful.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook