LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Script to change directory (https://www.linuxquestions.org/questions/linux-general-1/script-to-change-directory-450235/)

gudum35 05-31-2006 09:58 AM

Script to change directory
 
Hi all,

I wrote a little script that greps a directory path in a file and echos it:

Code:

# Script jobdir
jobdir=$(grep $1 ~/jobs_summary.txt | cut -c54-0)
echo $jobdir

but what if I want to cd to $jobdir? I tried several things like executing the script as

Code:

$ . jobdir arg
without succes: /usr/bin/. says permission denied and source doesn't work either. The easiest way i found up to now is

Code:

cd `jobdir arg`
but I would really like to get rid of the backticks. I'd rather a command like

Code:

cdjobdir arg
but I can't find out how to do this? Any ideas?

Thanks

scorbett 05-31-2006 11:51 AM

How about alias mycommand="`jobdir $1`"

Then you can just type "mycommand arg" without the quotes and you're set. You might want to pick a better name other than "mycommand" but you get the idea.

gudum35 05-31-2006 12:56 PM

Quote:

Originally Posted by scorbett
How about alias mycommand="`jobdir $1`"

Thanks, I didn't know that it was possible to invoke arguments in aliases. I tried
Code:

alias cdjobdir 'cd `jobdir $1`'
but when I run the command
Code:

$ cdjobdir arg
I get
Code:

cd: Too many arguments.
Any other ideas?

scorbett 05-31-2006 03:09 PM

Quote:

Originally Posted by gudum35
Any other ideas?

Are you using bash? In bash, aliases are not as powerful as in some other shells (like tcsh), and they don't handle arguments very well. Fortunately there is an alternative - you can use a bash function to do the same thing. Type this:

Code:

cdjobdir () { cd `jobdir $1`; }
Once you've typed that, you should be able to do this:

Code:

cdjobdir argument
I've tested it a bit here with a dummy script standing in for your jobdir script, and it works perfectly in bash. (You'll probably have to unalias cdjobdir before you enter the function - I don't think you can have a function and an alias with the same name at the same time).

gudum35 05-31-2006 03:13 PM

No, I use tcsh on this machine

scorbett 05-31-2006 03:43 PM

Quote:

Originally Posted by gudum35
No, I use tcsh on this machine

Huh - I used to use tcsh all the time and I could have sworn it had the capability of accepting arguments in aliases. However, I just tested here on tcsh and sure enough, I can't get it to work. I think you might be stuck typing in those backticks! Sorry for the lack of help.


All times are GMT -5. The time now is 06:09 PM.