Linux User Group of Gaia's Tip, Tricks, How-tos, and FAQs
Editing a Bash Command
Sometimes when you try to execute a long command, it scrolls
beyond the screen. Then if you want to modify the command and
re-execute it, there is an easy way for it. Just type "fc"
which will load the command in your default editor; in my case vi.
Now you can modify the command in the editor and exit the editor, and
your modified command is executed automatically. For example try
typing the following command in the bash shell and type "fc".
"fc" will bring the last command typed into an editor,
"vi" if that's the default editor. Of course you can
specify a different editor by using the -e switch as follows:
For the last 10 commands it will be:
To seach for a command, type "CTRL+r" at the shell prompt for starting a search as you type prompt. Once you found your command, press enter to execute it. If you want to transpose two characters say you typed 'sl' instead of 'ls'. Then move the cursor between 'sl' and type "CTRL+t".
Bash Completion
One thing that really makes working in the command line in Linux a pleasure is the various in-built shortcuts and name completion features in Bash - the default shell in Linux. But one grouse I always had was it was really difficult to remember all the options that each command had. For example, 'find' came with numerous options which I found difficult to memorize and had to resort to reading the man page each time I had to use the command. Now you can enhance the bash shell to give you the added functionality of listing the options that can be used with a command. For that you should download and install an add-on package called bash-completion. I use Fedora Core 2 but if you are using the latest Linux distribution, it might be installed by default on your machine. In Debian based Linux distributions, you may install it using the following command :
After installing the bash-completion package, fire up a terminal
and type:
Special Shell Variables
These are the special shell variables which can be used in shell
scripts in Linux.
$# The number of arguments.
$@
All arguments, as separate words.
$* All
arguments, as one word.
$$ ID of the current
process.
$? Exit status of the last
command.
$0,$1,..$9,${10},${11}…${N} Positional
parameters. After “9″ you must use the ${k} syntax.
For
example, if you create a shell script as follows :
#!/bin/sh
#File: test.sh echo $$
Now if you execute it, then you will find
that it will print the program 'test.sh' process ID.
Bash Shell Shortcuts
Bash, which is the default shell in Linux contains a whole lot of
key bindings which makes it really easy to use . The most commonly
used shortcuts are listed below :
____________CTRL Key
Bound_____________
Ctrl + a - Jump to the start
of the line
Ctrl + b - Move back a char
Ctrl
+ c - Terminate the command
Ctrl + d -
Delete from under the cursor
Ctrl + e - Jump to
the end of the line
Ctrl + f - Move forward a
char
Ctrl + k - Delete to EOL
Ctrl + l
- Clear the screen
Ctrl + r - Search the history
backwards
Ctrl + R - Search the history backwards
with multi occurrence
Ctrl + u - Delete backward
from cursor
Ctrl + xx - Move between EOL and
current cursor position
Ctrl + x @ - Show
possible hostname completions
Ctrl + z - Suspend/
Stop the command
____________ALT Key Bound___________
Alt
+ < - Move to the first line in the history
Alt
+ > - Move to the last line in the history
Alt
+ ? - Show current completion list
Alt + *
- Insert all possible completions
Alt + / -
Attempt to complete filename
Alt + . - Yank last
argument to previous command
Alt + b - Move
backward
Alt + c - Capitalize the word
Alt
+ d - Delete word
Alt + f - Move
forward
Alt + l - Make word lowercase
Alt
+ n - Search the history forwards non-incremental
Alt
+ p - Search the history backwards non-incremental
Alt
+ r - Recall command
Alt + t - Move
words around
Alt + u - Make word uppercase
Alt
+ back-space - Delete backward from
cursor
----------------More Special
Keybindings-------------------
Here "2T" means Press
TAB twice
$ 2T - All available
commands(common)
$ (string)2T - All available
commands starting with (string)
$ /2T - Entire
directory structure including Hidden one
$ 2T -
Only Sub Dirs inside including Hidden one
$ *2T -
Only Sub Dirs inside without Hidden one
$ ~2T -
All Present Users on system from "/etc/passwd"
$ $2T
- All Sys variables
$ @2T - Entries from
"/etc/hosts"
$ =2T - Output like ls or
dir
Configuring Xterm in Linux
xterm is a terminal which runs in X. In linux when you open xterm, you get a small window with a small - hard to read - font by default. Compared to the ordinary xterm, the gnome-terminal and konsole come loaded with lots of features and are good to view. So why would anybody use an xterm over the other two? The answer lies in its low memory foot print. While konsole takes a whooping 8MB and gnome-terminal over 3MB of memory, you can run xterm under 1MB which makes it blazingly fast even when your computer has only 64MB of RAM. Below I describe ways in which you could configure xterm to make it look good and easier to read with larger fonts. If you look in the man page of xterm, you will find that you can configure xterm's colour, font, font size ,the position in which to load the window w.r.t your screen and much more.
The first thing you to do is decide on your choice of font and its
size for your xterm. This can be achieved by running xfontsel as
follows:
...if you have selected a fixed, medium 18pt regular font with iso8859 support. These values will be used while starting xterm. Also to specify the size and position of the xterm window, you can use the -geometry option.
Now to test your settings, execute xterm. I have executed xterm
with the following settings:
#File: ~.bashrc alias xterm='xterm -font -*-fixed-medium-r-*-*-18-*-*-*-*-*-iso8859-* -geometry 70x24'
The next time you type xterm, your system will execute the program with the options in your .bashrc file in your home directory.
There is another more powerful way to achieve the same. That is to pass the parameters to your X server so that it will know how to display your xterm when you execute it. This is as follows: Create a '.Xresources' file in your home directory and enter the values that you want to set. My .Xresources file is as follows:
As you can see in my .Xresources file above, I have set my xterm
to start with a fixed font, white background and black foreground and
with no scrollbar. I have also set other settings like the number of
lines to save in history and so on. Note: You could do the following
instead of the above method to get the same effects. Create a file
say .x_settings - the file can have any name - and copy the contents
of .Xresources to it. Now each time you start an X session, type the
following command in the console :
$ xrdb -merge .x_settings
Another method to increase the font size of the xterm is by pressing [Ctrl] key and the right mouse button at the same time while you have focus in xterm window. Then a pop-up menu will come up which can be used to set the font size to your taste. By pressing [Ctrl] key and the middle mouse button, you get a pop-up menu which helps you set/unset a lot of other features of your xterm window like enabling/disabling the scroll bars and so on. Here I have explained some of the methods by which you can configure xterm to look good. Ofcourse, GNU/Linux is all about the freedom of choice. So xterm is not the only light weight x terminal around. Two other x terminals which are light weight and having even more features like support for transparency that come to mind are eterm and aterm. But the advantage of xterm is that you can be sure of having it in all linux distributions by default where as if you want to use aterm or eterm, you may have to download them from their site and install it in your machine.
A Short Guide to Shell Scripting
First let me clarify that this is not going to be a detailed study of shell scripting, but as the name of the post indicates, it will be a quick reference to the syntax used in scripting for the bash shell. So if you are expecting the former, then you should buy yourself a good book on shell scripting . ;-) So let's move on to the guide. Start your stop watch now.
-- Start of The Short Guide to Bash Scripting --
Common environment variables
PATH - Sets the
search path for any executable command. Similar to the PATH variable
in MSDOS.
HOME - Home directory of the user.
MAIL
- Contains the path to the location where mail addressed to the user
is stored.
IFS - Contains a string of characters
which are used as word seperators in the command line. The string
normally consists of the space, tab and the newline characters. To
see them you will have to do an octal dump as follows:
$ echo
$IFS | od -bc
PS1 and PS2 -
Primary and secondary prompts in bash. PS1 is set to $ by default and
PS2 is set to '>' . To see the secondary prompt, just run the
command :
$ ls |
... and press enter.
USER - User login name.
TERM
- indicates the terminal type being used. This should be set
correctly for editors like vi to work correctly.
SHELL
- Determines the type of shell that the user sees on logging
in.
Note: To see what are the values held by the above environment
variables, just do an echo of the name of the variable preceeded with
a $. For example, if I do the following:
$ echo $USER me
... I
get the value stored in the environment variable USER.
Some bash shell scripting rules
1) The first line in your script must be
#!/bin/bash
...
that is a # (Hash) followed by a ! (ban) followed by the path of the
shell. This line lets the environment know the file is a shell script
and the location of the shell.
2) Before executing your script,
you should make the script executable. You do it by using the
following command:
$ chmod ugo+x your_shell_script.sh
3) The
name of your shell script must end with a .sh . This lets the user
know that the file is a shell script. This is not compulsary but is
the norm.
Conditional statements
The 'if' Statement - evaluates a condition which accompanies its command line.
syntax:
if condition also permits multiway branching. That is you can evaluate more conditions if the previous condition fails.Example :if's companion - test
test is an internal feature of the shell. test
evaluates the condition placed on its right, and returns either a
true or false exit status. For this purpose, test uses certain
operators to evaluate the condition. They are as follows:
Relational operators
-eq Equal to
-lt Less
than
-gt Greater than
-ge
Greater than or equal to
-lt Less than
-le
Less than or equal to
File related tests
-f file True if file exists
and is a regular file
-r file True if file exists
and is readable
-w file True if file exists and
is writable
-x file True if file exists and is
executable
-d file True if file exists and is a
directory
-s file True if file exists and has a
size greater than zero.
String tests
-n str True if string str is not
a null string
-z str True if string str is a null
string
str1 == str2 True if both strings are
equal
str1 != str2 True if both strings are
unequal
str True if string str is assigned a
value and is not null.
Test also permits the checking of more than one expression in the
same line.
-a Performs the AND function
-o
Performs the OR function
Example:
Things to remember while using test
If you are using square brackets [] instead of test, then care should be taken to insert a space after the [ and before the ]. Note: test is confined to integer values only. Decimal values are simply truncated. Do not use wildcards for testing string equality - they are expanded by the shell to match the files in your directory rather than the string.
Case statement
Case statement is the second conditional offered by the shell. Syntax:
The keywords here are in, case and esac. The ';;' is used as option terminators. The construct also uses ')' to delimit the pattern from the action. Example:Note: The last case option need not have ;; but you can provide them if you want.Here is another example:Case can also match more than one pattern with each option.You can also use shell wild-cards for matching patterns. In the above case, if you enter YeS, YES,yEs and any of its combinations, it will be matched.
This brings us to the end of conditional statements.
Looping Statements
while loop
Syntax :
Example:The above code implements a infinite loop. You could also write 'while true' instead of 'while :' . Here I would like to introduce two keywords with respect to looping conditionals. They are break and continue.break - This keyword causes control to break out of the loop.
continue - This keyword will suspend the execution of all statements following it and switches control to the top of the loop for the next iteration.
until loop
Until complements while construct in the sense that the loop body here is executed repeatedly as long as the condition remains false. Syntax:
Example:The above code is executed repeatedly until the file myfile can be read.for loop
Syntax :
Example:Here the list contains 5 numbers 1 to 5. Here is another example:Suppose you have a directory full of java files and you want to compile those. You can write a script like this:Note: You can use wildcard expressions in your scripts.A few special symbols and their meanings w.r.t shell scripts
$* - This denotes all the parameters passed to
the script at the time of its execution. Which includes $1, $2 and so
on.
$0 - Name of the shell script being
executed.
$# - Number of arguments specified in
the command line.
$? - Exit status of the last
command.
The above symbols are known as positional parameters. Let
me explain the positional parameters with the aid of an example.
Suppose I have a shell script called my_script.sh . Now I execute
this script in the command line as follows :
$ ./my_script.sh
linux is a robust OS
... as you can see above, I have passed 5
parameters to the script. In this scenario, the values of the
positional parameters are as follows:
$* - will
contain the values 'linux','is','a','robust','OS'.
$0
- will contain the value my_script.sh - the name of the script being
executed.
$# - contains the value 5 - the total
number of parameters.
$$ - contains the process
ID of the current shell. You can use this parameter while giving
unique names to any temporary files that you create at the time of
execution of the shell.
$1 - contains the
value 'linux'
$2 - contains the value 'is'
...
and so on.
The set and shift statements
set - Lets you
associate values with these positional parameters .
For example,
try this:
Example :To see the process Id of the current shell, try this:Validate that it is the same value by executing the following command:
read statement
Make your shell script interactive. read will let the user enter values while the script is being executed. When a program encounters the read statement, the program pauses at that point. Input entered through the keyboard is read into the variables following read, and the program execution continues. Eg:
Exit status of the last command
Every command returns a value after execution. This value is called the exit status or return value of the command. A command is said to be true if it executes successfully, and false if it fails. This can be checked in the script using the $? positional parameter.
Here I have given a concise introduction to the art of bash shell scripting in Linux. But there is more to shell scripting than what I have covered. For one, there are different kinds of shells, bash shell being only one of them. And each shell has a small variation in its syntax. Like the C shell for example, which uses a syntax close to the C language for scripting. But what I have covered above applys to all the shells.
Make your shell scripts user-friendly using Dialog
If you have installed Linux using the text installer, then you will find a neat professional looking install process. You can rest assured that no extreme programming has gone into creating the text installer. In fact, it has been created using a utility called dialog. Dialog is a utility installed by default on all major Linux distributions. It is used to create professional looking dialog boxes from within shell scripts. Some of the dialogs supported are Input boxes, Menu, checklist boxes, yes/no boxes, message boxes, radiolist boxes and text boxes. Creating a dialog is very easy. Here I will explain how to create dialog boxes of different types.
Input boxes : These allows the user to enter a string. After the user enters the data, it is written to standard error . You may also redirect the output to a file.
As you can see, the options are self explanatory. The last two options 8 and 40 are the height and width of the box respectively.Textbox : This is a box which takes a file as the parameter and shows the file in a scrollable box.
... it shows the file myfile.txt in a textbox.Checklist : The user is presented with a list of choices and can toggle each one on or off individually using the space bar.
... here, 10 is the height of the box, 40 - width, 3 is the number of choices, and the rest are the choices numbered 1,2 and 3.Radiolist: It displays a list containing radio buttons. And the user can only choose one option from the set of options.
10 and 40 are the height and width respectively. 4 denotes the number of items in the list.Infobox: This is useful for displaying a message while an operation is going on. For example, see the code below:
Dialog is usually used inside a script which gives the script a degree of user friendliness. There is another package called Xdialog which gives the same features for scripts executed in X Windows. Xdialog utility also has additional functionality not found in the dialog utility. To know more about the dialog utility check the man page of dialog.
-- End of The Short Guide to Bash Scripting --
Enhanching System prompt $ps1
If you spend a lot of time remotely logging into several machines,
or just have many terminals open using the $PS1 variable can aid in
keeping track of the whole mess. Even prevent you from making silly
mistakes as root. For instance if you
put:
PS1="[\[\033[1;31m\]\u\[\033[0m\]@\h \W]\\$ "
at
the bottom of your /root/.bashrc file. Every time you are logged in
as root, the terminal will have a red color to it.
Suppose you have seperate accounts like a developer account, a
regular account and remote machines account, then you can use
separate colors for each of your accounts like, for developer account
(magenta), regular account (blue) and remote machines (green) so you
know at a glance which is which without reading every line of output
looking for that regular text that blends in. The actual color code
is "1;31" inside the PS1 variable. The 1 says make it bold,
and the 31 says the color (red). Other colors are:
1;30 Black
1;31 Red
1;32
Green
1;33 Yellow
1;34 Blue
1;35 Magenta
1;36 Cyan
1;37 White
This
is just a subset of PS1 variable tricks for more information on
enhancing the system prompt, read the article at www-106ibm.com.
Screen A window manager for the console
Screen is a full-screen window manager for the console, that comes
with every flavour of Linux and UNIX. It is best known for
multiplexing a physical terminal across several processes. By using
it, you can run any number of console-based applications within a
single terminal. For example, suppose I want to run the 'top' program
to keep track of the system load, edit my programming code and check
my mail at the same time ... Usually, I have to open at least 3
terminals to do all this. But by using the screen utility, I can run
all these programs at the same time in the same physical terminal.
Another great feature of this wonderful utility is its ability to
decouple the terminal emulator from the running program, which
ensures your job is running even if you accidentally close your
window, or in case you log out. After detaching, you can reconnect
once again to the same session and regain control over the running
jobs. Here I will explain how to use this utility.
1) First open a
terminal (eg: xterm if you are in X windows) and type :
3) Now that you have started screen in a terminal, suppose you want to run the 'top' program to check the System load and at the same time you want to compile a program too - Also you would like to switch between the two to see the progress made by both. For that, first execute the first program to be run (say 'top'). now 'top' will start in the terminal. Now open a new window in screen by pressing the '[Ctrl + a] c' - which I will state as 'C-a c' . This will create a new window in the same terminal. Here, you can give commands to compile your program. In screen, each window is given a unique identifier. The first window is numbered 0, the next window is 1 and so on. Now to switch between your 'top' and the compiling program, you can use the key 'C-a 0' and 'C-a 1' respectively. You can also associate names with each running window - use 'C-a A' to give a name to the current window. Here are a few other commands that might come in handy while toggling between various running windows :
'C-a p' and 'C-a n' can be used to switch to the next or previous window respectively. 'C-a N' - where N is the number from 0 to 9, that can be used to jump to the corresponding window. 'C-a w' displays a list of all windows. The unique ID of each window with its name and running process is displayed, for each window. The current window is marked with an asterisk(*). 'C-a k' - can be used to kill the current window. You can also type 'exit' to kill the current window. If no more windows are open, then screen exits. 'C-a \' also does the same thing. 'C-a d' - detaches the present screen session. You can also detach by closing the terminal running your screen utility. Though, this appears to close your terminal session, in reality this does not happen. It only unbinds your session from the current terminal. All the programs started under screen will still keep running.
Note if you mess up the text with in this box just refresh the page.
You can also log out from the machine and re-login. Then start any terminal session and type 'screen -r' to once again be connected from where you left. In case, there were more than one screen sessions running on the machine, Screen prompts for a host.tty.pid. For example, say I have two screen sessions. So when I type 'screen -r' command, it gives the following message:
Type "screen [-d] -r [pid].tty.host" to resume one of them.Choose the proper tty.host to connect to and you are controlling your program again. You can also share your screen session with others like using VNC . Imagine you are having a problem with your code and want to share it with your boss who is located in another office. You can use screen to share your session with your boss, and he can see what you are referring to. The first user starts his screen in a normal fashion and executes the following commands ...
User1: [Ctrl-A]:multiuser on [RET] [Ctrl-A]:acladd
The second user starts a new screen session and connects it to the first session using ...
User2: screen -x
There are lots and lots of features in Screen other than those that were described here. To know more about Screen, visit the Official Screen Home Page. You can also find a very good introductory tutorial at Kuro5hin.org.
Finding the Number of days since Jan 1
To find out how many days have lapsed since January 1st. There is
a easy method in Linux. That is using the cal command. For example,
try the following :
$ cal -j 8 2007
August 2007
Sun Mon Tue
Wed Thu Fri Sat
213 214 215 216
217 218 219 220 221 222
223
224 225 226 227 228 229 230
231 232 233 234 235 236 237
238
239 240 241 242 243
Since today is Auguest 10, as of January 1st
2007, 222 days have elapsed. The option -j displays the Julian date.
Mount your files the Linux way
In Linux, if you want to access a filesystem be it a CDROM, a partition or a different drive including zip, floppy, usb keys and so on, you have to mount the filesystem first prior to start using it. In most cases, the system will do it for you. But sometimes you have to do it yourselves. This you do by using the 'mount' command. Here I will explain different things you can achieve by using this very important and useful command.
Check which filesystems are mounted Just enter the command:
...to see all the filesystems that are mounted on your machine.Re-mount a drive as read-only
This is useful if you want to take a backup of a filesystem in a production server. Once you have taken the backup, you can remount the filesystem as read-write. For example, if I want to remount the already mounted partition /dev/hda2 as read-only, I enter the following mount command:
... so that no one can write to the partition while the backup is going on. Once the backup is finished, I can remount it again as read-write: ro -ReadOnlyrw - ReadWrite
You will find the above command useful in a situation in which your system gets corrupt. And when you boot into single use r mode, the filesystem is mounted read-only. In which case, you have to remount it as read-write (see the above command) inorder to make changes to the configuration files.
Mounting a ISO image file to view its contents
If you have downloaded an ISO image (perhaps a Linux distribution) from the net and you want to see the contents of the file, there is an easy way of achieving it in Linux. You just mount it using the loopback device as follows:
This is desirable in a situation where, you have downloaded a Linux ISO on to your harddisk. But your computer's BIOS does not support booting from the CD Drive. In which case, you have to create a boot disk (floppy) so that you can start the installation by booting from the floppy. Most Linux ISO's contain a floppy bootdisk image which you can copy to your floppy.Move a already mounted filesystem to another location
I have already mounted a FAT32 partition in the '/mnt/D/' location. Now if I want to delink it from the current location and make it available at another location, I use the --move flag with the mount command as follows:
Now all the files on my FAT32 partition have been moved from /mnt/D/ to the /mnt/newlocation/. The files have not been actually moved but has been decoupled from /mnt/D/ mount point and linked at /mnt/newLocation/ .Mount a filesystem simultaneously at two different places
I can use the --bind or --rbind option to mount an already mounted, part of file hierarchy or an entire filesystem to another location with different rights. For example, I have a subdirectory called 'softwares' in the read-only mount point /mnt/D/ , which I want to make accessible to other users for writing data. I can achieve this by using the --bind command as follows:
Now only the subdirectory 'softwares/' is accessible as read-write in the location /mnt/backup. If you ask me, mount is a very important command in the Linux toolbox. Because, if there was no mount, Linux would not have been the same OS that it is now. The mount command has lots of other options. You may read the man page of mount to know more about it.
Script
You are compiling a program and all the error messages, scroll off your screen without giving you time to read. You wonder how you could keep track of all the messages that scrolls off your terminal . Linux comes with a useful command called script which can be used to log everything that scrolls off your screen . This command takes one parameter which is the filename into which you want all the messages to be stored. Once the script command is executed, you can continue doing what you were doing like compiling or running a command and so on. And everything that you see on the screen will be written to the file you supplied with the script command. Once you have finished your task, type exit to terminate the script command.
In the above example, you first start the script command and pass a filename to it ('my_log_file'). After this all the data ie the output of 'ls -l', 'ps' command and 'cat' is recorded by the script command and stored in 'my_log_file'. After you press exit, the script command terminates and you can read the contents in the my_log_file.Note: Script command can record the output of any linux command .