/dev/null and main argument and redirection

ls -la /dev/tty shows the output:

crw-rw-rw- 1 root tty 5, 0 Dec 14 22:21 /dev/tty


The 'c' means it's a character device. tty is a special file representing the 'controlling terminal' for the current process.

Character Devices

Unix supports 'device files', which aren't really files at all, but file-like access points to hardware devices. A 'character' device is one which is interfaced byte-by-byte (as opposed to buffered IO).

TTY

/dev/tty is a special file, representing the terminal for the current process. So, when you echo 1 > /dev/tty, your message ('1') will appear on your screen. Likewise, when you cat /dev/tty, your subsequent input gets duplicated (until you press Ctrl-C).


-----------------------------------------
#include<stdio.h>
int main(int argc,char *argv[]){
    printf("%d %s\n",argc,argv[0]);
    int i;
    for(i=0;i<argc;i++)
        printf("%s ",argv[i]);
}

$ ./test> test.exe myarg1 myarg2
$ cat test.exe
3 ./test
./test myarg1 myarg2

嘿嘿~搞定

---------------------------------------------

$ myprogram 2>errorsfile

This above command would execute a program named ' myprogram ' and whatever errors are generated while executing that program would all be added to a file named ' errorsfile ' rather than be displayed on the screen. Remember that 2 is the error output file descriptor. Thus ' 2> ' means redirect the error output.

$ find / -name s*.jpg 2>/dev/null

What's /dev/null ????? That something like a black hole. Whatever is sent to the ' /dev/null ' never returns. Neither does one know where it goes. It simple disappears. Isn't that fantastic !! So remember.. whenever you want to remove something.. something that you don't want ...you could just send it to /dev/null

---------------------------
kludge:

Pronounced klooj. A derogatory term that refers to a poor design. Like hacks, kludges use nonstandard techniques. But, whereas a hack can connote a clever solution to a problem, a kludge always implies that the solution is inelegant.

---------------------------------

http://www.grymoire.com/Unix/Quote.html#uh-3
./a.out > outfile 2>&1
first sets standard output to outfile and then dups standard output onto descriptor 2 (standard error).

if you enter the following, where digit1 and
  digit2 are valid file descriptors, you can direct the output that would
  normally be associated with file descriptor digit1 to the file associated
  with digit2:

       digit1>&digit2

The default value for digit1 and digit2 is 1 (standard output).  If, at
  execution time, no file is associated with digit2, then the redirection is
  void. The most common use of this mechanism is to direct standard error
  output to the same file as standard output, as follows:

       command 2>&1

猜你喜欢

转载自wwwjjq.iteye.com/blog/1566634