How can I create a command in linux, is alias the only way? - shell

I come here, just for context, based on this question.
How to set bash aliases for docker containers in Dockerfile?
I know I could create an alias for a certain command and inject them to the bash profile of the users.
For example:
alias kc=/opt/kafka/bin/kc.sh
I could also add the directory to the path (probably this is what I'll do).
But I wanted to know is there a way I could create this as a kind of command?
Is alias the only way to do this, and is there a way I could create the alias for all users not depending on the bash they use?
For example if I would want to create a command something like
my_command is there a way to do this?
Probably this question is showing lots of lack of knowledge of the actual operating system but I just wanted to know what options I would have, and my google searches always show the same results so I thought I asked here

You have the command already: /opt/kafka/bin/kc.sh.
I understand that you want to make it available to your users as a simple kc.sh or kc, without the directory prefix. The solution is to either put that directory into the PATH variable, typically done in /etc/profile; or put the command respectively a (symbolic) link to it into a directory which is already in the PATH, like /bin/, /usr/bin or /usr/local/bin.
Putting the directory into the PATH is probably the better solution, also because there are probably more useful commands to be found. If you don't like kc.sh but want a simple kc you can still make a link in the same directory, e.g. by performing cd /opt/kafka/binand then ln -s kc.sh kc.

Related

in which order will ubuntu search bin-folders for executables?

So I learned that in order to be able to execute a program from everywhere in the shell I have to put a reference file looking something like
#!/bin/bash
path/to/my/original/executable
in my bin-directory and make it executable.
On my current linux system (provided by my workplace) there are multiple bin-directories like
/usr/local/bin
/home/MyUsername/bin/
/home/otherUsername/bin/
For my work I have to alter a c++ program which is supposed to be usable for every user on the computer (hence I used /usr/local/bin/ sofar). But I realized that my changes to the original program do not come through... So my questions are:
Where could other bin-folders be, that I need to check for old executables?
How does my operating system (ubuntu 16.04) choose between executables in different bin-locations but with the same name?
Thanks in advance!
The PATH shell variable contains a colon separated list of paths to look for executables in. The list is processed left to right, the shell executes the first executable binary it finds (make sure to chmod +x the binary you are providing). If you want an easier printout you can use: echo $PATH | tr ":" "\n". Also keep in mind that a program might have been started with a different PATH than your shell and that users can customize their PATH variable. Systemwide PATH settings can usually be found in /etc/profile or /etc/profile.d/. You can use which file to display the full path expansion of file.

cron: run a script that sources a function

I have script that does a bunch of stuff. It sources a bunch of functions that are in the directory the script is being run from. i.e.
/home/me/script.sh
/home/me/function1
/home/me/function2
If I cd into /home/me and run ./script.sh everything works fine. The functions are sourced and do what needs to be done.
However, if I try to run this as a cron job, it will run up until the point I am trying to source the functions, and then it just stops and the process is terminated (if I run it directly from the directory, at least I get some errors).
Like wise, if I try to run this from another directory, I get a bunch of errors. e.g.
cd /opt/
/home/me/script.sh
function1: command not found
function2: command not found
I'm sure this has something to do with environmental variables, but I have no idea which ones. I have tried setting (in crontab):
PATH=/home/me
SHELL=/bin/bash
But that doesn't work either. Any help is appreciated. I don't want to hard code in the paths to the functions, and instead make them relative to the path the script is in (preferably the same dir).
Please let me know if you need any more information.
You are most probably aware of this, but just to be clear: A shell function does not have a path. They just need to be loaded into the current shell by sourcing the script that contains them:
source /path/to/functions
or
cd /path/to/functions
source functions
If you are talking about shell programs (scripts) instead, then you need to account for the fact that on Unix-like OS, the current directory is never in the PATH by default:
/path/to/functions/function1
or
cd /path/to/functions
./function1
You tagged your question Bash, but note that to be POSIX-compatible (e.g. if using sh), you have to use the . keyword (instead of either . or source on Bash) and the same restrictions regarding the PATH as for command execution apply, see dot:
. ./function1

Setting up vim autocomplete for the Go programming language

EDIT:
I was generally interested to know how people had their go autocomplete set up for vim and was looking for advice on it.
Related to my original question, I think I know I can just go to:
https://github.com/nsf/gocode
copy the files that they provide there and just start using the vim autocomplete. However, I wanted to know what people thought and how the go community has the vim autocomplete set up.
Also, I have followed the intructions as were posted there and I still cannot make the autocompletion work for my go in vim. So I am looking for other alternatives or ideas on how to make it work. Not sure what the problem is. Currently when I try to autocomplete it simply writes the word PANIC instead of showing me the options for autocompletion.
ORIGINAL:
I was trying to set up my vim such that it could auto complete the Go programming language, however, I was following the instructions in the following page:
https://github.com/nsf/gocode
and I was not sure what they meant and I was a little scared of maybe changing my vim set up in a way that might be damaging by doing it wrongly.
The first thing that confused me is it says:
Install official Go vim scripts from $GOROOT/misc/vim. If you did that already, proceed to the step 2.
However, I was not sure what that even meant. I did go to that directory in my terminal and read the readme.txt file and I it said how to activate the syntax highlighting which I already had anyway. Is that everything I have to do for that step?
On step 2 it says:
do:
vim/update.sh
They actually provide the code that update.sh is but I was not sure what the beginning of the cp command meant i.e. its:
#!/bin/sh
mkdir -p "$HOME/.vim/autoload"
mkdir -p "$HOME/.vim/ftplugin/go"
cp "${0%/*}/autoload/gocomplete.vim" "$HOME/.vim/autoload"
cp "${0%/*}/ftplugin/go/gocomplete.vim" "$HOME/.vim/ftplugin/go"
But what does the ${0%/*} part do? and even if I know what the update.sh is, where do I even run this, since this vim/update.sh is done at a relative path?
I know update.sh wants me to copy some files to $HOME/.vim/ftplugin/go and $HOME/.vim/autoload, but I even did a find from ~ and couldn't find such files, so I am unsure on what to copy. I know where it should go, but not where the file even is. Does someone know where those files are or an easier or more detailed explanation on how to make vim auto-complete for go?
Thanks for the help in advance! :)
Some of the problems that I have discovered that I have, not sure if its expected, but in the $GOROOt/misc/vim/ftplugin/go I do not have the gocomplete.vim file at all. I have other stuff that seems irrelevent like an fmt.vim import.vim and a test.sh file.
But the odd thing is that at $GOROOt/misc/autoload I do not have the gocomplete.vim file but I instead have a complete.vim file. Not sure if that the same or why the name of the file would have changed. Anyway has their go autocomplete set up and mind sharing what they have and if they know what the differences might be with what I have encountered? Thanks!
ADDITION to Question
I am also generally interested in how other people have their auto-complete set up for their go in vim. Feel free to share that too!
Have you executed the update.sh command already? I'm pretty confident that it will work.
All of your Vim configuration is stored in ~/.vim/, ~/.vimrc and ~/.gvimrc (with Vim 7.4, you can put the last two also inside the first directory). To backup your Vim configuration, just store those somewhere (or put all of your dotfiles under version control, as many now do).
The ${0%/*} manipulates the script's file name ($0) like dirname does: It cuts off the script file name itself (everything at the end * until the last /). With this, you can invoke the script from anywhere, e.g. $GOROOT/misc/vim/update.sh or cd misc; vim/update.sh or cd misc/vim; ./update.sh.
The script also ensures that the autoload and ftplugin subdirs exist, and creates them if they don't yet. Just give it a try!

Running a script automatically when logging in via ssh

I would like to know if there is a (simple) solution to the following issue:
When I log in with ssh to a specific host, I would like to automatically execute a (bash)script on that host. This way I could -for example- load my aliases on that host.
Definitively the bashrc script is not executed; The ssh configuration files do not seem to help in this issue either.
Any suggestions?
Thanks in advance!
BTW: The host is running on Gentoo
If .bashrc isn't being run, try .profile, which has a similar function. Different shells use different startup scripts at different times, so knowing when to run things is useful.
On many systems where you have a choice of which shell to use, you are put through ~/.profile only. This way there is no need to find out (and no probably wrong guessing) which shell you're running in and which profile to actually load (.bashrc, .cshrc, .kshrc etc.) and which ones to avoid loading.
The easiest solution in your case would be to create a link (a symbolic one if you prefer visibility) to your favourite shell's startup script as in ln -s ~/.bashrc ~/.profile. If you don't intend to ever using anything other than bash, you're set.

How do I alter tab autocomplete in bash to dive through folders?

I have a folder 'test' which contains another folder 'test2'
When I type 'cd te[tab]' it auto-completes to 'cd test/'
How do I make it autocomplete to 'cd test/test2/', without hitting tab again?
To clarify: test is the only folder/file in the folder test. I want this to work recursively so if there is a folder/with/a/lot/of/single/files/or/folders/in/it
Bash supports programmable auto completion (at least since version 3.0). There is some documentation in the bash manual on
http://www.gnu.org/software/bash/manual/bashref.html#Programmable-Completion
It might also be a good idea to look at existing scripts to get an idea how to really make use of that feature. Debian for example has a /etc/bash_completion file with completion scripts for various programms. I'm sure other distributions have something similar
It is hard for bash to understand either you want to jump to test or to test/test. So I believe there is no standard settings.
But you can always alias commands for particular cases like
alias cdtest="cd test/test"

Resources