Nov 24, 2011

2 ways to generate random numbers in Bash shell

Have you ever tried to write Bash shell script and need a random number generator? Here provides 2 ways to achieve this.

1. Use the Built-in variable $RANDOM of Bash

$RANDOM is a built-in variable of Bash, it can be used to generate a random number. It's very easy to use, we can use echo to get its value. Here is the example:

$ echo $RANDOM
4371
$ echo $RANDOM
20269
$

Nov 17, 2011

3 advanced uses of Linux IO redirect

IO redirect is fundamental in Linux shell -- one basic thing you should  pay your attention when learning/using Linux.

3 Basic File descriptors:


File Descriptor Name Description
0 stdin standard input
1 stdout standard output
2 stderr standard error

Below I'll introduce 3 advanced usage, well they are also very common scenarios.

1. Redirect stderr to stdout

$ ls non-exist 2>&1


Note the "2>&1" sequence, please keep them there. This command will redirect stderr to stdout.

4 ways to check Linux distribution version

Do you know how to check Linux distribution version by a shell command?

In Linux, the most common command to show current version is "uname-r" to check the kernel version:

[jackieyeh@dhcppc15 ~]$ uname -r
2.6.18-6-686
[jackieyeh@dhcppc15 ~]$

What if you'd like to know the distribution version in stead of kernel version?