简体   繁体   中英

how to execute program which is in my environment variable in a bash script

I'm just learning bash but the script that I made is running (quite proud of it despite its modest result) except for one little detail:

I wrote a bash script that in the end should execute a program. This program is known in my environment variable, so that when I write the line:

my_program -b result.dat

The program executes directly with the result file as input (and in batch mode).

I would like to execute this program at the end of my script, so I just have to execute my script, have it make it's calculation, and in the end execute my_program with some options

#!/bin/bash

#calculations

my_program result.dat 

After some searching I found a command that could fit

eval my_program

env my_program

source my_program

bash my_program

each time the result is the same

=> my_program: command not found 

=> my_program: no such file or directory 

Any way of doing it ?

thank you in advance for your time

EDIT

I could write the whole path of my_program ( it works ) but i am looking for a solution that can work on another computer which can have another path.

My idea is that knowing the name of the program, the script can search through the environnement variable and retrieve the path to the program. Can this be done ?

BEST SOLUTION - by zezollo

  • Finding the program path in every computer to set it is $PATH is taking too much time
  • Putting manually "my_program" path in the $PATH variable is recommanded
  • Putting manually "my_program" path in the .bashrc is even better for it will stay for each execution

Set $PATH correctly

The best way to do what you want is to set $PATH correctly, eg to have the directory containing my_program listed in $PATH .

To see the content of $PATH , just type echo $PATH in a console.

To add one or more directories to $PATH you can type in a console:

$ export PATH=/home/one/more/path/:/yet/another/path/:$PATH

but this will work only for your current console session.

You certainly want to have this $PATH set the same way everytime you start a new console: then you can add this very same line to the file .bashrc in your own profile.

Of course, you'll have to set $PATH on every computer where you want to use your program. But I would go for this solution as it quite belongs to the normal 'installation' process of a program: if you want a program to be called from another script, you'd better tell the other script where is your program. Set $PATH is a straightforward way of doing it.

Once $PATH is set correctly, your script only need to call directly my_programm -b my_file , no need for eval or source or whatever.

Use find

Now, say you don't know where is your program and you don't want to set $PATH correctly on every computer and the full path of my_program is different on each computer (are you sure there's no other solution? There must be a GOOD reason for such a situation, as only have 1 or 2 different possible directories should really be enough and can be easily fixed by setting $PATH correctly).

Then of course you can always find your program. But it's not really a good solution.

It could be something like:

full_path=`find /home/$USER -nowarn -name my_program 2> /dev/null`

BUT it means:

  • you are sure there won't be several lines of output (that's why I added -nowarn and 2> /dev/null to avoid warnings and to redirect possible output on stderr to /dev/null, but what if my_program exists twice, for instance?). Or you can also filter find 's output (only the first line). But well this is really a patch-up job.

  • it will be slow, even if you look for my_program only in /home/$USER , as in this example. A workaround could be to store the result of this command in a file, and read this file's content (if not empty) in order to run the find command only once. But this all would really be a hassle for something you can so easily fix only by setting $PATH correctly.

Finally

Also, do not forget to give my_program the execution rights, otherwise, you won't be able to execute it at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM