• Welcome to Valhalla Legends Archive.
 

*nix shell scripting

Started by K, June 26, 2006, 01:58 PM

Previous topic - Next topic

K

I've noticed that when I'm changing directories from the command line I instinctively type 'ls' as soon as I've typed 'cd.'  So to cut down on the repetition, I decided to write a little script to list the directory right after I changed to it.  This was my first try.

@~/.bashrc

lscd() { cd $1 ; ls ; };
alias cd='lscd';


Then I realized that this wouldn't work for folder names which have spaces in them --

Quote
$ cd Mary\ Star\ of\ the\ Sea/
bash: cd: Mary: No such file or directory

So I changed it to this:

lscd() { cd $* ; ls ; };

Which still doesn't work, because the '\ ' (obviously) gets replaced by a simple space, which results in the same problem.

So I came up with THIS.
(this is off the top of my head -- might have the wrong number of backslashes.  Seems like I needed six.


#!/bin/sh
DIR_TO = `echo $* | sed 's/ /\\\\\\ /g'`
cd $DIR_TO ; ls ; 


But this failed:

Quote
sh -x /usr/local/bin/cdls Counting\ Crows/
++ echo Counting Crows/
++ sed 's/ /\\\ /g'
+ DIR_TO='Counting\ Crows/'
+ cd 'Counting\' Crows/
/usr/local/bin/cdls: line 3: cd: Counting\: No such file or directory
+ ls
Note the end single quote where I was trying to escape the whitespace.

There has got to be a solution, and probably an easier way to accomplish this.

Anyone?

dxoigmn


K

Quote from: dxoigmn on June 26, 2006, 04:40 PM
Why not:


cd "$*"; ls;



You know, I'm almost positive I tried that on my work computer and it didn't work, but I just tried it here and it worked great.

Doh!