Page 1 of 1

Get only the filename in shellscripts

PostPosted: Wed Jan 05, 2011 10:14 am
by Endast
If you write a shell script and you want to know the filename and maybe not necessarily the full path, which hazel always sends in to your script ($1). You can use the command "basename".

So if you use "Run shell script" and typed in the following:
Code: Select all
LONG_FILENAME="$1"

echo "Name with full path: $LONG_FILENAME"
echo "Name with only filename: $(basename $LONG_FILENAME)"


If the rule matched the file /usr/local/bin/foo this would be the output:
Code: Select all
Name with full path: /usr/local/bin/foo
Name with only filename: foo


Hope it helps someone :)

Ps I have only used Hazel for a day, so if there is a better way please tell me :)

Re: Get only the filename in shellscripts

PostPosted: Wed Jan 05, 2011 12:31 pm
by sjk
And dirname is a companion to basename:
Code: Select all
dirname /a/b/c
/a/b

Re: Get only the filename in shellscripts

PostPosted: Fri Jan 07, 2011 1:08 am
by slappula
Great post Endast! I was just looking for this info last night and you posted this morning. I wanted to take it one step further and eliminate the file extension. (In my case to remove the .dvdmedia when converting a DVD rip using Handbrake CLI). Here's the Shell Script I used:

LONG_FILENAME="$1"

VARIABLE=$(basename "$LONG_FILENAME" .dvdmedia).m4v

/Applications/CLI/HandBrakeCLI -i "$1" --main-feature -o "$VARIABLE" --preset="AppleTV 2"

***This now works for filenames with spaces. I forgot to put quotes around the filename variables. It works fine now.***

I also found an easier way to do this:

/Applications/CLI/HandBrakeCLI -i "$1" --main-feature -o "${1%dvdmedia}"m4v --preset="AppleTV 2"

Courtesy of another post on this forum:

viewtopic.php?f=4&t=859&hilit=filename

Re: Get only the filename in shellscripts - 10.6

PostPosted: Fri Feb 18, 2011 1:25 pm
by macscript
I'm having a strange issue.

When I use the following code:

LONG_FILENAME="$1"

VARIABLE=$(basename "$LONG_FILENAME")

sed '/<?xml version="1.0" encoding="UTF-8"?>/d' "$1" > /path/to/the_folder/"$VARIABLE"

To remove the text string, it works flawlessly on OSX 10.5.8 client, but when I cut and paste it into a rig running 10.6.x SERVER, it fails. (yes, I change the paths)

The error is that "the_folder" is a directory, so it's not seeing "$VARIABLE" as a variable.

Anyone have any thoughts on this? I'm worried that something is wonky when you go to 10.6 or maybe it's server. hmm.

Re: Get only the filename in shellscripts

PostPosted: Fri Feb 18, 2011 3:16 pm
by macscript
Update:

I notice that my bash versions are different from 10.5 to 10.6

10.5.x = GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0)

10.6.x = GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)

Could someone verify that the basename variables work the same on their 10.6 rig?

Re: Get only the filename in shellscripts

PostPosted: Fri Feb 18, 2011 4:59 pm
by Mr_Noodle
Maybe try "/path/to/the_folder/$VARIABLE" instead.

Re: Get only the filename in shellscripts

PostPosted: Fri Feb 18, 2011 8:32 pm
by macscript
Thank you for the quick response, strangely no dice.

I've got some friends looking into it, but it looks like the newer bash might have some twists to it.

If anyone else can help, please chime in.

Re: Get only the filename in shellscripts

PostPosted: Fri Feb 18, 2011 9:16 pm
by sjk
Try running the script with "bash -x …" (on both systems, for comparison) to display expanded variables, etc. and possibly isolate where the problem is.

And maybe use ${VARIABLE} instead of $VARIABLE though I doubt it'll make any difference for this.

Re: Get only the filename in shellscripts

PostPosted: Fri Feb 18, 2011 11:55 pm
by macscript
I got this reply over on an Apple forum and tried it in Hazel and it doesn't seem to work on 10.6. Could this be tested?

<<<<<<<------------>>>>>>>

Not sure how much this helps but the following code works on a 10.6.6. client.

#!/bin/bash

LONG_FILENAME="$1"

VARIABLE=$(basename "$LONG_FILENAME")

echo $VARIABLE

echo "hello hello" > ~/Temporary\ Items/"$VARIABLE"


The version of bash is:
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)

which basename
returns /usr/bin/basename

<<<<<<<------------>>>>>>>

From my testing with a simple echo command, I'm not getting the variable picked up. I can output $1 and all is well, but outputting $VARIABLE (or whatever I choose to call it) doesn't seem to work.

For now, I'm running it in 10.5 and I'll keep testing. Thanks.

Re: Get only the filename in shellscripts

PostPosted: Thu Feb 24, 2011 1:34 pm
by macscript
Uninstalled Hazel and reinstalled and it works now.

Thank you for the quick responses and info.

Great app.

Re: Get only the filename in shellscripts

PostPosted: Fri Mar 04, 2011 4:04 pm
by Mr_Noodle
I don't know reinstalling would have fixed it but if it's fixed, I'm not going to argue. :wink: