Wednesday, December 29, 2010

How to use multiple line comments in bash scripting

All single line comments in bash begin with # symbol, except for the first line (#!/bin/bash)
e.g.;
# this is an example of single line comment.

You can use HERE DOCUMENT feature to create multiple line comment

<<MULTILNCMNT
welcome to the go2linuxworld.blogspot.com
this is an example of
multiple line comment
in bash
MULTILNCMNT

Tuesday, December 28, 2010

How to debug a bash script in Linux

To debug a bash script, you need to run a shell script with -x option from the command line itself:
bash -x scriptname
or
bash -xv scriptname

You can also modify shebang line to run an entire script in debugging mode.

#!/bin/bash -x
echo "Hello ${USER}"
echo "Today is $(date)"

Monday, December 27, 2010

How to execute a scirpt in Linux

A shell script can be executed using the following syntax
chmod +x script.sh
./script.sh

You can also run the script directly without setting the script execute permission
bash script.sh
or
source script.sh
or
. script.sh

In the last example we are using . (dot) or source command which reads and executes commands from filename in the current shell.
When a script is executed using either the bash command or the source (.) command, you don't have to set execute permission on script.

reference: http://www.cyberciti.biz

Monday, December 13, 2010

How to run a C++ program in linux

Install g++ (Ubuntu) or gcc-c++ (RedHat or fedora) package using 
apt-get or yum.
sudo apt-get install g++
or 
yum install gcc-c++
Write a C++ program using your favorite editor.
emacs hello_world.cpp
or
vi hello_world.cpp
or
gedit hello_world.cpp
 #include <iostream>  
  using namespace std; 
  int main() { 
 
      cout << "Hello World!\n";
         
      return 0;
  }
The following command will compile your program 
and create an executable called a.out
   g++ hello_world.cpp
The following command will execute your program.
  ./a.out
Congratulations! You are done!
 

Friday, December 10, 2010

How to avoid writing sudo every time

If you are a sudoer and want to aovid writing sudo every time (like sudo commandname), just invoke a shell as root
sudo bash
Then you can execute all the commands as root.

**!! when you are finished do not forget to exit from the shell
 exit

Wednesday, December 8, 2010

How to install Flash Player for Mozilla firefox web browser

*Download the latest Flash Player from Adobe web site.
  http://www.adobe.com


*Select version to download

     YUM for linux
     .tar.gz for linux
     .rpm for linux  

     .deb for Ubuntu
     APT for Ubuntu


1. YUM for linux (RedHat, fedora)
install the downloaded .rpm file using rpm command.
rpm -ivh rpmfile.rpm
this will install adobe repository on your system.
now you can use yum command to install flash player.
yum install flash-plugin

2. .tar.gz for linux (for any distribution)
extract the .tar.gz file using tar command
tar xvf tarfile.tar.gz
you will get a file named libflashplayer.so. Copy this file to the mozilla plugins directory.
cp libflashplayer.so /usr/lib/mozilla/plugins/
change the permissions of the file if required.
Restart the firefox browser.

3. .rpm for linux (for RedHat, fedora)
install the .rpm file using rpm command
rpm -ivh rpmfile.rpm
verify the flash player plugin using the following command
ls /usr/lib/mozilla/plugins/
now restart the browser.

4. .deb for linux (Ubuntu)
install the .deb file using dpkg command
dpkg -i debfile.deb
verify the flash player plugin in mozilla/plugins directory.

5. APT for linux (Ubuntu)
install the .deb file using dpkg command
dpkg -i debfile.deb
this will install adobe repository on your system.
Now you can use the apt-get command to install flash player.
apt-get install flash-plugin.

Restart your web browser.






Wednesday, December 1, 2010

Output man pages as plain text with col

There is an easy way to convert man pages into simple, non-redundant text.
To save a copy of any man page to text simply type:

man command_name | col -b > output_file_name

e.g.;

man ls | col -b > grep.txt

This can be especially useful for larger man pages, as you can now open
and work with the text file in any GUI text editor.

Wednesday, June 23, 2010

How to run a Java program in Linux

Java (JDK) installation:
Download the latest jdk from sun's website http://java.sun.com/javase/downloads/index.jsp
e.g.; 
jdk-6u20-linux-i586.bin

now make this bin file executable.
chmod +x jdk-6u20-linux-i586.bin
To execute the bin file, type
./jdk-6u20-linux-i586.bin
Agree the License agreement. It will extract the content of bin file into a directory named jdk1.6.0_20
rename this directory to jdk1.6
mv  jdk1.6.0_20  jdk1.6
Now move this directory to /usr/share/
sudo mv jdk1.6 /usr/share/
set path to the java bin directory
export PATH=$PATH:/usr/share/jdk1.6/bin
or make it permanent in .bashrc file of your user's home directory
vim .bashrc
export PATH=$PATH:/usr/share/jdk1.6/bin
save and close this file
reread .bashrc file using source command
source .bashrc
Write a program in java: 
Open your favorite editor in Linux. Type a sample Hello world program 
/*
java Hello World example.
*/
class HelloWorldExample 
{ 
public static void main(String args[]) 
{ 
/* 
Use System.out.println() to print on console. 
*/ 
System.out.println("Hello World !"); 
} 
} 
/* 
OUTPUT of the above given Java Hello World Example would be : 
Hello World ! 
*/ 
Save it as HelloWorldExample.java
Now compile it
javac HelloWorldExample.java
it will create a file named HelloWorldExample.class. Now you can run the HelloWorldExample.class file.
java HelloWorldExample
*** Important!!!!! this is HelloWorldExample not HelloWorldExample.class

  
   

