LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2011, 05:44 AM   #1
varunimast
LQ Newbie
 
Registered: Sep 2011
Posts: 4

Rep: Reputation: Disabled
basename not working why?


In C-shell :
when typing..

foreach f ( ' ../trip/*.jpg' )
foreach? set name = ' basename $f .jpg '
foreach? echo $name
foreach? end
result is :
basename $f .jpg

why id he basename not working and is there a way through it?
 
Old 09-16-2011, 06:25 AM   #2
u111979
LQ Newbie
 
Registered: Apr 2010
Location: Basel
Distribution: Fedora
Posts: 1

Rep: Reputation: 0
Hi varunimast

there is an important difference in C-shells between double (") and single (') quotes. Within single quotes there is no variable replacement. Just try to replace your line

Quote:
foreach? set name = ' basename $f .jpg '
by

Quote:
foreach? set name = " basename $f .jpg "
and give another try.

Thomas
 
Old 09-16-2011, 06:35 AM   #3
varunimast
LQ Newbie
 
Registered: Sep 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for replying thomas.
But now its giving the result as :

basename ls a.jpg b.jpg .jpg

what must be done now?
 
Old 09-16-2011, 06:50 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You need command substitution: use backticks `
Code:
foreach? set name = `basename $f .jpg`
 
Old 09-16-2011, 06:53 AM   #5
varunimast
LQ Newbie
 
Registered: Sep 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
now the result is :

basename: extra operand `.jpg'

Actually , what I have found is that "basename" command is not working properly,
as when I applied it in another directory, it is not removing the directory path name.
Again.. what must be done?

Last edited by varunimast; 09-16-2011 at 07:01 AM.
 
Old 09-16-2011, 07:27 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by varunimast View Post
basename: extra operand `.jpg'
So you have a version of basename that does not accept/remove a suffix. Which system is this? Which version of basename?

In alternative you can try sed, e.g.
Code:
foreach? set name = `echo "$f" | sed 's/.*\///'`
 
Old 09-16-2011, 07:36 AM   #7
varunimast
LQ Newbie
 
Registered: Sep 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Smile

thank you colucix.
I have found the answer as suggested by you, by using the " backticks " not only
in the set command but also in the foreach command.
Now its working.
Thanks again
 
Old 09-16-2011, 07:48 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Great! I didn't notice that. Actually you should not use quotes at all in the foreach statement. This is a working version for me:
Code:
foreach f ( ../trip/*.jpg )
  set name = `basename $f .jpg`
  echo $name
end
 
  


Reply



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
Problem with basename mierdatuti Programming 4 05-16-2011 12:51 PM
basename naidu29 Linux - Software 11 06-02-2010 02:03 AM
“basename $0” returns basename: -k: unknown option jaideepca Linux - Newbie 6 11-09-2009 05:30 AM
How to get the 'basename' (or what other.?) command to do what I want..? craftybytes Programming 1 02-13-2008 06:27 AM
basename ovince Programming 2 03-21-2007 09:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:56 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration