LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-31-2006, 09:58 AM   #1
gudum35
LQ Newbie
 
Registered: Jun 2005
Posts: 9

Rep: Reputation: 0
Question 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
 
Old 05-31-2006, 11:51 AM   #2
scorbett
Member
 
Registered: May 2002
Location: Canada
Distribution: Slackware, Mandriva, RedHat
Posts: 46

Rep: Reputation: 15
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.
 
Old 05-31-2006, 12:56 PM   #3
gudum35
LQ Newbie
 
Registered: Jun 2005
Posts: 9

Original Poster
Rep: Reputation: 0
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?
 
Old 05-31-2006, 03:09 PM   #4
scorbett
Member
 
Registered: May 2002
Location: Canada
Distribution: Slackware, Mandriva, RedHat
Posts: 46

Rep: Reputation: 15
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).
 
Old 05-31-2006, 03:13 PM   #5
gudum35
LQ Newbie
 
Registered: Jun 2005
Posts: 9

Original Poster
Rep: Reputation: 0
No, I use tcsh on this machine
 
Old 05-31-2006, 03:43 PM   #6
scorbett
Member
 
Registered: May 2002
Location: Canada
Distribution: Slackware, Mandriva, RedHat
Posts: 46

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


Reply



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
How to change directory in a shell script? jdupre Linux - Newbie 13 06-25-2015 05:03 AM
script to change multiple filenames in a directory jeffreybluml Linux - Newbie 8 12-06-2006 01:46 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM
Script to change names of files in a directory geomonap Linux - General 2 12-03-2004 03:04 PM
script to change directory trout21 Linux - Newbie 9 02-17-2004 12:49 PM

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

All times are GMT -5. The time now is 11:01 AM.

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
Open Source Consulting | Domain Registration