/dev/null and /dev/tty in Linux
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
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)
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
:: Users edited this page
No users edited this page yet.
:: Recent articles
- Content based HTTP Cache
- Select top 3 values from each group in a table with SQL
- Meta tag in HTML header
- Text editor vs IDE
- Sorry, I don't want to download your fucking app
- Display GIF animation while submitting the web form
- PHP to get access token for Sina Weibo app
- Tencent released Q1 earning report of 2013
- How to be jQuery-free?
- Programmer's Mother's Day
- more>>
:: Most read
- TIOBE : C overtakes Java as the No.1 programming language
- Which programming language should I learn first?
- Sony is to release PlayStation4 in 2015
- Disposable Email address
- 5 Free Open Source Chat Applications For Developers
- Never ever touch a programmer
- Hacking Vs. Programming
- TIOBE : Where is that next big programming language?
- Multitasking vs multiprogramming
- Google is developing advanced programming technology to simplify Web application development
- more>>
:: Most commented
- Hacking Vs. Programming
- Sony is to release PlayStation4 in 2015
- Which programming language should I learn first?
- Error handling style in C
- 10 controversial programming opinions?
- C vs Java Complete Comparison
- Google+ is sick
- Disposable Email address
- Unix Philosophy
- Should we use Abstract class or Interface?
- more>>
:: Find us
