I have a project where there are several helper scripts that call the main executable with different command-line options. Right now, the scripts assume the executable is in the same directory, so the calls to the executable in the script look like ./my_program. This, however, is not very flexible. What if the program is installed in the /usr/bin directory, and is not in the current directory?
Is there a way, using automake or autoconf, to generate these scripts, and substitute the calls to the executable with either ./my_program or just my_program, depending on whether or not the executable is already installed?
Sure. IMO the simplest solution with autotools would be:
create new m4 macro under m4/ folder that finds a path of your program, and sets it to a variable.
For example, you created a macro:
MY_PROGRAM_PATH_CHECK([action-if-found], [action-if-not-found])
This macro creates MY_PROGRAM_PATH variable if path is found.
configure.ac
MY_PROGRAM_PATH_CHECK(,[AC_MSG_ERROR([my_program path not found, woot?])
AC_SUBST(MY_PROGRAM_PATH)
AC_CONFIG_FILES([src/script1.sh], [chmod +x src/script1.sh])
AC_CONFIG_FILES([src/script2.sh], [chmod +x src/script2.sh])
convert your scripts to .in files, so the substitution would happen:
src/Makefile.am
bin_SCRIPTS = script1.sh script2.sh
src/script1.sh
#MY_PROGRAM_PATH#/my_program --option1
src/script2.sh
#MY_PROGRAM_PATH#/my_program --option2
Related
I want to add a small script to the linux PATH so I don't have to actually run it where it's physically placed on disk.
The script is quite simple is about giving apt-get access through a proxy I made it like this:
#!/bin/bash
array=( $# )
len=${#array[#]}
_args=${array[#]:1:$len}
sudo http_proxy="http://user:password#server:port" apt-get $_args
Then I saved this as apt-proxy.sh, set it to +x (chmod) and everything is working fine when I am in the directory where this file is placed.
My question is : how to add this apt-proxy to PATH so I can actually call it as if it where the real apt-get ? [from anywhere]
Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.
Try this:
Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
Add ~/bin to your PATH, typing export PATH=$PATH:~/bin
If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.
Then you can just run apt-proxy with your arguments and it will run anywhere.
Note that if you export the PATH variable in a specific window it won't update in other bash instances.
You want to define that directory to the path variable, not the actual binary e.g.
PATH=$MYDIR:$PATH
where MYDIR is defined as the directory containing your binary e.g.
PATH=/Users/username/bin:$PATH
You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.
Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you could name your script as apt-get and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.
I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some trojan variant of a common utility (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.
As a final step, after following the solution form proposed by #jlhonora (https://stackoverflow.com/a/20054809/6311511), change the permissions of the files in the folder "~/bin". You can use this:
chmod -R 755 ~/bin
make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.
adding to #jlhonora
your changes in ~./bashrc or ~./zshrc won't reflect until you do
source ~./zshrc or source ./bashrc , or restart your pc
I am trying to install some scripts into linux and follwoing line is given as instruction.
Install the xyz script into some convenient directory in $PATH.
but I'm unable to understand what exactly does it mean.How do I install given script in $PATH directory.Script is placed under /users/username/ Dir.
In a terminal type echo $PATH. You will see a list of directories. Put your script, or a link to your script in one of those directories, typically in /usr/local/bin.
its a bad description of the script-author :) usually, in your $PATH are multiple directories mentioned, separated by colon.
you can echo them:
echo $PATH
What the Author means: just copy the script in a directory which is in your $PATH, e.g. /usr/local/bin
$PATH is a list of directories where bash looks for executables.
The given instruction suggests that yours scripts should be put in one of these directories.
An alternative is to put your scripts in any directory and add that directory to $PATH. In your case, add the following line in your $HOME/.bash_profile configuration file:
export PATH=$PATH:/users/username
I am trying to do this:
I want to call a make (Makefile exists in some other directory, abc path can be used) from a shell script located in a different directory. How do I do this?
Since shell scripting does not allow me to cd into the Makefile directory and execute make, how can I write the shell command (by giving path to the Makefile to be executed) to execute make?
GNU make accepts many options, notably -C to change directory before running, and -f for giving the Makefile to follow.
Combine them appropriately.
Consider using remake to ease debugging (notably with -x) of Makefile related issues. With GNU make version 4 or better, also use make --trace...
You could have your own executable shell script (e.g. in your $HOME/bin/ which would be in your $PATH) which uses both cd and make).
You could consider other build automation tools (ninja perhaps)
Read also P.Miller's paper Recursive Make considered harmful
I haven't written any Shell scripts before, but i have to write a simple shell script to do the following;
I will keep all the required files in a single folder and bundle it with this shell script as a tar file; so when the user runs the shell script, it needs to copy the respective files to the respective destinations.
The execution of copy as follows:
copy the plugin.so file to /usrlib/mozilla/plugins/
copy the .so library files to /usr/local/lib/
copy some header files directories(folders) to /usr/local/include/
and finally, need to do ldconfig.
Basically, you can add in a script any command you are able to type inside the terminal itself. Then, you have two options for executing it:
Execute it from the terminal with sh your_script.sh. You don't even need to give execute permission to it with this solution.
Give it the execute permission and run it with ./your_script.sh.
For the second solution, you have to start the file with what is called a shebang. So your script will look like:
#!/bin/sh
cp path/to/source path/to/destination
cp path/to/source path/to/destination
cp path/to/source path/to/destination
ldconfig
echo "Done!"
Nothing else. Just write the commands one after the other.
The first line is the so-called shebang and tells the shell which interpreter to use for the script.
Note: the extension for shell scripts is usually .sh, but you can actually name your file however you prefer. The extension has no meaning at all.
Good scripting!
I have a program written in C, which is named computeWeight.c and to compile it i use the following code
chaitu#ubuntu:~$ gcc -Wall -o computeWeight computeWeight.c
//to execute it:
chaitu#ubuntu:~$ ./computeWeight
Do i have any mechansim where i can directly use as mentioned below,
chaitu#ubuntu:~$ computeWeight
Should i be changing any permissions on the executable to get this?
You need to add "." to your path. Some people regard this as dangerous, though. See for instance http://www.arsc.edu/support/policy/dotinpath.html .
The $PATH variable define the places where linux would look for executables (try typing echo $PATH in a terminal). You need to put that file in one of those places. One way is to add a bin folder in your home directory, put the executable file there, and add this line (which adds the bin directory in your home folder to the search path) to your .cshrc file so that it'd be executed for every shell:
set PATH = ($PATH $HOME/bin)
With that said I don't think typing ./ is that bad.
export PATH=$PATH:.