Today's Question:  What does your personal desk look like?        GIVE A SHOUT

/dev/null and /dev/tty in Linux

  sonic0002        2013-03-04 02:23:23       15,904        0    

In Linux, there are two special files /dev/null and /dev/tty. /dev/null will drop all the data written to it, i.e, when program writes data to this file, it means the program has completed the data write operation. But in  fact it does nothing, if you just want the status of a command but not the output of a command, this feature will be very useful. See below shell codes:

    /> vi test_dev_null.sh
    
    #!/bin/bash
    if grep hello TestFile > /dev/null
    then
        echo "Found"
    else
        echo "NOT Found"
    fi

After exiting vi, type below command:

    /> chmod +x test_dev_null.sh 
    /> cat > TestFile
    hello my friend
    CTRL + D                             #Save and exit
    /> ./test_dev_null.sh
    Found                                 #Output

Modify the above script:

    /> vi test_dev_null.sh
    
    #!/bin/bash
    if grep hello TestFile
    then
        echo "Found"
    else
        echo "NOT Found"
    fi

    /> ./test_dev_null.sh
    hello my friend                     
    Found

Let's see /dev/tty command now, /dev/tty stands for the controlling terminal (if any) for the current process. To find out which tty's are attached to which processes use the "ps -a" command at the shell prompt (command line). Look at the "tty" column. For the shell process you're in, /dev/tty is the terminal you are now using. Type "tty" at the shell prompt to see what it is (see manual pg. tty(1)). /dev/tty is something like a link to the actually terminal device name with some additional features for C-programmers: see the manual page tty(4).

    /> vi test_dev_tty.sh
    
    #!/bin/bash
    printf "Enter new password: "    #Prompt input
    stty -echo                              
    read password < /dev/tty        
    printf "\nEnter again: "          
    read password2 < /dev/tty      
    printf "\n"                             
    stty echo                              
    echo "Password = " $password
    echo "Password2 = " $password2
    echo "All Done"

 
    /> chmod +x test_dev_tty.sh
    /> ./test_dev_tty
    Enter new password:            
    Enter again:                         
    Password = hello
    Password2 = hello
    All Done

Reference : http://www.cnblogs.com/stephen-liu74/archive/2011/11/10/2240461.html

LINUX  /DEV/NULL  /DEV/TTY 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.