Dec 2, 2011

3 Advanced usages of Linux ‘date’ command

In Linux shell, ‘date’ is a basic command to show current date, like:
$ date
Sat Jan  8 15:34:16 CST 2011
$

From the result, it shows more than just date, and also time and time zone. It’s different from what DOS does, which use two command to get/set date and time. However the ‘date’ command can do much more than this, let’s first check the usage.
Standard version:
$ date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
...[skipped]

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

Linux shell 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

Nov 16, 2011

How 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?

Since there are some many "flavors" in Linux, the Linux version is distinguished not only by kernel version, but also distribution versions, like Fedora, Debian, Ubuntu, etc...

Here is the summary of the ways to get the distribution version. Although different distributions may have slight difference, luckily you can get the answers with couple trials.