Register
It is currently Fri Jul 25, 2014 7:39 am

Making symlinks


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Dec 02, 2008 4:15 pm   

Joined: Tue Dec 02, 2008 4:00 pm
Posts: 2
This is a simple one. I have a folder ~/Dropbox/dotfiles/ and I want to create symlinks in my home folder to each file and folder in dotfiles. For example if dotfiles contains a file muttrc, without the leading dot, the scripts should create a link ~/.muttrc -> ~/Dropbox/dotfiles/muttrc. It should do this for each file and folder in dotfiles.

If a file or folder called ~/.muttrc (or whatever) already exists it should print out "File already exists at ~/.muttrc". If a symlink already exists it should print out "Already linked ~/.muttrc."

I made an attempt at this but it's not well written and doesn't seem to work quite right, it tries to link folders that already exist, and it seemed to overwrite a file that already existed once, and I think it has other bugs.

Thanks

Code:
#!/bin/bash
# This is a bash script that makes various symlinks in my home directory to
# files in my Dropbox folder.

# FIXME: code repetition of the if block! Should be some way to put it in a
# function and reuse it.
# FIXME: it seems that -f checks is a file exists, -a if a dir exists, and
# something else if a link exists, if _anything_ with a given name already
# exists then you can't make a link with that name, so the if function
# should check for the existence of all three.

# Link ~/bin to ~/Dropbox/bin. bin is where I put all my custom executable
# files and scripts.
if [ -h ~/bin ]
then
    echo ~/bin: already linked
else
    if [ -e ~/bin ]
    then
        echo ~/bin: file exists
    else
        echo linking ~/bin
        ln -s ~/Dropbox/bin ~/bin
    fi
fi

# Link to every dotfile in ~/Dropbox/dotfiles.
DIR=~/Dropbox/dotfiles
cd $DIR
for i in *; do
    if [ -h ~/.$i ]
    then
        echo ~/.$i: already linked
    else
        if [ -f ~/.$i ]
        then
            echo ~/.$i: file already exists
        else
            echo linking .$i
            ln -s $DIR/$i ~/.$i
        fi
    fi
done


Top
 Profile  
 PostPosted: Tue Jan 27, 2009 9:15 pm   

Joined: Mon Jan 26, 2009 4:40 am
Posts: 3
Hey did you get it to work?

I still kind of new to this and I am still learning BRE,. but from what i can see you are using the WILDCARD "." character when you mean to be using "." (In the peroiod sense)

try replaceing all "." with "\."

ie
replace
if [ -h ~/.$i ]
with
if [ -h ~/\.$i ]


Top
 Profile  
 PostPosted: Wed Jan 28, 2009 7:28 am   

Joined: Tue Dec 02, 2008 4:00 pm
Posts: 2
No, bash is so arcane and I was never going to find the time to learn it properly. I wrote a script in python to do the job instead.

http://gist.github.com/53945


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP