Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

So I was looking through Paul Irish's dotfiles and found reference to this bash script:

#!/bin/bash

cd "$(dirname "$BASH_SOURCE")" \
    && source 'utils.sh'

declare -a FILES_TO_SYMLINK=(

    'shell/bash_aliases'
    'shell/bash_exports'
    'shell/bash_functions'
    'shell/bash_logout'
    'shell/bash_options'
    'shell/bash_profile'
    'shell/bash_prompt'
    'shell/bashrc'
    'shell/curlrc'
    'shell/inputrc'
    'shell/screenrc'
    'shell/tmux.conf'

    'git/gitattributes'
    'git/gitconfig'
    'git/gitignore'

    'vim/vim'
    'vim/vimrc'
    'vim/gvimrc'

)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

main() {

    local i=''
    local sourceFile=''
    local targetFile=''

    for i in ${FILES_TO_SYMLINK[@]}; do

        sourceFile="$(cd .. && pwd)/$i"
        targetFile="$HOME/.$(printf "%s" "$i" | sed "s/.*\/\(.*\)/\1/g")"

        if [ -e "$targetFile" ]; then
            if [ "$(readlink "$targetFile")" != "$sourceFile" ]; then

                ask_for_confirmation "'$targetFile' already exists, do you want to overwrite it?"
                if answer_is_yes; then
                    rm -rf "$targetFile"
                    execute "ln -fs $sourceFile $targetFile" "$targetFile → $sourceFile"
                else
                    print_error "$targetFile → $sourceFile"
                fi

            else
                print_success "$targetFile → $sourceFile"
            fi
        else
            execute "ln -fs $sourceFile $targetFile" "$targetFile → $sourceFile"
        fi

    done

}

main

There are two things in this script that really confuses me.

Firstly, what does this sed actually do?

sed "s/.*\/\(.*\)/\1/g"

Secondly what does execute do?

Couldn't find anything on the execute command.

share|improve this question

1 Answer 1

up vote 3 down vote accepted
  1. The sed command appears to be taking the basename of the file. It removes anything before a slash.

  2. The execute function must be defined in utils.sh, which I don't see in that repo. It looks like it runs the command given as its first argument then (on success?) prints the message given in its second argument.

Looks to me like the upshot is to make, e.g., ~/.gitignore a symlink to git/gitignore.

share|improve this answer
    
yes, execute runs command and calls print_result. execute from utils.h –  Evgeny Vereshchagin Aug 8 at 11:33
    
But why are the parentheses escaped? I thought you didn't escape the parentheses if you grouped. Also, does backslash 1 refer to group one (and the only group)? –  Mattias Aug 8 at 12:01
    
Per unix.stackexchange.com/a/33005/63804 , sed matches () literally, so \( and \) are used to delimit the group. Yes, \1 matches the contents of that group. –  cxw Aug 8 at 12:46
    
defined as: execute() { $1 &> /dev/null; print_result $? "${2:-$1}"; } in the same file as being executed here. –  BinaryZebra Aug 8 at 16:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.