0

I have a C++ program with a main routine which reads arguments from command line:

int main(int argc, const char *argv[])

In my system I have defined some aliases in my bash environment which abbreviates directory names.
Example:

alias dst='home/username/Documents/test'
alias src='home/username/Download/test'

My program is called "dircopy" and I want to use the following input:

dircopy /home/username/Download/test /home/username/Documents/test

But I want to avoid writing the whole path for destination directory and source directory each time I run the program. Is there a clever way to use the alias definitions in bash when calling my "dircopy"-program ?

(Example: dircopy $src $dst - or something similar...)

Edited by it@61@sec: spelling correction

3
Contributors
5
Replies
19
Views
2 Years
Discussion Span
Last Post by it@61@sec
Featured Replies
  • 1

    You could use environment variables. In C/C++ you can access your environment either by the `envp` to main or through the `getenv` call. For `getenv` you might do something similar to the following pseudocode: for arg in argv: if arg[0] == '$': char * variable = getenv(arg[1..-1]) You'd obviously need … Read More

  • 1

    If you do make them variables, `dircopy $src $dst` will work fine. Read More

  • 1

    Yes, thank you **sepp2k**, I forgot to mention that `$var` in bash gets expanded *prior* to getting passed to your program. With that in mind, my prior example is not too accurate since you wont likely see `$` characters byt the time `argv` is filled in. Hopefully I haven't confused … Read More

1

You could use environment variables. In C/C++ you can access your environment either by the envp to main or through the getenv call. For getenv you might do something similar to the following pseudocode:

for arg in argv:
   if arg[0] == '$':
      char * variable = getenv(arg[1..-1])

You'd obviously need to add error checking to that but it should convey the process clear enough.

0

Seems OK, but I wonder if there is a way to make bash do the substitution BEFORE it lets the program process the input.
In that case I will not be forced to change my c++ program..

1

Yes, thank you sepp2k, I forgot to mention that $var in bash gets expanded prior to getting passed to your program.

With that in mind, my prior example is not too accurate since you wont likely see $ characters byt the time argv is filled in. Hopefully I haven't confused you too much.

0

Thanks to both of you. This was exactly what I was looking for..
(I'm a newbie on this.. :-) )

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.