LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-04-2003, 12:07 PM   #1
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.

Last edited by Dark_Helmet; 02-04-2003 at 12:10 PM.
 
Old 02-04-2003, 02:52 PM   #2
nxny
Member
 
Registered: May 2002
Location: AK - The last frontier.
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771

Rep: Reputation: 30
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
 
Old 02-04-2003, 02:56 PM   #3
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 49
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. . .

Last edited by moses; 02-04-2003 at 02:57 PM.
 
Old 02-04-2003, 03:10 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Original Poster
Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 02-04-2003, 03:41 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Original Poster
Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 02-04-2003, 03:48 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Original Poster
Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Alias question (2 commands for one alias) gflores Linux - Newbie 3 01-21-2006 01:40 AM
arguments in alias definition bluefire Linux - General 4 11-23-2004 06:48 AM
Red Hat 9 h.c.l. modem definition question. Hal Linux - Hardware 2 01-16-2004 05:07 PM
Two C question: definition of veriable and powerplane Programming 4 05-01-2003 11:59 PM
alias question tied2 Linux - General 4 08-23-2002 11:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration