LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   basename not working why? (https://www.linuxquestions.org/questions/linux-newbie-8/basename-not-working-why-903332/)

varunimast 09-16-2011 05:44 AM

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?

u111979 09-16-2011 06:25 AM

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

varunimast 09-16-2011 06:35 AM

Thanks for replying thomas.
But now its giving the result as :

basename ls a.jpg b.jpg .jpg

what must be done now?

colucix 09-16-2011 06:50 AM

You need command substitution: use backticks `
Code:

foreach? set name = `basename $f .jpg`

varunimast 09-16-2011 06:53 AM

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?

colucix 09-16-2011 07:27 AM

Quote:

Originally Posted by varunimast (Post 4473504)
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/.*\///'`

varunimast 09-16-2011 07:36 AM

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 :)

colucix 09-16-2011 07:48 AM

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



All times are GMT -5. The time now is 03:29 AM.