Showing posts with label Linux Command and Shell scripting. Show all posts
Showing posts with label Linux Command and Shell scripting. Show all posts

Tuesday, 3 April 2012

Advanced Shell Script . [Back up]


#!/bin/bash
# Shell script (BASH) to backup the selected directory on server and upload
to
# another ftp server securely. This script uses the gpg command to
# encrypt the .tar.gz file before upload take place.
#
# In order to run this script you must have following tools installed:
# - /usr/bin/ncftpput
# - /bin/tar
# - /usr/bin/mail
# - /usr/bin/gpg
#

# Script also mails back the ftp operation failed or not
#
# Installation:
# Customize the script according to your need. You need to setup ftp
# server, password etc. Next, you need to setup gpg user name and
# import public key so that you can encrypt the files. Usually following two
# commands needed for gpg:
# gpg --import userkey
# gpg --edit-key KEY_ID|USER_ID
# Command>trust
#
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project.
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Dirs to backup, Separate multiple directories using space
# for example /home /www /data2
BACKUP="/home"
# Remote ftp server
FTPH="ftp.backup.com"
# Remote ftp user name
FTPU="ftpusername"
# Remote ftp user password
FTPP="secret"
# Local gpg user_id
GPGU="nixcraft"
# Remote directory, blank for default remote dir
# If dir does not exist it will be created automatically by ncftpput :)
FTPD="backup/"
# Temporary directory to store tar.gz file and process it
TMPD="/tmp"
# Mail message
# Admin email me@mycorp.com or pager@yourmobile.com
MTO="support@mycorp.com"
# Mail subject
MSUB="Backup $(hostname) report"
# Admin info, URL email id; change it according to your need :)
ADMIN_INFO="For support visit http://cyberciti.biz/fb/ or write an email to
nobody@cyberciti.biz"
# Only change if your UNIX stores bin in diffrent location
NCFTP="/usr/bin/ncftpput"
TAR="/bin/tar" # must be gnu tar
MAILC="/usr/bin/mail"
GPG="/usr/bin/gpg"
#######################################################################
# Do not change anything below
#######################################################################
FILE="$(hostname).$(date +"%d-%m-%Y").tar.gz"
OUT="$TMPD/$FILE"
FOUT="$OUT.gpg"
MFILE="/tmp/ftpout.$$.txt"
MESS=""
if [ ! -x $TAR ]; then
echo "$TAR command not found, contact $ADMIN_INFO"
exit 1
fi
if [ ! -x $NCFTP ]; then
echo "$NCFTP command not found, contact $ADMIN_INFO"
exit 1
fi
if [ ! -x $GPG ] ; then
echo "$GPG command not found, contact $ADMIN_INFO"
exit 1
fi
$TAR -zcf $OUT $BACKUP
if [ $? -ne 0 ];
then
MESS="$TAR failed to create backup. Nothing uploaded to remote FTP $FTPH
server"
else
# Encrypt the .tar.gz file before upload
$GPG -e -r $GPGU -o $FOUT $OUT
$NCFTP -m -u "$FTPU" -p "$FTPP" "$FTPH" "$FTPD" "$FOUT"
OSTAT="$?"
case $OSTAT in
0) MESS="Success.";;
1) MESS="Could not connect to remote host $FTPH.";;
2) MESS="Could not connect to remote host $FTPH - timed out.";;
3) MESS="Transfer failed.";;
4) MESS="Transfer failed - timed out.";;
5) MESS="Directory change failed.";;
6) MESS="Directory change failed - timed out.";;
7) MESS="Malformed URL.";;
8) MESS="Usage error. May be your version of ncftpput ($NCFTP) is
old";;
9) MESS="Error in login configuration file.";;
10)MESS="Library initialization failed.";;
11) MESS="Session initialization failed.";;
*) MESS="Unknown error, contact admin $ADMIN_INFO";;
esac
fi
>$MFILE
echo "Backup status for $(hostname) as on $(date):" >>$MFILE
echo "" >>$MFILE
echo "Backup File : $FOUT" >>$MFILE
echo "Backup ftp server : $FTPH" >>$MFILE
echo "Backup status message : $MESS" >>$MFILE
echo "" >>$MFILE
echo "-- Automatically generated by $(basename $0)" >>$MFILE
# send an email to admin
$MAILC -s "$MSUB" $MTO <$MFILE
# remove the files
[ -f $MFILE ] && rm -f $MFILE || :
[ -f $FOUT ] && rm -f $FOUT || :
[ -f $OUT ] && rm -f $OUT || :

