Getting last character

Hi i am passingsome parameter while running the script. so ineed to take the last character of thse parameters. i know we can take value of parametr using $

But i need last character and last but one character to store in seperate variables.

This is because, i need to find out the records with the starting characters. i dont know how to pass parameter dynamically so i am appending these with file names.

Eg: Script.sh File01 File19 so on(can be no. of files might be more or less.).

then i can take the File01 and take the last and last but one characters.
Is there any other way to pass these values.

Which script interpreter? For ksh/bash, there are length and substring operators, and arithmetic, and you can ask for a scan of a constructed string using 'expr', like the last argument is:

expr '$'"$#"
 
For quick and dirty, you can:
 
echo $* | tail -2c    # two is one for line feed and one for character

We are using bash

Well, I know what it can do, and I just look up the details when I occasionally need them, like google 'bash substring tutorial'.

To get last and last but one character

VAR=$(echo 'File01'|sed 's/.*\(..$\)/\1/')
VAR=$(echo 'File01'|tail -c3)

thanks to all.

I got the characters waht i want.

But there is two more problem with my script i am using positionsal parameters to pass the all parameter which are used while running the script.

 
for file in $@

this is working fine but i need to pass the directory path also as arguement. so how do we skip that path positional parameter.

 
For Eg: Script.sh <Directory path> <file1> <file2> <file3>....
  1. some times there might be a chance to get the ebcdic format files how can we handle these files?

Below is my code:

#!/bin/bash
#################################################################################################
# This Script will check the Whether Headers and Trailers exist and not more than 1. If any file#
# does not meet this criteria script will return the non zero. If all files are ok it returns #
# zero(0). #
# Usage: Script.sh <filename><Header_identifier><Trailer_Identifier> <filename><Header_identifie#
#r><Trailer_Identifier>..... and so on. #
# #
#################################################################################################
set -x
Result=0
[ -f "$0.log" ] && > $0.log
function log
{
echo "$@" >> "$0.log"
}
for file in $@
do
################################################################################################
# Retrieve the File name, Header and Trailer identification from the arguements.
################################################################################################
file_temp=`echo $file`
length_file=${#file_temp}
filename=${file:0:length_file-2}
headerid=${file:length_file-2:1}
trailerid=${file:length_file-1:1}
################################################################################################
cd /informationserver/projectsw/TescoBank_SoW043/landing/
if [ -f $filename ]
then
if [ `grep "^[$headerid]" $filename | wc -l` = 1 ]
then
if [ `grep "^[$trailerid]" $filename | wc -l` = 1 ]
then
Result=` expr $Result + 0`
else
Result= ` expr $Result + 1`
log There is No Trailer in $filename
fi
else
if [ `grep "^[$trailerid]" $filename | wc -l` = 1 ]
then
Result=` expr $Result + 1`
log There is No Header in $file
else
Result=` expr $Result + 1`
log There is No Header&Trailer in $filename
fi
fi
else
log No $filename exists in the directory.
Result=` expr $Result + 1`
fi
done

echo $Result

Parametrs: Usually, either:

  • those sort of parameters go first, and you shift them away after storing or acting on them, or
  • you recognise them right under the do, cd to that dir or store it in a variable, and continue.

EBCDIC: Once you detect that (how? name ? failure to parse as ascii ?), in ksh where there is /dev/fd/# or bash you can set the file interrogation name variable to "<( dd i-forget-the-details $file_in - )".

tail -3c, is this a WinDOS file with cr and lf? cat -v and cat -vte provide a nice way to see hidden characters, even on binary files.

Thanks DGPickett

I will try the first one.

For regardign EBCDIC, i need to find out whether there is Header and Trailer records. Here i want to write a Generic Script it should check the file(either its is EBCDIC or ASCII).

Well, if no header in ascii, see if there is a header in EBCDIC. There are no magic markups, files are piles of bytes and can be anything. Code and scripts have embedded magic, see man file, but it is shaky, saying gzipped files are awk or perl!

Do you want it translated? Rename it $file.ebc, and then dd an ascii copy to $file.

Thanks DGPicket

I have done the first thing. it worked for me

for second question, When i get this EBCDIC format file script is not able to identify the record which starts with 1 and Z then my condition is failing becasue there should be at leaset one record wich starts eith 1 and one with Z in the each file.

So if we can convert thies to ASCII we can achieve this i believe, but we are not sure which input file will be EBCDIC and ASCII. So we need to find this do the rest of the process