Technical Articles => Operating System =>  Linux/Unix

/dev/null and /dev/tty in Linux

Source : sonic0002    Date : 2013-03-04 02:23:23  

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

Save as PDF Mark as read Mark as important
By clicking the "Mark as read" button, this article will be marked as read. It will be removed from the homepage's latest news and the article list on the "Technical article" page in following visits and it will be put to your read list which you can find in "Amin->Article read list". There you can unmark the read articles.
By clicking the "Mark as important" button, this article will be put to your important article list which you can find in "Amin->Article important list". Later when you want reread this article, it's easier for you to find it by checking the "Article important list".

Tags:Linux,/dev/null,/dev/tty   Read(390) Comment(0)

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Digg  Share on Tumblr    Delicious 

 Previous : Change password of postgres account in Postgres
 Next : iWatch is coming to us before 2014

  ::Related Articles

  ::Comment List

No comment for this article.

  ::Comment

Nickname  
Email 
Comment

:: Users edited this page

No users edited this page yet.

:: Recent articles

:: Most read

:: Most commented

:: Find us

Back to top