Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VARIABLE using first character in a directory 1

Status
Not open for further replies.

maxcrook

Programmer
Jan 25, 2001
210
GB
I have a directory /home/max/delivery/process
That contains that files 5nnnn 2nnnn and Innnn I wish to define a variable so that it can determine what type of file is present within the /home/max/delivery/process area.
ie.Whether it is a 5 2 or I file - Can this be done ie:

filename=/home/max/delivery/process/TYPE_OF_FILE

echo $TYPE_OF_FILE

Should return 5 2 or I

Have i made this clear ?



 
Surely what you are asking can be done. I must confess though that I am still a bit unclear as to what you are looking for. I have a couple of conclusions that I have drawn, but would rather not elaborate until I know exactly what you would like to type, and what type of result.

Bill.
 
Try this.
#List what is in the directory
file=`ls -1 =/home/max/delivery/process/*`

#get just the file name
file1=`basename ${file}`

#remove the 4 n character on the right KORN Shell
echo ${file1%nnnn}

#remove the all n characters on the right KORN Shell
echo ${file1%%n*}

#get the first character in the filename
file2=`echo ${file1} | cut -c1`
echo ${file2}
 
I have files in directory which are manipulared within a script. There are three different file types.

/dir/dir/I12345678
/dir/dir/200332333
/dir/dir/523423423

They are identified by the first character in the filename and will always be I 2 or 5.

I need to use conditions within the script to decide how to handle each file. So I need to identify the file type.

In DCL, I can set the filetype as a variable using F$EXTRACT - and my Unix book tells me I can do it using the following. ${filename:pos:len}

In AIX 433 - this doesn't work and reports that the substitution can't work.

How can I extract the character at the beginning of the filename and use it as a variable in a shell script?

 
Look at the "basename" and "dirname" command.

$ pathfile="/dir/dir/.../somefile"
$ path=`dirname $pathfile`
$ file=`basename $pathfile`
$ char=`echo "$file"|cut -c1`

$ echo $path
/dir/dir/...

$ echo $file
somefile

$ echo $char
s

How about something like this ?

Bill.
 
I'll use:
[tt]
FILEPATH=/your/filename/here
FILENAME=$(basename "$FILEPATH")
FILECHAR=$(expr substr "$FILENAME" 1 1)
[/tt]

I hope it works...
Unix was made by and for smart people.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top