Linux: Triggering Shell Command On File Save - linux

I want to automatically trigger a shell command when a file is modified. I think this can be accomplished in code by registering an inotify hook and call to system, however is there a higher level bash command that can accomplish this?

Try inotify-tools. I'm having problems copying the link (sorry), but there is a wiki on GitHub which yu should be able to find with G-search-engine.

I don't have a solution to your problem, but I've found this nice program that you might use the feature you request: cwatch

Related

Writing, deploying, installing command line tool written in shell

It's just one of those days...
I want to create a shell script (naive version could be a simple alias), but I want to make it the good way from the bottom up. Writing script itself isn't a problem at all, but since it's possible a couple of other people would like to use this script as well I want to make it accessible and maintainable as well. That's why I have two questions:
Are there any guides regarding structure of the repository with shell script inside? I mean, some conventions that, for example, we put a script in some directory, manpage, should I split it into multiple files etc.
How to make this script be accessible to other people? I know that if we want to install command line tool, we move it to /usr/bin or /usr/local/bin, but what with docs or some dependencies?
There are a lot of tutorials regarding writing command line tools in Ruby or Python with some templates how to manage everything, but I haven't found anything about writing a command line tool as a shell script.
In general, you would follow the conventions in man hier for the distribution. A shell script would be treated the same as a binary file.

How to execute a perforce command like 'diff' within a P4api.net?

I am working with a p4api.net application and I need to make use of perforce 'diff' command in it. I couldn't find a right approach to execute this command. I need a way to execute the command inside the application and get its result. Thanks in advance.
Without knowing what you've tried so far, or what the specific problem is, the best we can do is point you to the P4API.NET documentation for the diff method.

How is tab completion implemented for linux commands?

I've noticed that sometimes commands can be tab completed.
e.g. the xm command in xen.
you type xm[space][tab] and it prints out the valid options
which are:
addlabel destroy info network-attach resume sysrq vnet-delete
block-attach dmesg labels network-detach rmlabel top vnet-list
block-detach domid list network-list save trigger vtpm-list
block-list domname loadpolicy new sched-credit unpause
cfgbootpolicy dry-run log pause sched-sedf uptime
console dump-core makepolicy reboot serve vcpu-list
create dumppolicy mem-max rename shutdown vcpu-pin
debug-keys getlabel mem-set resources start vcpu-set
delete help migrate restore suspend vnet-create
That's pretty slick!
How can I implement my own tab command completion in Linux?
This is a pretty broad question, but the general idea is that you register something with the either the compgen or complete builtin. They're both documented in the manual. The previous section documents the general topic of programmable completion, going through how completion attempts are processed.
For a whole ton of examples, see /etc/bash_completion, which provides all the default completion that comes with bash (beyond the totally built-in stuff like filename completion). For even more examples, see anything in /etc/bash_completion.d; those are automatically sourced by /etc/bash_completion as a way of extending the default completion.
This is done via the shell through the use of the GNU Readline library in the case of bash
bash's smart completion is handled by a series of scripted bash functions. On Debian, probably Ubuntu, and maybe other Linux distributions, you can find your system's installed completions in /etc/bash_completion.d.
The official documentation on this mechanism is at http://www.gnu.org/software/bash/manual/bash.html#Programmable-Completion
See this:
How does bash tab completion work?
and this:
http://www.debian-administration.org/articles/316

Detect directory changes in unix

How could I track changes of specific directory in UNIX? For example, I launch some utility which create some files during its execution. I want to know what exact files were created during one particular launch. Is there any simple way to get such information? Problem is that:
I cannot flush directory content after script execution
Files created with the name that has hash as a compound part. There is no possibility to get this hash from script for subsequent search.
There could be several scripts executed simultaneously, I do not want to see files created by another process in the same folder.
Please notice that I do not want to know whether directory has been changed as stated here, I need filenames which ideally could be grepped to match specific pattern.
You need to subscribe to file system change notifications.
You should use something like FAM, gamin, or inotify to detect when a file has been created, closed, etc.
You could use strace -f myscript to trace all system calls made by the script, and use grep to filter the system calls that create new files.
You could use the Linux Auditing System. Here is a howto link:
http://www.cyberciti.biz/tips/linux-audit-files-to-see-who-made-changes-to-a-file.html
You can use the script command to track the commands launched.

How can I hook into tcsh's TAB completion on Linux

I have some directories with a number of "hidden" files. One example of this is I'm in a source controlled sandbox and some of the files have not been checked out yet.
When I hit TAB, I'd like the option of seeing these files.
A similar question has been asked before: CVS Tab completion for modules under linux
The answers to that question summarize to: "Ubuntu's got that built in".
I don't have the option of switching to Ubuntu, but surely I can use the same mechanisms.
how can I hook into the TAB-completion feature of tcsh to add additional file Support for CVS, SVN and BitKeeper would all be useful.
More important than support for a specific source control system is the ability to control the returned list myself.
An acceptable solution would also be to use a key-binding other than TAB. (ctrl- perhaps)
From the manpage:
the complete builtin command can be used to tell the shell how to complete words other than filenames, commands and variables
might get you started
I do not know how to program in tcsh. But if you can, then you could look at the file named "bash_completion" from the archive (find the download link here.)
On line 1673 begins CVS completion code - and this might be portable to csh if you are familiar with the differences between bash/tcsh.
On my ubuntu machine, there is also a section for SVN completion (in /etc/bash_completion) that doesn't seem to be present in the maintainer's archive.
That's not Ubuntu-specific behavior, it's the bash-completion project.
You could use that, if you can switch from tcsh to bash.

Resources