Operating System - Linux
1830238 Members
3419 Online
109999 Solutions
New Discussion

null and/or random devices ?

 
SOLVED
Go to solution
Maaz
Valued Contributor

null and/or random devices ?

Dear Gurus
what is null devices ? and its use ? any example plz
what is random devices ? and its use ? any example plz

Regards
Maaz
6 REPLIES 6
Ivan Ferreira
Honored Contributor
Solution

Re: null and/or random devices ?

Null devices are normally used to redirect the output of commands. Null devices ignore the data sent to it, like a black hole. It's used when you want to ignore the output of a command, example:

ls -la > ls.out 2> /dev/null

In this example, you redirect the errors to /dev/null, so it's ignored.

Another use is to test the performance of somethig, like this:

dd if=/dev/hda1 of=/dev/null bs=1024 count=10000

This example will read the data from the disk and write to nothing, so, you can get a more accurate estimation of the read rate.

Random devices are normally used by cryptography, like gnupg, to generate random characters. When you create a certificate (make testcert), or a public key pair (ssh -keygen), you are using a random device, normally /dev/random.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Geoff Wild
Honored Contributor

Re: null and/or random devices ?

There is only one null - /dev/null

Example - cron:

# Run the disckcheck script
30 6 1 * * /usr/local/bin/diskcheck >/dev/null 2>&1

That will re-direct STOUT and errors to /dev/null - else it would go to an email...

Another in a script - checking the return signal of a command:

ps -aef | grep -v grep | grep mbtsk > /dev/null
if [ $? -ne 0 ]
then

# No mbtsk, so no Oasis.
exit 0
fi


Rgds...Geoff



Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Maaz
Valued Contributor

Re: null and/or random devices ?

nice help Dear Ivan Ferreira, and Geoff Wild.

Ivan Ferreira u wrote:
ls -la > ls.out 2> /dev/null

in the example above the output is redirecting to a file "ls.out" and as well as to /dev/null ... m i right ?

may i ask why u use "2" i.e instead of "...> /dev/null" why u wrote "...2> /dev/null"

and should i have to create /dev/null via mknod command ?

Regards
Maaz
Alexander Chuzhoy
Honored Contributor

Re: null and/or random devices ?

ls -la > ls.out 2> /dev/null
It means:
redirect the output to a file named ls.out
redirect the errors (STDERR) to /dev/null

basically: you don't wish to see errors that might occur during the "operation".
Uwe Zessin
Honored Contributor

Re: null and/or random devices ?

There are 3 predefined streams:
0 = stdin (standard input)
1 = stdout (standard output)
2 = stderr (standard error)

">" means "stdout" redirection, while "2>" means "stderr" redirection.

">&" writes the output of one stream into another. Taking Geoff's example:
... >/dev/null 2>&1

- redirect "stderr" (2) to "stdout" (1)
- redirect "stdout" to "/dev/null"
.
Maaz
Valued Contributor

Re: null and/or random devices ?

Thankx Dear Uwe Zessin and All

Rgrds
Maaz