LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2009, 11:45 PM   #1
WardXmodem
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
Post 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
 
Old 11-23-2009, 12:24 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
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
 
  


Reply

Tags
bash, script, shell, shortcut, subshell


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to insert id3 tags from file and directories jason7 Linux - General 15 07-20-2009 05:10 AM
Problems with Fat long file names in Bash brett90 Slackware 5 06-03-2005 12:15 PM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 03:40 AM
script #!/bin/bash, problem with space in file names existent Linux - General 3 06-17-2004 08:13 AM
Second CD doesn't work - short file names auditude Slackware - Installation 4 02-09-2004 11:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 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