LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-28-2005, 09:48 AM   #1
chooi
LQ Newbie
 
Registered: Mar 2005
Posts: 4

Rep: Reputation: 0
shell script problem on sed.


Hi all,
here is my shell script.

Code:
backup_files()
{
  FSource=$1
  if [ $ABSOLUTE = "1" ]; then
        FTarget="$BACKUPPATH/`date '+%Y%m%d'`"
	
	desDir = "`echo $FILEPATH | sed -e 's/\/.*/ /'`"
	TmpTarget = $FTarget/$desFir
	if [ ! -d $TmpTarget ]; then
             mkdir -p $TmpTarget
        fi
	cp $DATAPATH/$FSource $Tmptarget/.

        
  else
        FTarget="$BACKUPPATH/`date '+%Y%m%d'`"
	TmpTarget = $FTarget/$FILEPATH
        
	TmpTrgt = "`cd $TmpTarget | cd .. | pwd`"
        if [ ! -d $TmpTrgt ]; then 
	     mkdir -p $TmpTrgt
        fi
	cp $FSource $TmpTrgt/.

  fi
}
what i want to do is :
let say the $FILEPATH = "/tmp/200/readme"
i want the desDir to capture only the "readme" by detect the last "/"
i write the statement like this:
desDir = "`echo $FILEPATH | sed -e 's/\/.*/ /'`"
But failed to get the value, it return empty.

Can anyone teach me how to use the sed or awk? thanks.
 
Old 03-28-2005, 10:09 AM   #2
futhark
Member
 
Registered: Nov 2003
Location: Montréal (Can)
Distribution: FC4
Posts: 110

Rep: Reputation: 15
Well my suggestion would be not to use sed how awk. Since you seem to be using sh, here's something to try:

file=/tmp/readme.test
echo ${file##*/}

Dissecting ${file##*/} :

${file} : the var we work on

##*/ : replace from beginning of string, up to last occurence of /

The greatest bash doc around is this one:
http://www.tldp.org/LDP/abs/html/
 
Old 02-10-2006, 03:08 PM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
this is exactly what the command basename does

basename $FILEPATH

this will return 'readme'
 
Old 02-10-2006, 11:35 PM   #4
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
with just sed,

echo "/tmp/200/readme" | sed 's/^.*\///'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
A problem about a shell script jpan Linux - General 2 01-19-2005 06:14 PM
problem with sed in a bash script nexus55 Linux - Software 6 05-03-2004 09:40 PM
Shell-Script Problem? x4v013 Slackware 7 09-17-2003 12:15 AM
shell script problem steltner Programming 0 05-17-2003 03:26 PM


All times are GMT -5. The time now is 09:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration