LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Alias definition question (https://www.linuxquestions.org/questions/linux-general-1/alias-definition-question-44044/)

Dark_Helmet 02-04-2003 11:07 AM

Alias definition question
 
I'm using tcsh and I'd like an alias to perform some simple automation. There are application I run that need to be run from the same working directory (as they store configuration files in the directory they are launched from). So, I'd like this alias to save the directory I'm currently in, switch to the appropriate directory, launch the app, and then switch back to the original directory.

Yes, yes, I know I can easily accomplish what I need with a script, but I'd like to know if it's possible with an alias. So far, this is what I've played with:

alias do_foo 'setenv OLD_DIR `pwd`; cd $HOME/foo/bar; launch_foo & ; cd $OLD_DIR; unsetenv OLD_DIR'

The problem with that is, when the file is sourced, the shell tries to execute the commands one at a time which is just really bizarre. After I did some looking, I saw some other code (in a corporate-defined alias) used parentheses to surround the alias definition.

alias do_foo '(setenv OLD_DIR `pwd`; cd $HOME/foo/bar; launch_foo & ; cd $OLD_DIR; unsetenv OLD_DIR)'

Now when the file is source'd, I get an error about "Badly placed ()'s". Obviously a syntax or similar error because I'm throwing random punctuation into the mix...

Any suggestions? Is it possible the '&' is giving me grief?

P.S. I'm in the habit of jumping back and forth between bash and tcsh scripting... There is no '=' used for the setenv statements... removed them.

nxny 02-04-2003 01:52 PM

Dont know a thing about tcsh or why you're using it, but you may need to escape the ampresand with a backslash.

Here's the bash equivalent,
alias foo='OLDDIR=`pwd`; cd ~/bin; ./bar arg1 arg2 \& ; cd $OLDDIR; unset OLDDIR'

HTH

moses 02-04-2003 01:56 PM

You are making it much more complicated than it needs to be. . . =-}

Code:

alias tdir '(cd /tmp; ls)'
P.S.
Code:

cd -
will bring you to your previous directory. . .

Dark_Helmet 02-04-2003 02:10 PM

tcsh usage
 
I'm using tcsh for two reasons:
1. The syntax is more C-like in nature, making it easier for me to pick up shell scripting in general.
2. The stuff I'm trying to accomplish is to make my life easier at work, and the corporate environment provides tcsh only.

I've got bash and tcsh on my boxes at home, and to be hoest, they're a lot like programming languages in that once you know one, it's typically just a matter of vocabulary translation to get the same results from the other. What goes on under the hood may be an extreme contrast, but for my uses, they're very similar.

I'll try out the things you guys brought up. I really appreciate the help.

Dark_Helmet 02-04-2003 02:41 PM

Stupid reserved words!
 
I CANNOT believe that I could overlook something so trivial!

My main problem was my alias name. To test what I was coding, I named my alias "test". Bad, bad, bad, bad choice since test is a built-in function of the shell and is used quite liberally. That seems to be why the alias is executed when the file is sourced. Anyway, now that I've changed names, everything looks to be happy.

Thanks again guys.

Dark_Helmet 02-04-2003 02:48 PM

Coincidentally...
 
I should have mentioned this in the last message, but the parentheses are not needed. I'm not sure what the script I saw them in was trying to do...

Also, the '&' does not need to be escaped. Some day I'll have a complete grasp of when, where, how, and why the shell expands and interprets things...

and naming the alias properly helps too.


All times are GMT -5. The time now is 04:41 PM.