「実行」コマンドと奇妙な正規表現を使用するBashスクリプト

「実行」コマンドと奇妙な正規表現を使用するBashスクリプト

だから私は探しています。ポールアイランドのドットファイルそして参考資料を探すこれバッシュスクリプト:

#!/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

このスクリプトには私を本当に混乱させる2つのことがあります。

まず、このsedは実際に何をするか。

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

次は何が来るのか?実装するする?

実行コマンドで何も見つかりませんでした。

ベストアンサー1

  1. sed コマンドがファイルのデフォルト名を取得するようです。スラッシュの前にあるすべてを削除します。

  2. 実行可能な関数はutils.shで定義する必要がありますが、そのリポジトリには表示されません。最初の引数として与えられたコマンドを実行してから(成功したら?)、2番目の引数に与えられたメッセージを印刷するようです。

その結果、~/.gitignoreたとえばシンボリックリンクが生成されるようです。git/gitignore

おすすめ記事