Monday, June 7, 2010

How to run a C program in Linux

To run a C program in Linux:

1. Verify the gcc program is installed on system
gcc --version

2. Write a test program
cat > mycprog.c
#include <stdio.h>
     int main (void)
        { printf ("My C Program \n");
            return 0;
         }

Save the program using ^D

3. Compile the C program
gcc mycprog.c

The output will be a.out compile file. Now run the compiled file by typing
./a.out

Output of a C program can be renamed
e.g.;
gcc mycprog.c -o firstprog

Now execute as
./firstprog

  

Friday, June 4, 2010

bash: rsync: command not found

problem: 
bash: rsync: command not found
          rsync: connection unexpectedly closed.......
          rsync error: remote command not found code (127) at ioc

solution: 
rsync should be installed on both the local and remote computer
To install rsync:
yum install rsync

if rsync installed in both local and remote machines it is complaining about rsync unavailability. What happened here is that it was not able to find rsync in standard path in remote machine. The solution for this problem is:
         --rsync-path=/path/to/rsync
In this type of problem we need to explicitly suggest the rsync path of remote machine through  --rsync-path argument.

    

Wednesday, May 26, 2010

How to make a permanent hostname for a redhat linux system

To make a permanent hostname for a redhat linux system, edit the following file
# vi /etc/sysconfig/network
HOSTNAME=station1.example.com

save this file. Now check the file /etc/hosts, it contain the old hostname, change/delete it.

restart the network service.
# service network restart

Logout and then login again.


   

Tuesday, May 25, 2010

root, sudo and shells

To start a root shell, starting root's environment and login scripts, use:
sudo -i
This is similar to sudo su - , gives you roots environment configuration.

To start a root shell, but keep the current shell's environment, use:
sudo -s 
This is similar to sudo su

 
 

Monday, May 24, 2010

How to disable root login (or any user's login)

If you want to disable root user's login (or any other user's login), you can change his/her home directory to /sbin/nologin or /bin/false.

Now user can not login into any shell.

  

Wednesday, May 19, 2010

execute a program periodically

To execute a program periodically, you can use 'watch' command

watch runs command periodically, displaying its output. This allows you to watch the command output change over time.
By default, the command is run every 2 seconds. Use -n or --interval to specify a different interval.

e.g.;

watch df -h
checks free disk space in every 2 seconds.

watch -n 5 df -h
checks free disk space in every 5 seconds.

   

Tuesday, May 18, 2010

sudo: cannot get working directory

sudo problem
sudo: cannot get working directory

solution:

at terminal type 'cd' and press enter

.

Sun Virtual Box -Tips n Tricks

Access windows folders in linux guest OS using Share folders in Sun Virtual Box
In linux you can use mount command to access windows share

mount -t vboxsf  share  mountpoint

where share is the name of shared folder in windows and mount point is the directory where you want to mount the share.
e.g.; 

mount -t  vboxsf   mypic  /mnt/pictures/


Linux File Structure

Linux File Structure

  • root - The home directory for the root user
  • home - Contains the user's home directories
  • bin - Commands needed during bootup that might be needed by normal users
  • sbin - Like bin but commands are not intended for normal users. Commands run by LINUX.
  • proc - This filesystem is not on a disk. It is a virtual filesystem that exists in the kernels imagination which is memory.

    • 1 - A directory with info about process number 1. Each process has a directory below proc.
  • usr - Contains all commands, libraries, man pages, games and static files for normal operation.

    • bin - Almost all user commands. some commands are in /bin or /usr/local/bin.
    • sbin - System admin commands not needed on the root filesystem. e.g., most server programs.
    • include - Header files for the C programming language. Should be below /user/lib for consistency.
    • lib - Unchanging data files for programs and subsystems
    • local - The place for locally installed software and other files.
    • man - Manual pages
    • info - Info documents
    • doc - Documentation
    • X11 - The X windows system files.
  • boot - Files used by the bootstrap loader, LILO. Kernel images are often kept here.
  • lib - Shared libraries needed by the programs on the root filesystem

    • modules - Loadable kernel modules, especially those needed to boot the system after disasters.
  • dev - Device files
  • etc - Configuration files specific to the machine.

    • skel - When a home directory is created it is initialized with files from this directory
    • sysconfig - Files that configure the linux system for devices.
  • var - Contains files that change for mail, news, printers log files, man pages, temp files

    • lib - Files that change while the system is running normally
    • local - Variable data for programs installed in /usr/local.
    • lock - Lock files. Used by a program to indicate it is using a particular device or file
    • log - Log files from programs such as login and syslog which logs all logins and logouts.
    • run - Files that contain information about the system that is valid until the system is next booted
    • spool - Directories for mail, printer spools, news and other spooled work.
    • tmp - Temporary files that are large or need to exist for longer than they should in /tmp.
  • mnt - Mount points for temporary mounts by the system administrator.
  • tmp - Temporary files.