Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Sunday, 6 July 2008

Hello World program examples

Hello World is the first program every user usually codes when he learns a new programming language.

Its goal is learning the basics to edit, compile and execute a program which sends a "hello world" string to standard output.


We have used an Ubuntu Hardy distro based system. Most commands will serve to other distros as well, like Debian.


BASH Shell Scripting Hello World


bash command line interpreter is usually already installed in your system.

if it is not installed we type: $sudo aptitude install bash


Now we create hello.sh script using whatever text editor we prefer.
e.g: $ gedit hello.sh


Copy this lines into your hello.sh script
#!/bin/bash
echo "hello world"


Bash shell scripting is an interpreted language so we have not to compile anything. We directly execute the shell script file:

We give the shell script execution permissions:
$ chmod a+x hello.sh

and launch the program
$ ./hello.sh


PYTHON Scripting Hello World


If you do not have python installed in your system, exec:

$ sudo aptitude install python

Sunday, 30 March 2008

GNU/Linux SYSTEM DIRECTORIES and their FILESYSTEMS

Executing $ ls / lists the top level of the root filesystem tree. There appear directories like:
/dev, /etc, /proc, /sys, /var, /tmp and others.

In this article we are going to deal with directories that have special filesystems associated with them.


For a general view of the filesystem hierarchy we can visit the Filesystem Hierarchy Standard page.

FHS standard is a set of guidelines about where to place files and directories within UNIX based operating systems.


Check which filesystems are mounted


Two commands allow us to examine which filesystems and where are currently mounted in our system.

$ mount
or
$ cat /etc/mtab


Their response will be something like that:
/dev/sda1 on / type ext3 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
/sys on /sys type sysfs (rw,noexec,nosuid,nodev)
varrun on /var/run type tmpfs (rw,noexec,nosuid,nodev,mode=0755)
varlock on /var/lock type tmpfs (rw,noexec,nosuid,nodev,mode=1777)
udev on /dev type tmpfs (rw,mode=0755)
devshm on /dev/shm type tmpfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
lrm on /lib/modules/2.6.22-14-generic/volatile type tmpfs (rw)
securityfs on /sys/kernel/security type securityfs (rw)


We are specially interested in /proc, /sys, /var/run, /var/lock, /dev, /dev/shm, /dev/pts, /sys/kernel/security




/proc directory

Friday, 14 March 2008

GNU Readline Library Short Description

GNU Readline is a software library that provides a set of functions that allow users to edit command lines as they are typed in.

Every program that uses this library provides the same user command line interface improving consistency between applications.

It is part of the GNU Project and it is licensed under the GPL.

Current version is 5.2 and it is used in projects like bash.

There are two editing modes available: Emacs (default) and Vi

Readline library includes additional functions to maintain a list of previously-entered command lines.
Exec this command to obtain more info about this feature:
$help history


INSTALLATION

In Debian and Ubuntu this library is packed in readline-common package.
$sudo aptitude install readline-common

Once installed, you can access a complete documentation using info command:
$info rluserman


SHORT KEYSTROKE REMINDER

NOTATION:
C-k means while ctrl key is mantained pressed, depress 'k' key.
M-k means while "meta" key is pressed, depress 'k' key.

Usually "meta" refers to ALT key, but if you have no meta key you can use ESC key instead.


Emacs mode commmands:

Movement

C-b Move back the cursor one character.
C-f Move one character forward.
DEL or Backspace Delete the character placed left at the cursor.
C-d Delete the character underneath the cursor.
C-_ or C-x C-u Undo the last editing command.

C-a Move to the beginning of the line.
C-e Move to the end of the line.
M-f Move forward a word.
M-b Move backward a word.
C-l Clear the screen, reprinting the current line at the top.

Killing and yanking (cutting and pasting) commands

C-k Kill the text from the current cursor position to the end of the line.
M-d Kill from the cursor to the end of the current word.
M-DEL Kill from the cursor the start of the current word.
C-w Kill from the cursor to the previous whitespace.

