LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Little bash script and file to give long (or any) directories short names (http://www.linuxquestions.org/questions/programming-9/little-bash-script-and-file-to-give-long-or-any-directories-short-names-770898/)

WardXmodem 11-22-2009 11:45 PM

Little bash script and file to give long (or any) directories short names
 
WAaaaaaaaay back when I had my first CP/M machine, and expanded it with a (wow, 8M - yes M) hard drive, it was too easy to lose things, so I invented "g".

I was into 1-letter commands ;-)

g is a small executable that reads a file in the format:

hw c:\ward\w\hw
cust c:\ibm\2009\cust
dl x:\ward\inet\new-dl

The above examples are of course "current", not from the old days, and yes, I'm still running a variant of that old program.

g dl

automatically changes drive and directory, etc.

OK, so now I'm a linux newbie, and I kept wanting to "g x" and from that go to /c/backups/wardsyaddahyaddah/x which I got tired of typing or using tab completion one directory at a time.

I got severely bitten by the "subshell's vars are gone" so changed a cat | while into a while read ... <filename

Here's the command program:

__________________
#!/bin/bash
alias vig='vi ~/g.sh;source ~/g.sh'
function g () {
lineno=0
foundat=0
while read name dir
do
lineno=$(expr $lineno + 1) ;
if [ "$1" = "" ] ; then
echo $name $dir
foundat=$lineno
elif [ "$1" = "$name" ] ; then
foundat=$lineno
cd $dir
fi
done <~/names.txt
if [ $foundat == 0 ] ; then
if [ "$2" = "." ] ; then
echo $1 $(pwd) >>~/names.txt
tail ~/names.txt
fi
if [ "$2" != "." ] ; then
echo "Not Found"
fi
fi
}
_________________________________
I'm SURE many/any/one can improve upon it, but for now it finds the first match from ~/names.txt (format as above) and changes to that directory (cd $dir) and if no match, says so UNLESS you said:

g blahblah .
which is "shorthand" for "please add blahblah as a shortcut referring to '.' - the current directory.

My next iteration would accept a full path for the 2nd parameter not just . but I wanted to "whip out this little shell script very early in my linux usage so as to have one of my "comfort" commands available.

My third iteration (this much more complicated because of DELETING lines in a file which so far has only been read) is to implement the - option which DELETES an entry (like if you got it wrong):

g blahblah -
would remove blahblah from the list.

Also if you DID a 2nd option on g for a shortcut that already existed, it would have to delete/add or replace such as me freqently defining 'temp' or 'back'

"Oh gee, I am in a place I want to come back to"
g back .
and of course back has a different path today than some other time, etc.


I hope you find it useful!

Right now I'm using it on a ReadyNAS running the sparc unix in a 4 x 1TB raidX array (their definition of Raid - 1 drive = raid 0; 2 drives = raid 1, 3 = raid 5, 4 = raid 5, ALL automatic under the covers -- at least that's what I think it does.

VERY long post, please don't quote it in your reply!

Ward Christensen
Inventor of Xmodem & (W/Randy Suess) BBSs.

P.S. I found a bbs shell script on the site of prolific programmer Bedno! "cute". Even has xmodem xfer! haha

________
Kedit programmer (sigh) trying to learn Object Oriented.
Play with Java Netbeans SDK, getting into Lazarus (Free Open Pascal IDE), ch (c interpreter - great, but sadly only a free limited version and a $500 "next step" waaaaay beyond hobby) etc etc etc.

:wq <= valid signature line? haha

chrism01 11-23-2009 12:24 AM

I'd just like to point out that numeric and string comparison operators use different styles/symbols: http://tldp.org/LDP/abs/html/comparison-ops.html


All times are GMT -5. The time now is 02:49 PM.