Main       Comic       Forums       Anime       Tools       Fun       
Categories
Recent Articles
Recent Posts
Buy DSG Items
Like the new DeadGod design?
Do you hate it?
Click here to let us know!
Recent Posts       Popular Articles       Popular Threads       [ View All Recent Posts ]
1. Not so unexpected by DranoK -- 1H 4M ago.
2. Not so unexpected by 2DruNk2FraG -- 1H 37M ago.
3. New Server:  Rules Discussion by DranoK -- 13H 21M ago.
4. Unknown by noggin -- 13H 33M ago.
 
[ Login | Register ]
 
Viewing latest 20 tagged BEGINNER.
Jun
6
Wednesday, June 6th, 2007 (15894 Views)
Beginner
DranoK
Prerequisites:
o GNU tail
o Familiarity with the bash shell
o Familiarity with the tail command
o Beginner's knowledge of what a file descriptor is

Running tail -f /path/to/somefile.log is one of the first commands a beginner learns. Aside from use on the command line, tail can be used in scripts as well.

Most log files rotate at some point or another (that is, they're renamed), and a new file starts being logged to. This is a problem since tail will follow the file even through a rename. This is because by default tail follows the descriptor, which doesn't actually change when the file gets moved.

Say you're tailing /var/log/messages. This file gets rotated to /var/log/messages.1 and a new /var/log/messages is created. By default your tail will still be following the descriptor for /var/log/messages.1, which probably isn't what you want.

To change this you need to make tail follow the name of the file rather than the file's descriptor. This can be done with:

Listing 1: Tail by name
tail --follow=name /var/log/messages

The normal "tail -f" is equivalent to:

Listing 2: Tail by descriptor (aka, tail -f)
tail --follow=descriptor /var/log/messages

This is a good start, but there's one additional problem. Normally, if a file becomes inaccessible for even a short period of time tail will quit. This is a problem, since there will almost certainly be a delay between when the existing /var/log/messages is moved to /var/log/messages.1 and the new /var/log/messages is created.

To overcome this problem we can use the "--retry" option:

Listing 3: Tail retry option
tail --retry --follow=name /var/log/messages

This is a bit bulky, so luckily GNU tail gives us a single flag to use instead, "-F". The "-F" flag is an alias for "--retry --follow=name":

Listing 4: Final, simplified command to tail by name
tail -F /var/log/messages

Easy as that ;)

Listing 5: Relevant section from the tail man page
       --retry
              keep trying to open a file even if it is inaccessible when  tail
              starts  or  if it becomes inaccessible later -- useful only with
              -f

       -f, --follow[={name|descriptor}]
              output appended data as the file grows; -f, --follow, and --fol-
              low=descriptor are equivalent

       -F     same as --follow=name --retry


Permalink - Forum Thread - Reply to this - Subscribe!Social Bookmark Button



Jan
1
Monday, January 1st, 2007 (16756 Views)
Beginner
DranoK
Bash has several built-in special variables. Although most are used only for scripting, there are several that are useful at the command line.

The two I will address in this post are !! and !$.

Repeat entire last line: !!

One of the useful parts of bash is its history system. You can see a history of your commands by typing history at the command prompt. The history command is considered a built-in command because it is not a binary that exists on the filesystem such as ls or uptime. The cd command is another example of a bash built-in.

Built-in bash commands will be covered in more detail in the future, as will the history command. For now, however, know that you can repeat history commands by typing "!" followed by the history ID.

Take the following example:

Listing 1:
dranok@Neptune:~> history
    1  which uptime
    2  cd /tmp
    3  ls -l
    4  uptime

The which command will tell you the full path of a program, assuming it is in a path specified by your $PATH environment variable. If I wanted to run the which uptime command again, instead of typing it out I could instead type !1:

Listing 2:
dranok@Neptune:~> !1
which uptime
/usr/bin/uptime

!1 gets expanded to the actual command which uptime. Note !1 doesn't get saved in your history--if you type history again you'll see the expanded which uptime command. Likewise if you press the up key you'll see the expanded version there as well.

Also note that when you use special bash variables the expanded command is printed on the subsequent line. This is helpful in figuring out exactly what the bash shell actually ran.

So how does this relate to !!? !! is the bash built-in for the last command in your history file--the last command you ran.

Listing 3:
dranok@Neptune:~> cat /tmp/happy
I'm happy!
dranok@Neptune:~> !!
cat /tmp/happy
I'm happy!

This is very useful in conjunction with the sudo command, among other things. For example you might run a command as a non-privileged only to discover you need root access. Running sudo !! is a fast way to achieve this.

Listing 4:
dranok@Neptune:~> cat /tmp/rootonly.txt 
cat: /tmp/rootonly.txt: Permission denied
dranok@Neptune:~> sudo !!
sudo cat /tmp/rootonly.txt 
This file can only be read by root.


Repeat last argument: !$

!$ is similar to !!, however instead of expanding to the entire last command it expands to the last argument of the last command. This is best illustrated by example.

Listing 5:
dranok@Neptune:~> cd /tmp/testdir
-bash: cd: /tmp/testdir: No such file or directory
dranok@Neptune:~> mkdir !$
mkdir /tmp/testdir

In this case we tried to cd into a directory which did not exist. Since the last argument of the preceding command was /tmp/testdir, running mkdir !$ is the equivalent of mkdir /tmp/testdir

Bash contains many other special variables like !! and !$, however these are two of the most ubiquitous and useful.
Permalink - Forum Thread - Reply to this - Subscribe!Social Bookmark Button




Older Articles
 
 
DeadGod.Net - Cute Evil Atheists
Encouraging critical thought and general enjoyment since 2001.
Want to join our community? Have suggestions or comments?
Contact Us or Visit our forums.