LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 08-13-2004, 04:42 PM   #1
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Bash script question


Hey guys. I'm hoping someone there could help me out with a little bash script I'm trying to write. I've been doing more and more bash scripting lately and I'm starting to get the hang of it, but I've got a bit of a problem that I haven't figured out yet, and thought you guys might help me find a better way of doing what I'm trying to do...

The problem is that we are setting up a new computer here for a second VSS server. We want to take the latest code from our old VSS server and import it into a new project in the new VSS server. It should be easy enough, right... Get the latest version from the old, and import it into the new. Well... VSS doesn't appear to have a good way to recursively import stuff. I could do it all by hand, but our source tree is pretty extensive and I'd rather avoid that. The command-line version of VSS supposedly allows for a -R option on the "add" command to recursively add projects and sub-projects, but it didn't seem to actually work for me.

This is where my little bash script comes in... I'm using cygwin to run this script and I basically want to recursively step through each directory adding the subdirectories as new projects to VSS, and adding all the files to those projects. I'm currently just working on getting it to a state where I can process all directories recursively. The script I have probably isn't the best way to do things, but it works to a certain extent... The one thing it can't do is handle directories with a space in the name.

Looking through man bash, I notice that there is the pushd and popd commands that could come in pretty handy for this task. I'd need a better way of getting a list of directories that is within a given directory, though. I didn't see any parameters on ls that allow you to show only directories, so I'm basically using a combination of ls -l and awk to find the directory names... that's where the space problem comes in. My awk script will only print something like "Web" if the directory name is "Web References."

If anyone has a good way to get a list of directories that I could use, that would probably all that I would need to figure the rest out myself. My current, somewhat ugly, script looks something like so:

Code:
#!/bin/bash

SSPATH="\"/cygdrive/c/Program Files/Microsoft Visual Studio/Common/VSS/win32/\""
PATH=$PATH:$SSPATH
SSDIR="\\\\casa2\\vss\\"
SS=ss
ROOTPROJ="$/Test"
ROOTDIR="/cygdrive/c/temp/nes"

#$SS help

DIRS[0]=$ROOTDIR
echo ${#DIRS[*]}

while [ ${#DIRS[@]} > 0 ]; do
    I=$(( ${#DIRS[@]}-1 ))
    CURRENTDIR=${DIRS[I]}

    echo "Processing $CURRENTDIR"

    unset DIRS[I]

    TEMPDIRS=`ls -l "$CURRENTDIR" | awk '$1 ~ /^d/ {print $9}'`

    for DIR in $TEMPDIRS; do
        #echo "Adding $DIR"
        DIRS[${#DIRS[@]}]="$CURRENTDIR/$DIR"
    done

done

#ls -l $ROOTDIR | awk '$1 ~ /^d/ {print $9}'
 
Old 08-13-2004, 05:16 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
find -type d
 
Old 08-13-2004, 05:20 PM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Original Poster
Rep: Reputation: 32
Thanks. I found that moments after I posted this. I now have a script that can get all of the directories into an array. I had to do some tricks with sed to replace the spaces in directory names with a __ and then replace it back when I am working with it.... now I just gotta add in the VSS stuff and I should be set. Just in case anyone is curious, my script now looks like so:

Code:
#!/bin/bash

SSPATH="\"/cygdrive/c/Program Files/Microsoft Visual Studio/Common/VSS/win32/\""
PATH=$PATH:$SSPATH
SSDIR="\\\\casa2\\vss\\"
SS=ss
ROOTPROJ="$/Test"
ROOTDIR="/cygdrive/c/temp/nes"

#$SS help

DIRS=(`find $ROOTDIR -type d | sed 's/ /__/'`)

COUNT=${#DIRS[@]}
echo "$COUNT"

C=0
while [ $COUNT -gt $C ]; do
    DIR=`echo ${DIRS[$C]} | sed 's/__/ /'`
    echo $DIR
    C=$(( $C + 1 ))
done
 
Old 08-13-2004, 05:26 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
To get rid of the space issue, you can use null terminated strings, eg.
Code:
find -type d -print0 | xargs -0 -i echo aCommand '{}'
 
Old 08-13-2004, 05:28 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Whenever it is possible that a variable could contain a space, put the variable in double quotes. Example
CURRENTDIR="${DIRS[I]}"

I couldn't see were the $DIRS variable is initially set. If you need to process all subdirectories you could use the find command to return directories and call a routine from the find command.

find "$BASEDIR" -type d -exec script-to-run {} \;
 
Old 08-13-2004, 08:59 PM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 656

Rep: Reputation: 160Reputation: 160
Another way:

(1) ls -alR
(2) Grep all lines beginning with "d" (directories).
(3) Pipe it to awk where you would print the last filed (NF)

so maybe

ls -alR $MyStartDir | grep "^d" | awk '{print $NF}' | sort

END
 
Old 08-14-2004, 12:43 PM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Original Poster
Rep: Reputation: 32
The null-terminated strings idea sounds like a good one. I got my script working nicely, but maybe I'll try to tweak it a bit using that method.

jschiwal, the DIRS variable is set at this line:

DIRS=(`find $ROOTDIR -type d | sed 's/ /__/'`)

It uses the find command to find all directories, sed to replace the space with __ and then returns an array, which DIRS is set to. Since the () breaks the list into an array using a space as a delimiter, to properly get the full path arrays, just putting quotes around individual directories isn't enough, because at that point I just have a list of directories, not individual directories... Obviously, in places where I actually use the individual directories, I need to put quotes around it, though...

AnanthaP, that's basically what I had tried at first, but with directories that have spaces in the name, awk saw the last field as the last part of the directory names after the last space...

Anyway, I got things working like I need. Thanks for all the suggestions. I may have to write some more bash scripts for some common VSS maintenance I do at times. Nobody else at my job uses Linux that much so maybe if they start seeing all the stuff that can be done with these bash scripts they will be more inclined to look at it some...

p.s.

So you're from Fargo too, eh jschiwal?

Last edited by deiussum; 08-14-2004 at 12:45 PM.
 
  


Reply


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 question drj000 Programming 11 03-07-2005 11:25 PM
bash script question djgerbavore Linux - Newbie 3 07-08-2004 04:39 PM
bash script question xscousr Programming 5 07-03-2003 06:04 PM
Bash script question J_Szucs Linux - General 4 05-29-2003 09:48 AM
A bash script question J_Szucs Programming 2 05-13-2003 03:13 AM


All times are GMT -5. The time now is 08:09 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration