Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to create aliases UNIX
I'm not sure if this is useful for some, but I use this shell script I wrote for creating and adding aliases to my .cshrc file all the time. I must have 2 or 3 dozen aliases in .cshrc file. Here's the shell script:
#!/bin/sh

echo -n "Enter the CMD you want to make an alias for: " ; read CMD ;
echo -n "Enter the alias without quotes: " ; read ALIAS ;

echo "alias" $CMD "'"$ALIAS"'" >> /Users/$USER/.cshrc

echo " "
tail -1 /Users/$USER/.cshrc

echo " "
echo "You need to relogin now!"
su -l $USER
Save this somewhere in your path, make it excecutable (chmod 755 script_name) and then type 'rehash' to activate it. Now simply type the name of the script when you wish to create a new alias.

[Editor's note: If you use the OS X convention of an "aliases.mine" file in ~/Library/init/tcsh, simply replace ".cshrc" with that path and filename on the two lines where it appears.]
    •    
  • Currently 4.00 / 5
  You rated: 4 / 5 (1 vote cast)
 
[3,772 views]  

A script to create aliases | 9 comments | Create New Account
Click here to return to the 'A script to create aliases' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
try to avoid need to re-login
Authored by: hayne on Jul 26, '02 12:08:32PM

It is not such a good idea to have a relogin as part of the script since if you use this script several times, you will build up many nested invocations of this script and nested login shells. Not such a big problem but a bit worrisome if you don't logout for weeks & weeks. (If you don't understand what I mean, try adding a line
echo "finished adding alias"
at the end of the script - you won't see the "finished ..." until you exit from the new shell you have created.)

You could avoid the need for re-login if you put the aliases in a separate file (.aliases or aliases.mine) since then all that would be needed to get the updated aliases is to 'source' the aliases file. But this source-ing of the aliases file can't be done from inside the script since that wouldn't affect the current shell. So you would need to use an alias to invoke the script and have the 'source' as part of the alias.

I also have a small quibble with the terminology used in the prompts. If I say:
alias copy cp
then the usual terminology is: "copy is an alias for the 'cp' command". Your prompts have this backwards.



[ Reply to This | # ]
try to avoid need to re-login
Authored by: agengler on Jul 26, '02 12:27:32PM

You could add a message to source ~/.cshrc and you won't need to login, or just add it to the code



[ Reply to This | # ]
Here is a python version
Authored by: buc40 on Jul 27, '02 01:21:22PM

Here is a python version taht does not require a login.

It does however require an aliases.mine file.

#!/usr/bin/env python
import os

user = raw_input("Enter username: ")
cmd = raw_input("Enter the CMD you want to make an alias for: ")
alias = raw_input("Enter the alias without quotes: ")
pound = raw_input("Enter a one line description for the alias: ")

file = "/Users/%s/Library/init/tcsh/aliases.mine" % user
output = "# %s n alias %s '%s' n" % (pound, cmd, alias)

fileout = open(file, "a")
fileout.write(output)
fileout.close()

See if it works for you.

Thanks.
SA



[ Reply to This | # ]
doesn't make the new alias immediately available
Authored by: hayne on Jul 29, '02 01:45:46AM

But, as far as I can see (not being very knowledgeable in Python), this script does not result in the new alias being immediately available. I.e. so that immediately after using the script you can make use of the new alias. That was the reason, I believe, for the re-login in the original script.



[ Reply to This | # ]
Just do this then ...
Authored by: buc40 on Jul 30, '02 10:05:22PM

No prob:#!/usr/bin/env python
import os

user = raw_input("Enter username: ")
cmd = raw_input("Enter the CMD you want to make an alias for: ")
alias = raw_input("Enter the alias without quotes: ")
pound = raw_input("Enter a one line description for the alias: ")

file = "/Users/%s/Library/init/tcsh/aliases.mine" % user
output = "# %s n alias %s '%s' n" % (pound, cmd, alias)

fileout = open(file, "a")
fileout.write(output)
fileout.close()

os.system("su %s" % user)


This of course leads a possible security opening open for abuse as
mentioned earlier.

Good Luck.
SA



[ Reply to This | # ]
Re; Just do this then ...
Authored by: hayne on Jul 31, '02 12:17:26AM

Sigh.
This version has exactly the same problem as the original script.
It starts a new (nested) shell and the Python script continues to run as long as you use that shell. If you did this several times, you would have several copies of this Python script running as well as several extra shells.



[ Reply to This | # ]
Re; Just do this then ...
Authored by: buc40 on Jul 31, '02 10:06:13AM

Why not just rehash then instead of su?

Will this work?

SA



[ Reply to This | # ]
Re; rehashing
Authored by: hayne on Jul 31, '02 01:39:37PM

rehashing updates the shell's list of executables.
It does not do anything with aliases.

The solution is relatively simple - you use an alias to perform multiple commands as I suggested earlier. (There is nothing that can be done in a shell script since what you are trying to change is the current shell's environment.)



[ Reply to This | # ]
A Simple Solution to the Re-Login Problem
Authored by: dcheck on Oct 16, '02 12:03:23PM

Okay, so the problem that people have identified with the script is that you need to relogin for it to take effect. But you don't -- all you have to do is take the command that it echoes:

alias listall 'ls -l'

or whatever, and then copy and paste it to the command line, and hit return. Not fully automated or elegant or anything, but certainly effective.

Dan



[ Reply to This | # ]