After killing(cutting) text, we yank(paste) it back into the line.

C-y Yank the most recently killed text back into the buffer at the cursor.
M-y Rotate the kill-ring, and yank the new top. You can only do this if the prior command is C-y or M-y.

Searching commands

C-r Searchs backwards on the previous commands you typed.
C-s Perform a forward search from the command line history you are now.

Passing arguments to a command

e.g: M-2 3 C-d means delete next 23 characters on input line.



CONFIGURING THE LIBRARY

GNU Readline keybindings and more can be changed editing ~/.initrc or /etc/initrc files.


URLs
GNU Readline Official page
GNU Readline (Wikipedia)
Official online Readline documentation

Tuesday, 19 February 2008

Bash tips II

RETYPING A COMMAND YOU TYPED SEVERAL LINES BEFORE

Sometimes you have to type a command you typed before, but you don't want to write it again, or don't remember exactly what you wrote on your command prompt.
Solution is easy, we are gonna use reverse search!

Press the keys: ctrl+r and then type part of the command you want to retype.
NOTE:It is not necessary to write the beginning of the command, you can write whatever part you want for reverse search.

If reverse search has found a command but it is not the one you were searching for, type ctrl+r again till you find it, or write a better part of the command at search time.


Now we have found the command, we have two options:
- pressing enter, the command will be executed
- pressing tab key we will be able to edit this command at the bash prompt prior to its execution.


CHANGING INTO ANOTHER DIRECTORY but memorizing where we are now in order to return later easily

This is useful if you have long weird named paths.

We are now located at foo directory:
:/foo$pwd
/foo


We want to change into bar directory, instead of using cd command we use pushd
:/foo$pushd /bar

Now we are in /bar directory:
:/bar$pwd
/bar


In order to return to prior foo directory we only have to run popd command.
:/bar$popd

:foo$pwd
/foo


CHECKING FOR BAD BLOCKS IN A PARTITION

Sometimes we want to check the blocks of a hard disk we have just bought, or whatever...

We have two options:

  • First time, when creating the filesystem with '-c' option

    e.g: mkefs.ext3 -c -b 4096 -L foo /dev/sdb2


  • If the filesystem at this partition has been already created we can use badblocks command.

    NOTE: Read carefully badblocks man page, $man badblocks before doing anything potentially harmfull.

    e.g: sudo badblocks -v /dev/sdb2

Sunday, 17 June 2007

Bash tips

http://builder.com.com/5100-6372_14-5827311.html

Customizing the bash command prompt




COMMAND REFERENCE

Linux Command Reference Index

Another command Reference

O'Reilly Command Reference

GNU/Linux Command Line Tools Sumary

Learning the Shell Tutorial





Changing X resolution

$xrandr #shows all available resolutions
i.e:
SZ: Pixels Physical Refresh
*0 1024 x 768 ( 271mm x 203mm ) *61
1 800 x 600 ( 271mm x 203mm ) 73
2 640 x 480 ( 271mm x 203mm ) 73


now, to switch to 800x600 resolution we can exec:

$xrandr -s 1

or

$xrandr --size 800x600

wait for 5 secs and voila! resolution changed!

NOTE: another way to change resolution is pressing ctrl+alt++ or ctrl+alt+- keys

NOTE: this command is very useful to change qemu X-windows resolution when it is bigger than your screen.




Listing file sizes in human readable form:

ls -l # shows file sizes, usually in bytes.

if you want to list them in a more readable way type:

ls -lh

NOTE: this "h" switch option also works with other commands like df, du, etc.




Obtaining an ISO file from a CDRom

You need to now what device your cdrom drive is associated to: (In this example will be /dev/sdX)

exec this command:
dd if=/dev/sdX of=cdrom_image.iso bs=1000000

that makes a byte by byte copy from /dev/sdX device (our cdrom) to cdrom_image.iso file in 1000000 bytes size chunks.

NOTE: Change bs argument to suit your needs. That value has effect on the speed the copy is performed.