Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > symbolic links, aliases, cls clear

Reply
Thread Tools

symbolic links, aliases, cls clear

 
 
mp
Guest
Posts: n/a
 
      03-29-2006
i have a python program which attempts to call 'cls' but fails:

sh: line 1: cls: command not found

i tried creating an alias from cls to clear in .profile, .cshrc, and
/etc/profile, but none of these options seem to work.

my conclusion is that a python program that is executing does not use
the shell (because it does not recognize shell aliases). is this
correct?

should i use a symbolic link? if so, where should i place it?

what is the difference between aliases and symbolic links?

if i execute a command like 'clear' to clear the screen, where does the
shell look to find the command 'clear'?

i'm using os x.

thanks
mp

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-29-2006
"mp" <> writes:
> i have a python program which attempts to call 'cls' but fails:
>
> sh: line 1: cls: command not found
>
> i tried creating an alias from cls to clear in .profile, .cshrc, and
> /etc/profile, but none of these options seem to work.
>
> my conclusion is that a python program that is executing does not use
> the shell (because it does not recognize shell aliases). is this
> correct?


Yes.

> should i use a symbolic link? if so, where should i place it?


You could, but I don't think it's the best solution.

> what is the difference between aliases and symbolic links?


Aliases exist only in a shell. Symbolic links exist in the file
system.

> if i execute a command like 'clear' to clear the screen, where does the
> shell look to find the command 'clear'?


Generally it searches $PATH for an executable file called "clear".

I don't know Python very well (note the cross-post), but if it
provides a way to detect which operating system you're running on, you
could execute "cls" if you're on Windows, or "clear" if you're on a
Unix-like system. Or there might be some Python library with a
clear-screen function.

Are you sure you want to clear the screen? If I run your program and
it clears my screen for me, it could be erasing significant
information. If you want complete control over the screen, you should
probably use something like curses or ncurses (there may be a Python
interface to it).

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Chris F.A. Johnson
Guest
Posts: n/a
 
      03-29-2006
On 2006-03-29, mp wrote:
> i have a python program which attempts to call 'cls' but fails:
>
> sh: line 1: cls: command not found
>
> i tried creating an alias from cls to clear in .profile, .cshrc, and
> /etc/profile, but none of these options seem to work.


Why not call 'clear', since 'cls' does not exist?

> my conclusion is that a python program that is executing does not use
> the shell (because it does not recognize shell aliases). is this
> correct?


Even shell scripts do not normally expand aliases.

> should i use a symbolic link? if so, where should i place it?
>
> what is the difference between aliases and symbolic links?


What's the difference between a raven and a writing desk?

> if i execute a command like 'clear' to clear the screen, where does the
> shell look to find the command 'clear'?


In a directory listed in the PATH variable.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      03-30-2006
"mp" <> wrote:
# i have a python program which attempts to call 'cls' but fails:
#
# sh: line 1: cls: command not found
#
# i tried creating an alias from cls to clear in .profile, .cshrc, and
# /etc/profile, but none of these options seem to work.
#
# my conclusion is that a python program that is executing does not use
# the shell (because it does not recognize shell aliases). is this
# correct?

Shell command alias or Mac OSX file alias, like a Finder Make Alias?

A file alias is similar to a soft link except it has both path
and device/inode like information so that it can still identify
a renamed file. However aliasses are handled at the toolbox level
instead of the kernel, so that unix only code cannot resolve them.


// readalias - Resolve Finder alias files to the actual
// file, if it can be found. The interface is modelled
// after readlink. If the original is not alias
// or could not be resolved, return 0 and set errno.
// Otherwise return a mallocked string with the
// actual file path; caller must free.
//
// On non-macintosh systems, this always returns an
// error.

#ifdef ALIAS
#include <CoreFoundation/CoreFoundation.h>
#include <ApplicationServices/ApplicationServices.h>

static char *readalias(char *original) {
int ec = 0;
CFStringRef path = 0;
CFStringRef resolvedPath = 0;
CFURLRef url = 0;
CFURLRef resolvedUrl = 0;
FSRef fsRef;
char *s = 0;
if (!(path=CFStringCreateWithCString(NULL,original,kC FStringEncodingUTF)) {
ec = EFAULT; goto exit;
}
if (!(url=CFURLCreateWithFileSystemPath(NULL,path,kCF URLPOSIXPathStyle,0))) {
ec = EFAULT; goto exit;
}
if (!CFURLGetFSRef(url,&fsRef)) {
ec = ENOENT; goto exit;
}
Boolean targetIsFolder,wasAliased;
if ((ec=FSResolveAliasFile(&fsRef,true,&targetIsFolde r,&wasAliased))) {
goto exit;
}
if (!wasAliased) {
ec = EINVAL; goto exit;
}
if (!(resolvedUrl=CFURLCreateFromFSRef(NULL,&fsRef))) {
ec = EFAULT; goto exit;
}
if (!(resolvedPath = CFURLCopyFileSystemPath(resolvedUrl,kCFURLPOSIXPat hStyle))) {
ec = EFAULT; goto exit;
}
s = (char*)CFStringGetCStringPtr(resolvedPath,kCFStrin gEncodingUTF;
if (s) {
s = strcpy(malloc(strlen(s)+1),s);
}else {
int n = 3*CFStringGetLength(resolvedPath) + 1; s = malloc(n);
if (CFStringGetCString(resolvedPath,s,n,kCFStringEnco dingUTF) {
s = realloc(s,strlen(s)+1);
}else {
ec = EFAULT; goto exit;
}
}
exit:
if (path) CFRelease(path);
if (resolvedPath) CFRelease(resolvedPath);
if (url) CFRelease(url);
if (resolvedUrl) CFRelease(resolvedUrl);
if (ec) {
if (ec<0) ec = ENOENT;
errno = ec; free(s); s = 0;
}
return s;
}
#else
static char *readalias(char *original) {
errno = EINVAL;
return 0;
}
#endif

--
SM Ryan http://www.rawbw.com/~wyrmwif/
But I do believe in this.
 
Reply With Quote
 
Michael Paoli
Guest
Posts: n/a
 
      03-30-2006
mp wrote:
> i have a python program which attempts to call 'cls' but fails:
> sh: line 1: cls: command not found
> i'm using os x.


[Note Followup-to: severely trimmed]

I'd guestimate (those more familiar with python can probably fill in
more relevant python specific details) that the python program is
probably doing something like:
system("cls")
.... or whatever the python-specific way of writing something like that
is. Most likely that was written, intended for some Microsoft
DOS/Windows/XP or similar type of operating system - where CLS is a
legitimate command (to clear the screen). On UNIX (and probably also
OS X), there is no "cls" as a standard command, but there is the quite
standard command "clear", to clear the "terminal" screen.

In the land of UNIX, most languages implement the system() function or
equivalent, by typically fork(2)ing and exec(3)ing (at least one of the
exec(3) family of calls), generally invoking "the" or a default shell
(typically /bin/sh) with a first option argument of "-c", and then the
argument to system passed as one single string as the other argument to
the shell. It would seem likely that python was "smart enough" (of
course) to know on UNIX to implement system() as sh -c ..., rather than
something like COMMAND /C ... or CMD /C ... as it would likely do on
DOS/Windows/XP or similar. But "of course" python probably has no clue
what the cls is that's handed to it with system(), and likely just
blindly passes it on to the shell. The diagnostic you got would also
seem to imply that's what happened (the shell (sh) couldn't find the
cls command). ... as a matter of fact, if I try that on Debian
GNU/Linux 3.1 (technically not "UNIX", but neither is OS X, but for
practical purposes they're both quite sufficiently close), I get
results that would appear exceedingly consistent with the hypothesis I
put forth:
$ sh -c cls
sh: line 1: cls: command not found

If it's desired to have the python program function as close to its
(apparent) original intent as feasible, it may be desirable to:
have it test the operating system, and if it is UNIX or similar, use
clear, instead of cls ... or if one wants to port/adapt it to UNIX
(and OS X, etc.), with no need or intention to move it back and forth
or among significantly different operating systems, then perhaps
consider simply replacing the system(cls) with system(clear), or
whatever the precise suitable change in the python code would be.
It would probably also be worth inspecting the code for other
occurrences of system() that may also need to be adjusted or changed.

Note also that some languages (e.g. Perl) will potentially take
shortcuts with the system() function. For example, with Perl
(paraphrasing and perhaps over-simplifying a bit) if Perl sees no
need or reason to have to use the overhead of the shell to invoke the
system() function, it will just quite directly (after the fork(2))
exec(3) the command, setting the argument(s) suitably. Python may
(or may not) try similar shortcuts. For example, CLS, on DOS, etc.,
is internal to the "shell" (command interpreter), so, if python
didn't find an external CLS command, it would have to pass it to the
"shell", hoping the shell would know what to do with it. That would
happen to work with DOS, but would generally fail on UNIX (where cls
would generally not exist as a command, and wouldn't be built-in to
the shell).

 
Reply With Quote
 
Pankaj
Guest
Posts: n/a
 
      04-04-2006
its true for the processes, gone to the system mode and from there the
process is not considering the shell variable any more.

In case of deamons also it is the same case. For Phython programs, I am
not sure , may they are also nohops, so might be not accessing the
shell variables. But i think you have defined the alias to some other
place.... the error is reported by shell that cls not found.

This could be investigated if you do some tests with the system. Like
alias is shell script you can open it and read it , how it invokes the
command for creating aliases. etc etc.

Alias: When the shell is in picture it directly replaces the string
"cls" to "clear" and execute the clear.

Symbolic links: are reference to the inode of the actual files, so it
should work in your case.

just run
#which clear
(O/P , you will get the location of binary say /usr/bin/clear)

Now you can create the symbolic link named as cls at location
"/usr/bin". this symbolic link should be a soft link to /usr/bin/clear.
you can also put the symbolic links to any of the location that is
displayed by $PATH variable.

for any command you execute in shell, it functions in following order:
(Not exactly the same, need to confirm, i might miss some of the steps)

1) check with the shell variable(whether any alias to this command
exists or not!)
2) search it in the locations $PATH and execute the command is it is
found.
so on...

 
Reply With Quote
 
