-1

I have a program that's supposed to execute in any directory.

For example: My already compiled c program is in the directory "/Desktop/Folder1/" and it's named "program". The directory I'm in right now (in the terminal) is "/Desktop/Folder2/another/". Inside this folder is a "file.txt". What I want is to do something like "myProgram zip file.txt" and this will zip that file.

I searched around and found out about aliases and bash functions... As far as i know i cant pass arguments to aliases so i tried with a bash function:

myProgram() { /Desktop/Folder1/program "$1" "$2" ; }

However this only works if my current directory is the directory of my c program.

Did i get this all wrong and functions don't work like this? Is there another way to do what i want?

If i wasn't clear please let me know since english isn't my native language. Thank you.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
ohiohai
  • 73
  • 8
  • `function myProgram() { ... }` should work. Did you omit the `function` part? – R Sahu May 17 '16 at 03:07
  • As far as i know doing function myProgram () { .. } or just myProgram() {..} is the same. Doesn't work either way. – ohiohai May 17 '16 at 03:09
  • 1
    The problem is most likely in the C program. The `bash` function looks OK to me. – R Sahu May 17 '16 at 03:15
  • It could be because you are passing a relative path to your program. Try changing the second argument to an absolute path (see [this question](http://stackoverflow.com/questions/3915040/bash-fish-command-to-print-absolute-path-to-a-file)). Also, you don't need to pass arguments to an alias, you could just alias `myProgram` to `/Desktop/Folder1/program`. – DanielGibbs May 17 '16 at 03:24
  • Since I had 0 experience with bash functions I immediately assumed this was my problem. As @RSahu pointed out it was indeed a problem with my C program. Thanks for confirming my bash function was correct ! – ohiohai May 17 '16 at 03:49
  • @RSahu, `function` is a bashism that makes your code needlessly incompatible. `myProgram() { ...; }` is the POSIX-compliant function declaration syntax. – Charles Duffy May 17 '16 at 05:01
  • @CharlesDuffy, thanks for info. Learned something new today. – R Sahu May 17 '16 at 05:09

2 Answers2

0

I have a program that's supposed to execute in any directory.

Method 1

Edit your bash profile file usually ~/.bashrc , add :

export PATH=$PATH:/path/to/your/command

However, this has a disadvantage that other binaries in your path will also be readily accessible which may not be desirable.

Method2

cp -n your_command_name /usr/local/bin/
#-n option is  not to  overwrite  an  existing  file  

Notes :

  1. I suggest using a package organizer like paco to manage locally compiled programs.

  2. See the discussion below on what is a good place to store locally compiled binaries.

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • Hmm... writing into the system bin is more desirable? – too honest for this site May 17 '16 at 03:57
  • Ehm ... I meant the whole standard bin directories. They are typically under control of the packet management. There are better places for locally installed files ... – too honest for this site May 17 '16 at 04:00
  • @sjsam -- that is bad practice -- sbin is for system-bin (s for system) and are usually those programs that are not for regular users – Soren May 17 '16 at 04:16
  • "sbin: Utilities used for system administration (and other root-only commands) are stored in /sbin, /usr/sbin, and /usr/local/sbin. /sbin contains binaries essential for booting, restoring, recovering, and/or repairing the system in addition to the binaries in /bin" – Diego Torres Milano May 17 '16 at 04:17
  • @Olaf : Sorry I missed `local` ;) and I use a [paco](http://porg.sourceforge.net/) for double checking. – sjsam May 17 '16 at 04:17
  • @DiegoTorresMilano : Thanks for the info. I agree. I have changed it to `bin` – sjsam May 17 '16 at 04:21
0

You're going about this all wrong. Instead, take a minute and learn how other programs do it. Learn about the $PATH environment variable.

All modern OSes (yes, including Windows) have a concept of the PATH environment variable. This variable contains a list of all directories where executables (both binary and scripts) are installed.

The traditional solution

Traditionally unix systems are shared among many users. You still see this sometimes these days in servers where the sysadmin, db admin and developers all have their own logins (they don't all login as root right? right ??). So usually if you write your own script or program you normally don't have permissions to install your software in the usual places like /bin or /usr/bin or /usr/local/bin.

Fortunately, each user can modify their own PATH variable so they can add their own bin folder to the PATH variable.

Traditionally the solution is to create a bin folder in your home directory (you can name it anything you like, some people like programs but traditionally it is bin because all the standard folders are bin):

mkdir ~/bin

Then add the folder to the PATH variable:

export PATH="$PATH:~/bin"

Sometimes the ~ expansion doesn't work so sometimes you may need to use the full path of your home directory:

export PATH="$PATH:/home/ohiohi/bin"

To make the modification to $PATH permanent add it to .profile or .bashrc

Now you can copy the program you wrote to ~/bin to make it available from anywhere:

cp /Desktop/Folder1/program ~/bin

Now you can just type:

program

The advantages of the traditional solution

Traditionally, everything you own should only be saved inside your home directory. This includes personal scripts, executables etc. The advantage of this is that if you upgrade to a new PC or a new unix based OS all you need to do is copy your home directory over and you can continue with what you're used to.

I've personally been evolving this current home directory form my student days way back in 1997. It started on Linux PPC then moved to Red Hat Linux then moved to Cygwin on Windows95, Windows98, Windows2000, WindowsNT then again to Red Hat Linux then Centos, Ubuntu, Linux Mint and it currently resides on a Mac.

Obviously binary executables won't work if you move to incompatible platforms. This explains why a lot of unix users tend to prefer scripting languages. But you can always recompile.

slebetman
  • 109,858
  • 19
  • 140
  • 171