LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 06: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, 07: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, 07: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, 07:50 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You need command substitution: use backticks `
Code:
foreach? set name = `basename $f .jpg`
 
Old 09-16-2011, 07: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 08:01 AM.
 
Old 09-16-2011, 08:27 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 08: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, 08:48 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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


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


All times are GMT -5. The time now is 03:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration