Thursday, August 8, 2013

SHELL SCRIPT TO CONCATENATE TWO STRINGS AND DISPLAY LENGTH OF NEW STRING.

Here is a simple interactive shell script to display concatenation of two strings entered by the user and display the length the resultant string.

Vim Script_Name.sh
:i
#!bin/bash
echo -n "Enter  the first string: "                                              
read  str1                                                                   # input the first string.
echo -n "Enter the second string: "
read str2                                                                    # input the second string
str3=$str1$str2                                                         # concatenate   the above two input strings.
len=`echo $str1 | wc -c`                                         # find the length of first  input string.
len1=`echo $str2 |wc -c`                                       # find the length of second input string.
len2=`echo $str3 | wc -c`                                     # find the length of concatenated string.
echo "The length of first string is : `expr $len – 1`"            # display the length of first string.
echo "The length of second string is : `expr $len1 – 1`"   # display the length of second string.
echo "THe lenght of concatenated string is : `expr $len2 – 1`" # display the length of concatenated string.
:wq

The above script can be executed directly as follows:
# bash Script_Name.sh
  OR
#  sh Script_Name.sh

The script can also be executed  by giving execute permission as follows:
# chmod +x Script_Name
# ./Script_Name

Tuesday, August 6, 2013

HOW TO GIVE USER-BASED AUTHENTICATION TO WEBSITE IN APACHE SERVER



The htpasswd is the command used to give password to the various users who try to access a particular webpage in Apache 
Web Server.The following commands are executed to provide the user-based authentication:

 # htpasswd -cm /etc/httpd/conf/.htpasswd User_Name

Change the directory to http configuration file where the above file (.htpasswd) is created.
 # cd /etc/httpd/conf

Check the content of the file created using following command which will  show the encrpyted password:
 # ls -a

the above command shows the hidden files.

Next create the access file in the directory where webpages are  placed as follows:
 # vim /var/www/html/WebPage_Dir/.htaccess

Make the following entries in the above file :
 :i
 AuthType Basic  #AuthType directive selects that method that is used to authenticate the user. The most common method is 
Basic AuthName “Server Authentication”    #This directive sets the Realm to be used in authentication which is presentd to user 
as part of password dialog box as well as client uses it to determine what password to send for given authenticated area.
 AuthUserFile  /etc/httpd/conf/.htpasswd       #AuthUserFile directive sets the name of a textual file containing the list of users 
and passwords for user authentication.
 Require valid-user           #Grant access to all successfully authenticated users.
 :wq

Finally, to implement the changes make the following changes in th configuration file of apache server which is 
/etc/http/conf/httpd.conf:
 # vim /etc/httpd/conf/httpd.conf
 :i
 <directory /var/www/html/WePage_Dir>
  AllowOverride Authconfig
 </directory>

Now restart the service daemon and access the site from the client-end.
 # service httpd restart

HOW TO SET YUM-PLUGIN-PRIORITY IN CENTOS 6

The yum-plugin-priority associate a priority to the repositories so that packagesfrom repositories with lower priority will not be used to upgrade packages which were initially installed from higher priority repository. The users can also set the priorities to the repositories so that the packages are installed from higher priority repository if present in multiple repository.This plugin help user to update sytem from repositories which help in maintaining the functioning of CentOS.

To enable the priority-plugin the option of plugin should be enabled in /etc/yum.conf file as follows:

# vim /etc/yum.conf
     [main]

     cachedir=/var/cache/yum/$basearch/$releasever

     keepcache=0

     debuglevel=2

     logfile=/var/log/yum.log

     exactarch=1

     obsoletes=1

     gpgcheck=1

     plugins=1

     installonly_limit=5

     distroverpkg=centos-release
  


The priorities plugin is available at two locations:
By default enabled CentOS Extras Repository where yum-plugin-priority can be installed using following command :

  # yum install yum-plugin-priorities
 
yum-utils sources which has README file with instructions to install the plugin in the archieve directory.


Once the package is installed it should also be enabled in the /etc/yum/pluginconf.d/priorities.conf :

  [main]

  enabled = 1




