ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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}'
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
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.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.