af.dingo@gmail.com
Guest
Posts: n/a
 
      04-12-2006
If I may recommend an alternative,

print "\033[H\033[J"

the ansi sequence to clear the screen.

 
Reply With Quote
 
Floyd L. Davidson
Guest
Posts: n/a
 
      04-12-2006
wrote:
>If I may recommend an alternative,
>
>print "\033[H\033[J"
>
>the ansi sequence to clear the screen.


Or so you would hope (however, that is *not* what you have listed!).

Unfortunately, it is poor practice to hard code such sequences.
Instead the proper sequence should be obtained from the
appropriate database (TERMINFO or TERMCAP), and the easy way to
do that is,

tput clear

--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)
 
Reply With Quote
 
Floyd L. Davidson
Guest
Posts: n/a
 
      04-12-2006
"mp" <> wrote:
>i have a python program which attempts to call 'cls' but fails:
>
>sh: line 1: cls: command not found


Hmmmmm... (I don't program in Python, so precisely what is
happening isn't something I'm sure about).

But, note how that line starts with "sh:"! That indicates it is
/bin/sh which is reporting an inability to find a "cls"
command. It suggests that Python (like most other programming
languages) calls shell scripts using /bin/sh as the default
shell.

The problem is that unix has no command named "cls".

>i tried creating an alias from cls to clear in .profile, .cshrc, and
>/etc/profile, but none of these options seem to work.


In /etc/profile and ~/.profile you will cause an alias to be
defined *for* *interactive* *login* *shells*. But not for
non-interactive non-login shells, which is what you are invoking
with Python. The reason is because aliases are not inherited by
sub-shells, and only interactive login shells read those two
files.

Hence your alias is never defined in the subshell your program
executes.

>my conclusion is that a python program that is executing does not use
>the shell (because it does not recognize shell aliases). is this
>correct?


No.

>should i use a symbolic link? if so, where should i place it?


