1

I want to execute foo.bsh typing only foo.bsh or even foo instead of ./foo.bsh. I would like to do this temporarily for all .bsh in a directory, as well as globally. For example I want to have a global Key MYIP which calls a script to echo my external IP. I don't know if this make sense, because I could crash my bash if I overwrite echo.

Edit: to be more clear. I want a shortcut for writing "./" saving me 2 keys for those commands, such as in DOS where foo.exe can be executed with foo. Couldn't bash look in the current directory for .bsh files, if I type foo (ENTER) ?

4

3 に答える 3

3

You can hook into bash's processing when the command is not found, and add your own function to process however you like. For example, add this to your .bashrc:

function command_not_found_handle()
{
    [ -f "${1}.bsh" ] && bash "${1}.bsh"
}

So, if you have foo.bsh in the current directory, you can just type "foo" and it will run.

Edit: Clarification -- when bash has exhausted its internal mechanisms to find a command that you typed, it will run the command_not_found_handle() function with the name of the command as an argument. So, you can define this function with whatever logic you want.

于 2012-10-29T19:38:59.277 に答える
2

You really shouldn't do this, but type this into your .bashrc or session to add the current directory to the PATH.

PATH=$PATH:.

Of course this means if someone puts a command with a similar name in a directory, it could easily be executed.

So an attacker does:

cd /shared_directory
echo "rm -rf all_your_files" > apropo
chmod +x apropo

And then you:

cd /shared_directory
apro<TAB> (completes to apropo)
(all your files are gone).

In case you were wondering the target command here was apropos.

Generally the better solution is just to link your executables to something in your PATH like ~/bin

So:

PATH=$PATH:~/bin (in your bashrc or whatever)
ln -s ~/some_executable ~/bin
some_executable
于 2012-10-29T19:49:01.710 に答える
0

You can achieve this by editing your ~/.bash_profile page and adding the directories your executables are to the end of the lists, like

export PATH=$PATH:/your/directory

I usually create a ~/bin directory and store all my executables in this directory. So I usually add this entry to my ~/.bash_profile file

export PATH=$PATH:/home/yourname/bin/

After that all my scripts inside my bin directory (which have been chmod +x previously) works by simply calling them by the name at the console.

I hope it helped. Cheers

于 2012-10-29T19:35:29.233 に答える