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

0 comments:

Post a Comment