Establishing a unit converter which can be called globally through terminal

1

I would like to have a function (or may be some kind of executable) that can be called via Terminal from wherever I am (from any directory) and this function does a simple job of converting between units, e.g. from meter to feet. Is such task realizable? I can code in Fortran90 and C, the former being more familiar with than the latter. I work on MacOS X platform.

For the sake of simplicity let's say that my function written in C takes a string and print it in standard output:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char str[4];
    strcpy(str, argv[1]);
    printf("input = %s \n",str);       
}

Then compiling it to generate an executable named "example". I would like to be able to call this program globally, so that by executing $example abc it will print input = abc in the terminal window. I have tried placing this executable under the same directory as the gcc's (gfortran, gcc, g++, etc) since this path has been set to the environment variable but it didn't work. I can't call it from outside this directory.

nougako

Posted 2017-01-12T14:15:31.523

Reputation: 13

Answers

0

To call it from anywhere in your system, your application must be in a directory that is part of the variable PATH.

On terminal issue:

echo "$PATH"

or

printf "%s\n" $PATH

and you will see something like this:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

That is the list of locations where the system will search for the application.

Move you compiled application to any of those directories: usr/local/bin for example, enable the executable bit (chmod +x) and that's all.

jcbermu

Posted 2017-01-12T14:15:31.523

Reputation: 15 868

So I simply first run $chmod +x example, and then I can call it by e.g. $example abc? I am sorry right now I can't directly test that because the Mac I am using is actually in my office. Also, why do I need to use chmod first, is it because it will enable the global property of my application? As I recall I have actually tried placing my application "example" in usr/local/bin but it didn't work either. It couldn't be called globally. – nougako – 2017-01-12T14:50:41.227

@nougako In Unix-like operating systems, files have a set of permissions: read, write, and execute. Typically, by default, a file will not have the execute permission and cannot be run. chmod (think "change mode") is used to modify files' permissions. chmod +x specifically, will enable the execute permission on whichever files you specify, allowing you to run them. chmod has nothing to do with where you can run it from, only if you can run it at all. – 8bittree – 2017-01-12T21:46:49.257

In some shells (*csh at least), the builtin rehash command must be issued before a new executable will be found in your $PATH of the current shell process. Starting a new shell will also rehash the $PATH tree. – Nevin Williams – 2017-01-20T19:41:47.053

Thank you very much it works. Sorry for the late reply I had been really occupied last week and just got the chance to try your suggestion. – nougako – 2017-01-26T02:19:12.760