Identify an alias?

In a bash script I can identify a symlink (-h or -L) and hard links (f1 -ef f2) but I can't seem to find how to identify an Aqua alias. Is it even possible?

In Python I would use FSResolveAliasFile(dlfpath, 1)[2] but I don't know how to translate that to a bash script.

Thank you,
Lee C

PS: What is the difference (if any) between the bash file test operators -h and -L?

Posted on Sep 22, 2005 9:32 PM

Reply
11 replies

Sep 22, 2005 11:21 PM in response to Lee Cullens

Parse the output of /Developer/Tools/GetFileInfo (part of the XCode suite). In the "attributes" field, "A" indicates an Aqua alias, "a" indicates a real Aqua file (a "real" file or a unix symlink). Not sure how to find its target that way.

Alternately, you could probably use one of the command-line utilities in the osxutils suite: "lsmac" appears to resolve Aqua aliases to their targets but not unix symlinks.

Or since you already know how to do [whatever you want] in python, why not use a python one-liner in your bash script?
python -c "some command" where some command either prints a result (and you process the output) or sets an exit code (and you process $? or somesuch).

According to the "test" manpage (on OS X 10.3):
<pre>
-L file True if file exists and is a symbolic link.
-h file True if file exists and is a symbolic link. This
operator is retained for compatibility with
previous versions of this program. Do not rely on
its existence; use -L instead.
</pre>

Sep 22, 2005 11:44 PM in response to Lee Cullens

Lee,
I really hope there is a better way, but here is one which uses AppleScript imbeded in your bash script:

#!/bin/bash
myalias="/Users/wileecyo/Library/Favorites/Home"
echo $myalias >/tmp/myalias
osascript <<EOF
set myalias to (do shell script "cat /tmp/myalias")
set myfile to (POSIX path of (POSIX file myalias as text))
do shell script "echo " & myfile & ">/tmp/myalias"
EOF
myfile=`cat /tmp/myalias`
rm /tmp/myalias
echo $myfile

Hopefully you can follow what it's doing.
Here is the same script with all the AS code stuffed into one line:

#!/bin/bash
myalias="/Users/wileecyo/Library/Favorites/Home"
echo $myalias >/tmp/myalias
#next should be one line
osascript -e 'do shell script "echo " & (POSIX path of (POSIX file (do shell script "cat /tmp/myalias") as text)) & ">/tmp/myalias"'
#end one line
myfile=`cat /tmp/myalias`
rm /tmp/myalias
echo $myfile

As I said, I hope there is a better way and that someone will me ๐Ÿ™‚

Reese

Sep 23, 2005 3:04 AM in response to reese_

Hi Reese,

> As I said, I hope there is a better way and that someone
will me ๐Ÿ™‚


Sorry, no better way has been posted here using the tools at hand. Michael Conniff and I discussed this in the thread, User id cleanup. The alias is an Apple thing so it would have to be an Apple tool or one designed specifically for Mac OS.

Third party tools are a slightly different matter. I like Sveinbjorn Thordarson's osxutils. I like "fileinfo" better than Apple's "GetFileInfo". It reports words for Finder flags, one of which is "Alias". It also report four different kinds of dates and has a nice semi-graphical representation of permissions. More appropriate for you needs is his "lsmac", which lists Aliases just like "ls" lists symlinks. Unfortunately, it follows real symlinks but Tiger comes with "readlink" for that. Fink will install the osxutils.
--
Gary
~~~~
Old men are fond of giving good advice to console
themselves for their inability to set a bad example.
-- La Rochefoucauld, "Maxims"

Sep 23, 2005 10:10 AM in response to Lee Cullens

Thank you Daniel, Reese and Gary,

-L vs. -h; Hmmm, rtfmp, so burried in my books I don't look at the obvious source, sorry.

I was trying to avoid the Python one-liner for overhead reasons. I wanted to cut out the "middle man" and access the appropriate Carbon/Cocoa FS utility directly. In the man pages it shows such things as an include and fs utility call, but I can't seem to make one work in a Bash script.

I'm working on a file system clone verification tool for my article (including the newly OS X introduced EA and ACL data), thus the "overhead" concerns. Never used AS, but the same idea applies, in addition to the verboseness ๐Ÿ™‚

I was also trying to avoid awking ls and osxutils output, but such may be the choice I'm left with ๐Ÿ˜Ÿ

Thanks again all,
Lee C

