Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Say my pwd is at ~/myproject/ and I have a file in ~/myproject/scripts/com/example/module/run_main_script.sh

In vim with ctrlp plugin, I can press Ctrl+P, type run_main_ Enter, I am editing that script.

I want to run that script (with some arguments) in bash. And I don't want to type the full path. Is there any way to do that in bash?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

That's what normally the PATH variable is for. Though, I would not add your whole home-directory to your PATH. Consider adding a dedicated directory (like ~/bin) to add to your path your executables.

However, you could add a function to your ~/.bashrc which allows you to search for and run a script...something like this:

# brun stands for "blindly run"
function brun {
    # Find the desired script and store
    # store the results in an array.
    results=(
        $(find ~/ -type f -name "$1")
    )

    if [ ${#results[@]} -eq 0 ]; then   # Nothing was found
        echo "Could not find: $1"
        return 1

    elif [ ${#results[@]} -eq 1 ]; then   # Exactly one file was found
        target=${results[0]}

        echo "Found: $target"

        if [ -x  "$target" ]; then   # Check if it is executable
            # Hand over control to the target script.
            # In this case we use exec because we wanted
            # the found script anyway.
            exec "$target" ${@:2}
        else
            echo "Target is not executable!"
            return 1
        fi

    elif [ ${#results[@]} -gt 1 ]; then   # There are many!
        echo "Found multiple candidates:"
        for item in "${results[@]}"; do
            echo $item
        done
        return 1
    fi
}
share|improve this answer
    
This is a great script! I changed a bit to meet my need: ` results=($(find ~/ -name "$1")) => results=($(find ./ -type f -name "$1*")) exec $target ${@:2} => $target ${@:2} ` –  fqsxr Nov 6 '12 at 2:41
    
@fqsxr: -type f is good idea, yeah. –  Bobby Nov 6 '12 at 8:10
add comment

Execute the current file with sh or alternatively bash:

!sh % some arguments

Perhaps I misunderstood your question. If your intention is not to execute the file from vim but from a regular prompt you can:

  1. add the parent folder of you script to PATH (export PATH=$PATH:/my/scripts/folder/)
  2. make an alias (alias moo=/usr/local/something/moo/script.sh)
  3. create a function (function moo { sh /usr/local/something/moo/script..sh $@; } )

These are common solutions to your question, I do not know of any employing "fuzzy completion".

share|improve this answer
add comment

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.