No.

>what is the difference between aliases and symbolic links?


Aliases are a mechanism used by a shell to define a command name
that executes a series of commands. A symbolic link is a
directory entry that de-references another directory entry, so
that either entry points to the same actual file.

>if i execute a command like 'clear' to clear the screen, where does the
>shell look to find the command 'clear'?


It looks in locations specified by the PATH variable. By
default that will be a minimal list defined by the login
program, but it might be significantly added to in the
/etc/profile or other shell init scripts.

The question you need to answer first is what happens if your
Python program tries to execute /clear/ rather than /cls/. If
that works, then your PATH variable is set correctly. If it
doesn't work, verify that there is in fact a program named
/clear/ that can be run from a shell command line. Then figure
out how to set an appropriate PATH variable for your Python
program.

Note that if /clear/ does work, but you want this script to use
/cls/ so that it is portable to some silly OS where a /cls/
exists... You can define a shell function (which will be
inherited by sub-shells) to look like this,

function cls () {
clear;
}

And place it where ever is appropriate (/etc/profile is one place).

>i'm using os x.


I don't know anything about it...

--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)
 
Reply With Quote
 
Michael Paoli
Guest
Posts: n/a
 
      04-12-2006
Floyd L. Davidson wrote:
> wrote:
> >If I may recommend an alternative,
> >print "\033[H\033[J"

> Unfortunately, it is poor practice to hard code such sequences.
> Instead the proper sequence should be obtained from the
> appropriate database (TERMINFO or TERMCAP), and the easy way to
> do that is,
> tput clear


Or clear(1), as also mentioned earlier. Yes, definitely don't want to
hardcode the sequence. Definitely do use the appropriate terminal
capabilities database (terminfo or termcap) in the appropriate manner
(e.g. clear(1) or tput clear will handle that in the simple case of
shell accessible means to clear the screen).

Most UNIX(/LINUX/BSD/...) implementations support a large number of
terminal types. E.g. on my system, I check and find that there are
1470 unique terminal types (descriptions) supported - and that's not
including multiple aliases for the same terminal type/description (but
it does count distinct names/files which have differing
configurations, even if they are for the same terminal - such as
changing certain options or behavior of a terminal, or using the
terminal in distinct modes). Among those terminal types on my system,
I find 154 distinct means of clearing the screen. Just for
illustrative purposes, here are the top 10 I find, with count of how
many distinct types (descriptions) use that particular sequence:
236 clear=\E[H\E[J,
120 clear=^L,
120 clear=\E[H\E[2J,
64 clear=\EH\EJ,
61 clear=\E[2J,
42 clear=\E[H\E[J$<156>,
38 clear=^Z,
36 clear=\E[H\E[J$<50>,
31 clear=\E[H\E[J$<40>,
29 clear=\E[2J\E[H,
And of course, sending the wrong sequence (e.g. like trying some to
see what works) can be highly problematic - it can do very nasty
things to some terminals. E.g. I own one terminal, which among
sequences it supports, is one which effectively says interpret the
following hexadecimal character pairs as bytes, load them into RAM,
and execute them - a relatively sure-fire way to crash the terminal if
it is sent garbage (I used to run into that and other problems with
some BBS systems that would presume everyone must be running something
ANSI capable or that it was safe to do other tests such as see if
certain sequences would render a blue square on one's screen).

references:
"system" call/function, in various programming languages
clear(1)
tput(1)
terminfo(5)
termcap(5)
news: ups.com
news: oups.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abend with cls.__repr__ = cls.__str__ on Windows. J Peyret Python 10 03-19-2011 07:09 AM
Argument type 'CrystalDecisions.Web.CrystalReportViewer' is not CLS-compliant TS ASP .Net 1 01-25-2006 03:13 AM
system("clear"); vs. system("cls"); Buteo Lagopus Perl Misc 3 07-17-2003 11:09 AM
Re: When is virtual fn def in base cls required? Josephine Schafer C++ 0 06-25-2003 06:29 AM
Re: When is virtual fn def in base cls required? Victor Bazarov C++ 0 06-24-2003 04:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57