The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to find a string in a directory/sub-directory gross UNIX for Dummies Questions & Answers 3 11-13-2008 04:45 PM
Find a string under a directory that is contained in another file BMC Shell Programming and Scripting 3 09-17-2008 01:41 PM
find and replace string in a directory files koti_rama Shell Programming and Scripting 2 05-30-2008 03:48 AM
Appending a string to all files in a directory ragavhere Shell Programming and Scripting 1 04-28-2008 06:31 AM
searching for a string in directory warrend UNIX for Dummies Questions & Answers 4 09-27-2002 08:58 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 2 Weeks Ago
wtolentino wtolentino is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 33
How To Get The String Right After the Directory Name

i have this basic line of code that accepts for an input parameter. Example of input string is "/u02/app/ccalloc/dev/out/*.txt", a directory name with a wild card filename. my code below can get the directory name by using the `dirname $p1` command. when i attempted to use the `basename $p1` to get the filename with wild card character it failed.

Code:
$ sh sample.sh "/u02/app/ccalloc/dev/out/*.txt"
Usage: basename String [Suffix]
the directory is /u02/app/ccalloc/dev/out

the file string is

$
below is the code
Code:
#!/bin/sh

p1=$1
pdir=`dirname $p1`
pfile=`basename $p1`
echo "the directory is $pdir\n"
echo "the file string is $pfile\n"
please help. thanks.
  #2 (permalink)  
Old 2 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,046
Do you have more than one .txt file in that directory?

By quoting this "/u02/app/ccalloc/dev/out/*.txt" all text files will become $1 to the script. Basename can only handle one file at a time.

Either unquote the argument so basename can use the first one, or use a loop to process all of the .txt files:

Code:

for FILE in $@; do
  basename "$FILE"
done
  #3 (permalink)  
Old 2 Weeks Ago
wtolentino wtolentino is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 33
great that works. i need to add a second parameter when i added a second parameter it appears that it is also including the second parameter string.

Code:
$ sh sample.sh "/u02/app/ccalloc/dev/out/*.txt" "/export/home/ccalftdv/data"
EATVDAILY02132008.txt
EATVDAILY03292007.txt
EATVDAILY04082008.txt
EATVDAILY05012008.txt
EATVDAILY05282008.txt
EATVDAILY06052007.txt
EATVDAILY06062007.txt
EATVDAILY06192009.txt
EATVDAILY06222009.txt
EATVDAILY06242009.txt
EATVDAILY07132009.txt
EATVDAILY07142009.txt
EATVDAILY07152009.txt
EATVDAILY07162009.txt
EATVDAILY10212009.txt
PCARDDAILY07102009.txt
citicc_pre_pack.txt
data
$
the last display data is coming from the second parameter "/export/home/ccalftdv/data". how do i exclude that? thanks.
  #4 (permalink)  
Old 2 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,046
In the for-loop, change $@ to $1.

$2 is "/export/home/ccalftdv/data"
  #5 (permalink)  
Old 2 Weeks Ago
wtolentino wtolentino is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 33
wonderful that works. many thanks.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0