Monday, 19 March 2012

Linux beginner's Workshop 1.

Linux beginner's Workshop

 

This is a slideshow used in an introductory Linux workshop. This is Best slide to which i have found on google for you to understand in better way .

Sunday, 18 March 2012

C programming on Linux For Beginners


Linux is probably the best platform to start developing programs for beginners.This tutorial covers C programming basics like installation, compiling first program, running first C program on Linux.


C Programming on Linux:
Here is step by step detailed guide on showing you how to write and compile a C program in Linux. Note that the C code that you will write on Linux will be same that you would write on Windows/DOS, as long as you are writing ANSI C code. Some library functions, such as those provided by conio.h and graphics.h, are not part of the ANSI standard. Hence you won’t be able to use them on Linux. The C compiler you use on Linux is GCC





Open a terminal and run the command gcc:


$ gcc
gcc: no input files


If you see something like the above output, gcc is already installed. If you see something like “Command not found”, then you will have to install gcc using the package manager. Besides a compiler, you will also need the C standard library, called glibc, to compile your C programs correctly. Type in

# locate glibce




and check the output. If it shows directory structures of the form ‘/usr/share/man/man7/glibc.7.gz’ or ‘foo/bar/glibc’ or the like, then you have glibc installed; else you need to install it.
Okay, now that we have confirmed the presence of a text editor, a compiler and the standard library, let us write our first code in C on Linux. For demonstration purposes, I’ll show you how to write and compile Hello World!
Start up gedit (You can use vim or other editor also) and input the simple C code to print the Hello World!

Or use the terminal to open your favourite text editor, type in
 


$ gedit PROGRAM_NAME.c   e.g  $ gedit Hello_World.c
OR
$ vim PROGRAM_NAME.c    e.g   $ vim Hello_World.c


Now input this simple C code to print Hello World!

#include <stdio.h>


int main()
{
    printf("Hello World!\n");
    return 0;
}
Save this code with the name  Hello_World.c  Now, compile the code using the following command:
$ gcc  Hello_World.c  
After executing the command, type in
ls -l  
You will see an ‘a.out’ file. This is the executable file of your C program, compiled and linked with the appropriate libraries. To execute it, run(note the leading ./, which is essential!):

$ ./a.out
Hello World!

 Congratulations, you have just written your first C program on Linux! That was just the normal C that you write on DOS or Windows – no surprises there!
A bit more about this a.out file: This is the Linux equivalent of the .exe file that you would see under DOS/Windows; it is executable form of your code. As you might have already guessed, this file cannot be executed on DOS or Windows, since it is in a different format. Now instead of having to rename your executable file each time you compile, you can specify the output file name to the compiler:
 $ gcc -o Hello_World Hello_World.c
If you still have any questions/concerns/suggestions, share it on our comment below!

Make Your Linux Box Speak


Ubuntu and many other distros have an inbuilt speech synthesiser called espeak. Use the following command in the terminal:
espeak Linux

Did you hear your Linux box report, “linux” ?
Now replace linux with these words .and listen .
"I'm new in Linux World"

Sunday, 11 March 2012

Intorduction ~ Linux Command and Shell Scripting Series.


Quick Start
Linux Shell Scripting Tutorial
________________________________



Introduction  :

Salam to all
 In This Tutorial Series you Will learn a complete linux command and shell scripting . If u are beginner then you are at right place . No extra things I have included in my tutorial cover all the Basic things In short span of Time . To get the most benefit from thisTutorial you need to install  a Unix machine, preferably with AIX, HP-UX,
Linux,Ubuntu,Backtrack or Solaris installed. I urge everyone to study this entire Tutorial . Every Tutorial hits a different topic using a different approach. All of the shell scripts in this Tutorials are realworld examples of how to solve a problem. I hope you enjoy this Tutorial as much as I enjoyed writing it. Let’s get started!



Tutorial  1
Quick Introduction to Linux
1. What Linux is ?
2. Who developed the Linux ?
3. Different Linux Distribution ?
4. Where I can use Linux ?


What Linux is ?
Linux is 
1. Free
2. Unix Like
3. Open Source
4. Network operating system




