General source versus scripting question
I have a multi-file C++ program I wrote that takes a single input file, process the data in that file, and spits out multiple output files into a user-named directory. I was wondering if others often use support/wrapper scripts for these types of programs. For example, when the user specifies the output directory first I want to check that the directory exists, has the proper permissions, if it doesn't exist create it, etc. So there are two solutions I thought of here:
1) Use a wrapper script which does the following - Checks directory existence, permissions, etc. - Passes the program the rest of the arguments (but not the directory arg) - Assuming that the program always spits the output files in /somedir/program_dir/output, the script performs a move on the files when the program finishes to put them all in the correct directory 2) Do everything mentioned in 1 in the program itself Problem is, I don't know how to do #2 (but I think there should be a way?), but it's easier to manage than #1 and people might get confused into thinking that they should just run the program natively without the script. So what would you do? Script, or no script? This is more a question out of curious inquiry rather than a "help me" question mind you. :) |
If your just doing simple checks like "does the directory exist" and all that, I would not even tell the users about the C++ executable, and instead write a simple shell script to launch the C++ executuble after passing the shell script "checks".
I would write the wrapper in bash, assuming thats your environment, the things you mentioned are all doable, and relatively easy to accomplish in Bash - probably easier than performing the same task in C++ I might add. |
I agree with lowpro2k3. You could add an argument to your C++ program (like --checks-passed) that the C++ program will not run without to prevent users directly executing it.
|
Yeah I have a bash script half-written to do all this already.
Quote:
That's a great idea. :D Thanks! |
Quote:
|
Ok, here's a somewhat related question. How do you handle the balance when you're not necessarily in the scripts native PWD? For example, the structure of my program is like so:
program_dir (where the bash script for running the prog is) program_dir/bin (where the actual program executable is) program_dir/output (where the output files should temporarily be placed before moving) I want to be able to execute the run script from anywhere so I made a symbolic link to it in my personal ~/bin directory. So let's say I'm in a directory ~/data where a file my_data.dat resides. I want to do the following: runprog -i my_data.dat -o ../results/my_data Where -i specifies that I want to use the data in my PWD (~/data) and -o specifies that I want the output files to be located in ~/results/my_data/ when finished. But then things get screwy. The script looks for the executable at program_dir/bin/my_prog, but obviously it wouldn't be there in this case. So for a quick fix I wrote the script to call the program with it's absolute path prepended and that's fine and all, but the actual program stores its output files in "../output/" temporarily until the script regains control and moves them, but i this case my PWD is still ~/results/my_data/ where there is no "../output" directory. I can probably come up with a quick and dirty solution, like taking the $PWD when my script first starts up and prepending that onto both the -i and -o arguments so that everything uses the absolute path, and I'd have to modify the actual source code of my program to output the files in an absolute path and not a local one. But that's evil hacking! :( This program (and it's file structure) needs to be able to reside anywhere and be called from anywhere without requiring all this evil absolute path hacking, but I don't know how to go about solving that problem. Any ideas? |
Code:
OLDDIR=`pwd` |
Quote:
|
Quote:
script from just invoke the compiled program like this ... ./program -options $OLDDIR/$1 Cheers, Tink |
Ok, yeah I fixed it up so I don't think I'll have these issues anymore. It was simple really. I just decided to initally output the files to the PWD instead of to ../output (since they are moved by the script after the program finishes anyway and it looks like everything works ok now. I've just been working too much lately and I'm burnt out so my brain is not working as good as it should be. :cry: Here's the script source for anyone interested, although I had to change the name of the program to something generic for......reasons. ;)
Code:
#!/bin/bash |
All times are GMT -5. The time now is 01:23 PM. |