Linux to Define Custom Shell Command

How to make --version custom command in linux

You should make a CLI (Command Line Interface).
To do this, you should parse the command line arguments, like --help and --version. You can access them with $1, $2, ...

Here is an example on your code :

#!/bin/bash

cli_help() {
# --help: Shows help message
echo "
owncmd - Custom command

Usage:
owncmd [options]

Options:
--help / -h: Show this message and exit.
--version / -V: Show version number and exit.
"
exit
}

cli_version() {
# --version: Show version
echo "owncmd v1.0"
exit
}

# Argument
case $1 in
"--help"|"-h")
cli_help
;;
"--version"|"-V")
cli_version
;;
esac
# Main program
echo "Heya custom commands"

The program look at the first argument $1.

  • If it is --help or -h: Show an help message (function cli_help) and exit
  • If it is --version or -V: Show version number (function cli_version) and exit

ℹ️ Note: We use -V instead of -v because -v means "verbose mode", not "version".

If there is no --help or --version, the program shows Heya custom commands.

How to create a custom bash command in linux

yout just have to change it's owner & group root following commands
sudo chown root "file_name"
sudo chgrp root "file_name"
then give this command to change the permissions
sudo chmod 755 "file_name"
and place it in /bin with this command
sudo mv "file_name" /bin

now u can run it as a normal command.

Custom command line editor in Linux

For Bash:

There is a Readline command that opens the current command in an editor, edit-and-execute-command. By default, it is bound to C-x C-e, and it opens the command in what $VISUAL is set to, or $EDITOR, or with Emacs.

You can set $VISUAL to your editor by exporting it into the environment, for example in ~/.bashrc:

export VISUAL=youreditor

and bind it to Alt+Enter with

bind '"\e\C-m": edit-and-execute-command'

on the command line, or

"\e\C-m": edit-and-execute-command

in ~/.inputrc.

CMake run custom command with mixed shell scripts

CMake syntax is crazy and it's not easy to make this kind of escaping (involves cmake, shell and makefile) right. To work it around I suggest you put the shell command in a separate file (e.g. helper.sh) in your project dir and then call it like this --

add_custom_command(
...
COMMAND bash ${PROJECT_SOURCE_DIR}/helper.sh > foo.h
)

Line completion with custom commands

Create a file "myprog-completion.bash" and source it in your .bashrc file. Something like this to get you started...

_myProgram()
{
cur=${COMP_WORDS[COMP_CWORD]}
case "${cur}" in
d*) use="doSomething" ;;
n*) use="nowDoSomethingElse" ;;
esac
COMPREPLY=( $( compgen -W "$use" -- $cur ) )
}
complete -o default -o nospace -F _myProgram myProgram

Custom commands with git-shell

As it turned out, this feature has been introduced in git 1.7.4. I am using debian squeeze, wich contains an older version of git, so that was why it did not work.

If you experience this problem, check your git version.

However, as of git 1.7.10, the custom commands only work in interactive mode, and not with -c. I haven't tried the newest git though, so it is possible that this problem is unrelated to the version of the software.



Related Topics



Leave a reply



Submit