I wrote my program in C which includes calling a bash script and another external program from it (all of them located in the same directory as my C program). I set the path of the executing files strict so like:
char *path_for_the_script = "location/of/script.sh";
and compile my program.c without any special arguments..
Now I wonder how can I manage that this path gets set by the program, so that someone else could use it from his computer without changing the paths manually?
Load the path from a environment variable (also add a fallback or failure path if the variable is not set) and have your program being launched through a wrapper script, which the user can adjust.
Example
in yourprogram.c
char const * const path_for_the_script = gentenv("YOURPROGRAM_SCRIPT_PATH");
the programlauncher.sh
#!/bin/sh
export YOURPROGRAM_SCRIPT_PATH="..."
exec yourprogram $#
Instead of setting the path in your code, create a text file in the same directory and fetch the path information from the txt file. Thus anyone can modify the text file with required path and execute the program, with out any code modification.
create a file in same directory as source.txt
vi source.txt
location/of/script.sh
In your program, use file open operation and access the file contents and assign to path_for_the_script char buffer. This method works only when all the users share the same computer.
Related
Say I made and compiled a small program in C to count the bytes of a file, called filebyte. To run it I would use ./filebyte
Now I want to make it universal on bash, like for example to run a php file, I would use bash command php file.php, same way I would like to run my program, filebyte filename.
How do I do this?
Thanks!
I often create a bin/ directory in my home directory, for small custom applications.
You then need to add that directory to your PATH, which is a list of colon-separated paths that your shell searches for executables when you type a name on thr command line.
This is usually accomplished by putting this in your ~/.bashrc file:
PATH="$PATH:~/bin"
Check the environment variable PATH and put the executable in one of the directories listed. You can also put it in a custom directory and then append it to PATH. You can check it by executing printenv PATH
If you want it for your current active shell alone, do
export PATH=$PATH:</path/to/file>
For permanently making the file available add the above line to ~/.bashrc
Why add it in PATH variable, man bash says why,
PATH The search path for commands. It is a colon-separated list of
directories in which the shell looks for commands (see COMMAND
EXECUTION below). A zero-length (null) directory name in the
value of PATH indicates the current directory. A null directory
name may appear as two adjacent colons, or as an initial or
trailing colon. The default path is system-dependent, and is set
by the administrator who installs bash. A common value is
''/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin''.
I need to run a .lua file with another .lua file.
Here is my code:
Program = io.read()
dofile(program)
I type the name of the file that I want to run correctly, but for some reason my compiler says that the file I typed does not exist. (Which it does)
Lua is case sensitive; Program is not program
Otherwise the code should work.
Try putting the full path instead of the relative path. i.e. /home/nick/script.lua
Here is another(probably) noob question. Let us assume that I have a simple 1 file program (called myProg.c) written in C. When I want to compile this program in Linux/MacOSX, I type "gcc -o haha myProg.c". The executable file that is generated is now named "haha". When I wish to run this program, I will need to type "./haha" in the console.
What do I need to do to stop typing "./" always? How can I make sure that by just typing "haha", my program will be invoked? I have checked the permissions of the file "haha" and it is executable. Am I correct in thinking that the "./" indicates that the path of executable file i.e., the file is present in the current directory (".")??
The current directory is by default not a part of PATH in unix-derived OS'es. This is a security measure, which you can but should not change by modifying PATH in your .bash_profile or .bashrc
The reason not to include the current directory in path: Assume that you are root, and you have a malicious user. This user creates e.g. a ls executable in his home directory, which does something not nice. If you're looking at what this user is up to, and type ls while in his home directory, his ls will be executed.
If you want to just change it, add PATH="${PATH}:." to your .bashrc and .bash_profile
I want my program to create a file located at the path of the command parameter, i.e. program /home/user/directory/that/doesnt/exist/file. If the directory doesn't exist, fopen will fail to open the file, so the program would have to create the directories. Would I have to program my own loop to detect each slash and mkdir each directory or is there a function to do this automatically?
Someone coded it for you: mkpath()
Or if you're satisfied using system(), then you can use system("mkdir -p /your/path/here");
I am having a situation for which I am looking for some suggestion.
Suppose I write a program which prints the directory names of the directories.
Is it possible to convert this program into a command (just on my system).
Not be alias but via C only.
As long as the file is executable (has the exec x access for the user starting it) and can be seen from the command interpreter (usually bash or sh), you can consider it to be a command.
There will be no difference in running your own file from your path than the ls command for instance.
Also, the C (or C++ ...) language is not a requirement. There are plenty of commands in, for instance, /usr/bin that are a script, meaning they're sh or bash (or even perl)...
access Ensure the file has the x access right (e.g. chmod u+x file)
path Ensure the file is in your PATH, or add an entry in your path (for instance) with PATH=$PATH:mypath
test Test it well before to put it in a path from which other users may have access
Put it in the path. On Linux, for example, you should put it in /usr/local/bin.
First, compile the program and create an executable using gcc program.c -o myexecfile. Then, an executable file named myexecfile is created in the same directory. You can run it by using ./myexecfile.
If you are on Unix(Linux etc.) and want to use it like ls or any other standard command, you need to place it in a directory that is specified in $PATH variable. For example, /usr/local/bin.