Valhalla Legends Forums Archive | General Programming | *nix shell scripting

AuthorMessageTime
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.
[code]
@~/.bashrc

lscd() { cd $1 ; ls ; };
alias cd='lscd';
[/code]

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
[/quote]

So I changed it to this:
[code]
lscd() { cd $* ; ls ; };
[/code]
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.

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

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
[/quote]
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?
June 26, 2006, 6:58 PM
kamakazie
Why not:

[code]
cd "$*"; ls;
[/code]
June 26, 2006, 9:40 PM
K
[quote author=dxoigmn link=topic=15283.msg154992#msg154992 date=1151358045]
Why not:

[code]
cd "$*"; ls;
[/code]

[/quote]

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!
June 26, 2006, 10:04 PM

Search