Sep 23, 2005 11:06 AM in response to Lee Cullens

I wanted to cut out the "middle man" and access the appropriate Carbon/Cocoa FS utility directly. In the man pages it shows such things as an include and fs utility call, but I can't seem to make one work in a Bash script.

Sounds like you're reading manpages for C functions, not command-line stuff. It's true, there is a huge API for accessing this stuff from C (or ObjC or whatever)...that's how compiled programs like osxutils and python and the MacOSX::File perl module do it.

Sep 23, 2005 12:19 PM in response to Daniel Macks

Thanks again Daniel,

Yea, that's what I was afraid of - no direct "hook" for Bash ๐Ÿ˜Ÿ

I suppose I ought to go back to Python for this - a user can always invoke the Python module from Terminal if they don't want to get into Python otherwise. I was thinking eventually of a FS GUI utility in Python - along the lines of dev's FileMerge and MidnightCommander, but completely HFS+ friendly for us Mac folks. even have it expandable to a three+ pane utility. So maybe I ought to wrap up the current effort on my article with the inclusion of an rdiff-backup example, then switch gears back to Python.

Lee C

Sep 24, 2005 6:03 PM in response to Lee Cullens

For use within a script I can get just about everything I need between dev's GetFileInfo and xattr.

In all my searching though, I remember seeing where I could get a link count (as in identifying hard links - more than one reference to a file), but I've lost that particular reference and can't seem to find it again.

Can anyone keep me from going crazy by noting where I might have seen such?

What I'm thinking of doing for convenience and optimization in the script is going back to C to write a custom CLI. It's been more than twenty years since I last used C, but I'm finding the difficult part is locating the Developer docs for doing such properly. I'm still reading, but if someone can point me at the relevant Dev docs - please do ๐Ÿ™‚ Otherwise I'll just do a C module for use with Python.

Thanks,
Lee C

Sep 24, 2005 6:26 PM in response to Lee Cullens

Hi Lee,
This might drive you crazy because of the simplicity of the answer but I hope not. ๐Ÿ™‚ The link count is the second field in the long listing of the "ls" command. I think they are hard links although the '.' "directory" at the top level of each directory and the ".." "directory" in each subdirectory are all counted in the list of links. I don't honestly know how it's computed.
--
Gary
~~~~
You know your apartment is small...
when you can't know its position and velocity at the same time.
you put your key in the lock and it breaks the window.
you have to go outside to change your mind.
you can vacuum the entire place using a single electrical outlet.

Sep 24, 2005 7:10 PM in response to Gary Kerbaugh

Gary,

Yes %) just one more example of how I get so involved I miss the obvious. It also reminds me of the problem I had with such. The count is easily understood for files, but for folders there are various other HFS shenanigans going on.

What I'm trying to remember though, was accessing the count directly without having to awk ls output.

I looked back at one of my Python utilities and saw where I included the alias test from Carbon.File, Doing a help on Carbon.File I see quite a bit of other info I can access also. Now I'm wondering just what other Carbon apis there are for Python - but I know a good source to ask that question of ๐Ÿ™‚

Since I eventually want to develop a complete FS utility with a GUI (something along the line of MC, but full blown HFS+ like the lesser dev FileMerge) I'm getting back to Python. I can more easily add a C module where needed there also.

Lee C

Sep 24, 2005 9:49 PM in response to Gary Kerbaugh

Gary,

Ah yes, not what I remember seeing, but more straightforward than awking ls ๐Ÿ™‚

Chinook ~/Documents/Personal 503 $nx=`stat -f "%l" AGS0520.rtf`
Chinook ~/Documents/Personal 504 $echo $nx
2

Fairly complete also, but %f doesn't seem to include EAs, let alone ACLs - just dreaming :<))

Chinook ~/Documents/Personal 507 $touch AGS0520.rtf
Chinook ~/Documents/Personal 508 $xattr --set type test AGS0520.rtf
Chinook ~/Documents/Personal 509 $xattr --set priority low AGS0520.rtf
Chinook ~/Documents/Personal 510 $xattr --list AGS0520.rtf
AGS0520.rtf
com.apple.FinderInfo
priority low
type test
Chinook ~/Documents/Personal 511 $nx=`stat -f "%f" AGS0520.rtf` Chinook ~/Documents/Personal 512 $echo $nx
0
Chinook ~/Documents/Personal 513 $

Thanks,
Lee C

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Identify an alias?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.