How does one create a wrapper around a program?

436 Views Asked by At

I want to learn to create a wrapper around a program in linux. How does one do this? A tutorial reference web-page/link or example will do. To clarify what I want to learn, I will explain with an example.

I use vim for editing text files. And use rcs as my simple revision control system. rcs allows you to check-in and checkout-files. I would like to create a warpper program named vir which when I type in the shell as:

$ vir temp.txt

will load the file temp.txt into rcs with ci -u temp.txt and then allows me to edit the file using vim.

When I get out and go back in, It will need to check out the file first, using ci -u temp.txt and allow me to edit the file as one normally does with vim, and then when I save and exit, it should check-in the file using co -u temp.txt and as part of that I should be able to add a version control comment.

Basically, all I want to be doing on the command line is:

$ vir temp.txt

as one would with vim. And the wrapper should take care of the version control for me.

2

There are 2 best solutions below

2
On

I have a wrapper to enhance the ping command (using zsh) it could, maybe help you:

# ping command wrapper - Last Change: out 27 2019 18:47
# source: https://www.cyberciti.biz/tips/unix-linux-bash-shell-script-wrapper-examples.html
ping(){
    # Name: ping() wrapper
    # Arg: (url|domain|ip)
    # Purpose: Send ping request to domain by removing urls, protocol, username:pass using system /usr/bin/ping
    local array=( $@ )          # get all args in an array
    local host=${array[-1]}     # get the last arg
    local args=${array[1,-2]}   # get all args before last arg in $@
    #local _ping="/usr/bin/ping"
    local _ping="/bin/ping"
    local c=$(_getdomainnameonly "$host")
    [ "$host" != "$c" ] && echo "Sending ICMP ECHO_REQUEST to \"$c\"..."
    # pass args and host
    # $_ping $args $c
    # default args for ping
    $_ping -n -c 2 -i 1 -W1 $c
}

_getdomainnameonly(){
    # Name: _getdomainnameonly
    # Arg: Url/domain/ip
    # Returns: Only domain name
    # Purpose: Get domain name and remove protocol part, username:password and other parts from url
    # get url
    local h="$1"
    # upper to lowercase
    local f="${h:l}"
    # remove protocol part of hostname
    f="${f#http://}"
    f="${f#https://}"
    f="${f#ftp://}"
    f="${f#scp://}"
    f="${f#scp://}"
    f="${f#sftp://}"
    # Remove username and/or username:password part of hostname
    f="${f#*:*@}"
    f="${f#*@}"
    # remove all /foo/xyz.html*
    f=${f%%/*}
    # show domain name only
    echo "$f"
}

What it hides the local ping using a function called "ping", so if your script has precedence on your path it will find at first the function ping. Then inside the script I define an internal variable called ping that points out to the real ping command:

 local _ping="/bin/ping"

You can also notice that the args are stored in one array.

1
On

Take a look at rcsvers.vim, a vim plugin for automatically saving versions in RCS; you could modify that. There are also other RCS plugins for vim at vim.org