Following the above steps enables the plugin.Now we can add priorities to the repositories by appending following line
  
 priority=N

to the targeted repository,where N is integer ranging from 1 to 99. Repositories with the lowest numerical value have the highest 
priority. It is best practice to give CentOS base and update repositories very high priority.
The default priority for repositories is 99.

Monday, August 5, 2013

HOW TO REVERSE A NUMBER


Here is a simple shell script to reverse the digits of the number entered by the user.

echo "Enter the number to be reversed where the first digit should have range from 1-9"   #prompt the message to enter the 
number such that first digit is not zero
read number                                                       #enter the number
var=${#number}                                               #find the number of the digits in the  number                                                           
for((i=1;i<=$var;i++))                                       # initiate the loop
do
var1=$((number%10))                                        #find the last digit of the number
echo -n "$var1"                                                 #display the last digit such that output should not trail to the new line.
number=$((number/10))                                  #remove the last digit     
done                                                                    #terminate the loop
echo                                                                  #change to new line


The above script is simple technique to reverse any number entered by the user.
Note : The script can be executed by giving execute permission as below:
                     # chmod +x Script_Name
        # ./Script_Name
                         OR 
Executing the script directly as follows:
                      # bash Script_Name
                             OR
                     #  sh Script_Name

HOW TO READ FILE WORD BY WORD


Here is a simple script which reads and display each word of the file separated by a blank space whose path is entered by 
the user. The IFS(Internal File Separator) of the file is blank space in other case it should be changed accordingly.

echo -n "Enter the path to file whose content is to be read line by line :  "
#ask user to enter the path of the file whose data is to be read word by word
read file_path
#input the path
for i in `cat $file_path`
#start the loop 
do
echo $i
#display each word of file separated by blank space
done
#terminate the loop


Note: The above script can be executed by giving it execute permission as follows:
 #chmod +x Script_Name
 #./Script_Name
   OR
Directly executing the script as follows:
 #bash Script_name
 #sh Script_Name

Friday, August 2, 2013

HOW TO MOUNT NTFS PARTITIONS IN LINUX(CENTOS6.4)

Ntfs-3g is GPL licensed,cross-palteform and read/write driver used in Linux which  is provides safe handling of files in 
NTFS format. 
The basic requisite is to install the package which facilitate the mounting of nts partition in linux.This utility is provided by ntfs-3g 
package which are available    in EPEL Repository.
 # yum -y install ntfs-3g
The EPEL Repository for CentOS6.4 can be downloaded using following steps.
#wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
#rpm -ivh epel-release-6*.rpm
# yum install fuse
With FUSE it is possible to implement a fully functional filesystem in a userspace program.  Features include:
  • Simple library API
  • Simple installation (no need to patch or recompile the kernel)
  • Secure implementation
  • Userspace - kernel interface is very efficient
  • Usable by non privileged users
  • Runs on Linux kernels 2.4.X and 2.6.X
  • Has proven very stable over time


Install and load FUSE driver to mount detected devices
# modprobe fuse
Next,make the directory where the partition is to be mounted.
# mkdir /mnt/ntfs_partition
Temporary mounting can be done by following command.
# mount -t ntfs-3g /dev/sda1 /mnt/ntfs_partition
For permanent mounting make the following entry in /etc/fstab file:
/dev/sdx    /mnt/usb    ntfs-3g        defaults    0    0

where /dev/sdx is hard disk partition with NTFS file system.
Thus, the data can now be accessed by changing directory where partition is mounted.

Thursday, August 1, 2013

HOW TO READ A FILE LINE BY LINE USING SHELL SCRIPT


A simlpe shell script to read any file line by line 
where the name of file is passed as the argument:
~]# touch navneet.sh
~]# chmod 755 navneet.sh
~]# vi navneet.sh
 
 file_name=$1                                         #positional parameter $1 is passed as the argument to the script
flag=0                                                       #initialize the variable read the file
cat $file_name | while read LINE       #pipe the content of file to while loop
do  
let flag++                                                #post increment of flag counter
echo "$flag $LINE"                              #display each line with line number
done                                                      #termination of loop 
esc:wq                                                  #save file

The above script displays the content of the file name entered by line.