473,732 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

symbolic links, aliases, cls clear

mp
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

Mar 29 '06 #1
22 4230
"mp" <ma*********@em ail.com> 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_Keit h) ks***@mib.org <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.
Mar 29 '06 #2
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
Mar 29 '06 #3
"mp" <ma*********@em ail.com> 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 <CoreFoundati on/CoreFoundation. h>
#include <ApplicationSer vices/ApplicationServ ices.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=CFStrin gCreateWithCStr ing(NULL,origin al,kCFStringEnc odingUTF8))) {
ec = EFAULT; goto exit;
}
if (!(url=CFURLCre ateWithFileSyst emPath(NULL,pat h,kCFURLPOSIXPa thStyle,0))) {
ec = EFAULT; goto exit;
}
if (!CFURLGetFSRef (url,&fsRef)) {
ec = ENOENT; goto exit;
}
Boolean targetIsFolder, wasAliased;
if ((ec=FSResolveA liasFile(&fsRef ,true,&targetIs Folder,&wasAlia sed))) {
goto exit;
}
if (!wasAliased) {
ec = EINVAL; goto exit;
}
if (!(resolvedUrl= CFURLCreateFrom FSRef(NULL,&fsR ef))) {
ec = EFAULT; goto exit;
}
if (!(resolvedPath = CFURLCopyFileSy stemPath(resolv edUrl,kCFURLPOS IXPathStyle))) {
ec = EFAULT; goto exit;
}
s = (char*)CFString GetCStringPtr(r esolvedPath,kCF StringEncodingU TF8);
if (s) {
s = strcpy(malloc(s trlen(s)+1),s);
}else {
int n = 3*CFStringGetLe ngth(resolvedPa th) + 1; s = malloc(n);
if (CFStringGetCSt ring(resolvedPa th,s,n,kCFStrin gEncodingUTF8)) {
s = realloc(s,strle n(s)+1);
}else {
ec = EFAULT; goto exit;
}
}
exit:
if (path) CFRelease(path) ;
if (resolvedPath) CFRelease(resol vedPath);
if (url) CFRelease(url);
if (resolvedUrl) CFRelease(resol vedUrl);
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.
Mar 30 '06 #4
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).

Mar 30 '06 #5
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(whethe r 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...

Apr 4 '06 #6
If I may recommend an alternative,

print "\033[H\033[J"

the ansi sequence to clear the screen.

Apr 12 '06 #7
af******@gmail. com 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) fl***@apaflo.co m
Apr 12 '06 #8
"mp" <ma*********@em ail.com> 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) fl***@apaflo.co m
Apr 12 '06 #9
Floyd L. Davidson wrote:
af******@gmail. com 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:11******** *************@i 39g2000cwa.goog legroups.com
news:11******** **************@ v46g2000cwv.goo glegroups.com

Apr 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3792
by: ozarks | last post by:
I want to setup a database symbolic link within Windows XP to a network drive which has significanlty more space available and can't seem to get it working. Here is my setup.... - I am running MySQL 4.1.7 - I have tried this with both the 'mysqld-max' and 'mysqld-max-nt' servers - Per the MySQL manual (section 7.6.1.3) I setup a 'foo.sym' file in
0
1292
by: Mark Everett | last post by:
Hi, I am runnign the latest version of mysql and I desperately need to move one of my tables from a database so that it is stored on another drive. I have read the symbolic links reference but it seems to me that I can relocate a database but not a table. Which is not useful to me at all. I have tried anyhow without any success.
15
2451
by: jacob navia | last post by:
Recently, we had a very heated thread about GC with the usual arguments (for, cons, etc) being exchanged. In one of those threads, we came into the realloc problem. What is the realloc problem? Well, it begins with a successfull realloc: char *q = realloc(p,2*n); // for n size_t, a simple
4
3374
by: TK | last post by:
Hi, I have to know whether a typical file (under Linux) a symbolic link is (in the sense of the stat()-Funktion). How can I check it excactly? Thanks for help. o-o THomas
0
1386
by: humbleaptience | last post by:
Hey Ya'll, I basically have a bunch of unix servers with a dataset scrambled through about 10 of them. On unix, I maintain a symbolic link file tree so that for each server it appears to have access to all the files through auto mounting and so forth. I want to be able to access this same file tree from my asp.net web app and NFS - however, it's not working and I've tried everything. Microsoft Services for Unix just doesn't cut it...
0
1576
by: xhunga | last post by:
* Symbolic computation with the language c : The Derivative step by step. (Windows,Linux) You can find some examples into the file a_exampl.txt @ @
1
3490
by: Remote_User | last post by:
Hi All, .NET 1.1 doesn't support creating symbolic links to files, like in Linux? Can i use C# to make a file point to another file? Thanks.
1
8358
by: amygrant1701 | last post by:
Hi, I've done this before so I don't see what I could doing wrong here. I'm running mysql 5x on freebsd. I'm using the default data directory of "/var/db/mysql" In there I have several dozen mysql datasbases that are functioning perfectly. I am trying to add a database which will be stored on a different drive, therefore the entry in "/var/db/mysql" should be a symbolic link
2
2868
by: Desmodromic | last post by:
Informix recommend that symbolic links be used for chunks. These links are then used in the dbspace definition. Does this advice hold for DB2? The equivalent would be to create symbolic links for containers and use the link name when defining tablespaces. Wouldn't this allow containers to be physically moved/ renamed without having to use db2relocatedb? I haven't tested this but couldn't you just remove the link, move the container,...
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.