Free
Linux is free.
First ,It's available free of cost (You don't have to pay to use this OS, other OSes like MS-Windows or Commercial version of Unix may cost you money)
Second free means freedom to use Linux, i.e. when you get Linux you will also get source code of Linux, so you can modify OS (Yes OS! Linux OS!!) according to your taste.
It also offers many Software applications, programming languages, and development tools etc. Most of the Program/Software/OS are under GNU General Public License (GPL).


Unix Like
Unix is almost 35 year old Os.
In 1964 OS called MULTICS (Multiplexed Information and Computing System) was developed by Bell Labs, MIT & General Electric. But this OS was not the successful one.
Then Ken Thompson (System programmer of Bell Labs) thinks he could do better (In 1991, Linus Torvalds felt he could do better than Minix - History repeats itself.). So Ken Thompson wrote OS on PDP - 7 Computer, assembler and few utilities, this is know as Unix (1969). But this version of Unix is not portable. Then Unix was rewrote in C. Because Unix written in 'C', it is portable. It means Unix can run on varity of Hardware platform (1970-71).
At the same time Unix was started to distribute to Universities. There students and professor started more experiments on Unix. Because of this Unix gain more popularity, also several new features are added to Unix. Then US govt. & military uses Unix for there inter-network (now it is know as INTERNET).
So Unix is Multi-user, Multitasking, Internet-aware Network OS. Linux almost had same Unix Like feature
for e.g.
1. Like Unix, Linux is also written is C.
2. Like Unix, Linux is also the Multi-user/Multitasking/32 or 64 bit Network OS.
3. Like Unix, Linux is rich in Development/Programming environment.
4. Like Unix, Linux runs on different hardware platform .


Open Source
Linux is developed under the GNU Public License. This is sometimes referred to as a "copyleft", to
distinguish it from a copyright.
Under GPL the source code is available to anyone who wants it, and can be freely modified, developed,
and so forth. There are only a few restrictions on the use of the code. If you make changes to the
programs , you have to make those changes available to everyone.


For more details, please visit the open-source home page.


Who developed the Linux?
In 1991, Linus Torvalds studding Unix at the University, where he used special educational experimental purpose operating system called Minix (small version of Unix). But Minix had it's own limitations. Linus felt he could do better than the Minix. So he developed his own version of Minix, which is now know as Linux. Linux is Open Source From the start of the day. 


How to get Linux?
Linux available for download over the net, this is useful if your internet connection is fast. Another way is order the CD-ROMs which saves time, and the installation from CD-ROM is fast/automatic. Various Linux distributions available. Following are important Linux distributions. 


Red Hat Linux:  http://www.redhat.com/
SuSE Linux:  http://www.suse.com/
Mandrake Linux:  http://www.mandrakesoft.com
Caldera Linux:  http://www.calderasystems.com/
Debian GNU/Linux:  http://www.debian.org/
Slackware Linux:  http://www.slackware.com/


How to Install Linux
There is lot of tutorial on internet just search on Google .


Where I can use Linux ?
You can use Linux as Server Os or as stand alone Os on your PC. (But it is best suited for Server.) As a server Os it provides different services/network resources to client. Server Os must be

  •  Stable
  •  Robust
  •  Secure
  •  High Performance



What Kernel Is ?
Kernel is heart of Linux Os.
It manages resource of Linux Os. Resources means facilities available in Linux. For e.g. Facility to store data, print data on printer, memory, file management etc . Kernel decides who will use this resource, for how long and when. It runs your programs (or set up to execute binary files)
The kernel acts as an intermediary between the computer hardware and various programs/application/shell. 


What's Linux Shell
A shell is an environment in which we can run our commands, programs, and shell Scripts .  .
Computer understand the language of 0's and 1's called binary language. In early days of computing, instruction are provided using binary language, which is difficult for all of us, to read and write. So in Os there is special program called Shell. Shell accepts your instruction or commands in English (mostly) and if it’s a valid command, it is pass to kernel. Shell is a user program or it's environment provided for user interaction. Shell is an command language interpreter that executes commands read from the standard
input device (keyboard) or from a file. Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.


Several shell available with Linux including
Tip: To find all available shells in your system type following command
$ cat /etc/shells


Tip: To find your current shell type following command
$ echo $SHELL
Note that
In MS-DOS, Shell name is COMMAND.COM which is also used for same purpose, but it's not as powerful as our Linux Shells are! Any of the above shell reads command from user (via Keyboard or Mouse) and tells Linux Os what users want. If we are giving commands from keyboard it is called command line interface.

Thanks :)