Running Shell Script from CPP , while Script bundled inside the exe - c++

I wanna create a exe which has the Shell Script and Simple CPP file which calls the Shell Script using system() function. Lets say exe name 'myInstaller' which has files myintsaller.cpp and myShell.sh. When i run the exe myInstaller , it must execute shell script. I want to do like this so i can protect my Shell Script code ,which has over 3000 lines of Code.
How do i do this ... I m in real need of this.

As far as i've searched, there is no way to bundle the script inside the exe. Either it would be Shell Compiler as shelter suggested or plain shell scripts. No work around for this.

I guess it's Windows platform since you speak about exe. In this case you can add your script as a resource to your exe, extract it in run-time and execute.

You could keep the script as a string in your C++ code and call system("sh -c '" + code + "'"). Quoting may be an issue though. Otherwise, write it to a temp location, execute it and then unlink it.

Related

How to check if a script exists and source it if it does

I do not see native GDB commands can do this: check if a gdb script file exists, if so, source the file.
Maybe I need to resort to python, but still want to ask here.
The use case is, I am working on several different computing environments, each one has some different GDB setup. I like to add the above to ~/.gdbinit so that even in each environment I have a different GDB script, it is automatically sourced in my ~/.gdbinit.
There's no built-in way to do this. It can be done via scripting in a couple of different ways.
The classic way is to use shell to test the file existence and have it write out a file that then decides what to do. Something like:
(gdb) shell if test -f blah; then echo source blah; fi > /tmp/F
(gdb) source /tmp/F
This is pretty ugly, but it would work.
Alternatively you can write a Python command to do this quite easily. Or you can dig up the Python ignore-errors command, and just use ignore-errors source whatever.

How can I pass commands to the elevated application in a batch file?

I was wondering if anyone can help me. I'm currently working on a game engine project which involves its own c++ compiler. For this I'm "borrowing" the visual studio 2013 c++ compiler. everything works fine. The problem I am having is a cant figure out how I would pass commands to the elevated program in a batch file.
Let me Explain, right now I am using a program which calls the "vcvarsall.bat" file and passes in "x86" as a parameter. This is great for manual entry as it then allows me to input the commands to compile files. E.G "cl /EHsc <cpp files>"
As of now, when I add commands after I call "vcvarsall.bat", they just give me a command reference error saying the command is not recognized.
What I want to achieve is being able to call one bat file which executes and compiles all of my code for me. instead of having to manually type in the commands every time. This way the entire process is easier for the user.
Any help would be appreciated,
Thank you in advance!
when I add commands after I call "vcvarsall.bat"
Maybe it has been too long since I last did a batch file .. hope the following gets you started:
I think any .bat file will accept parameters, and internally, the .bat writer (i.e. you) uses special identifiers. Often they are named something like %1 and %2, etc. (some scripting languages use $1, and probably a few other approaches)
Without consuming these parameters in your .bat file, the command line interpreter tries to use the parameter as another command (so you get 'command not recognized')
Search all .bat files on your system for %1 (or $1 or whatever) ... maybe you'll find enough hints.
Thank you all for the help, the way I solved the problem was by finding the last batch file which was called and making the end of the file call another batch file in the main compile directory, this means I can programatically generate this batch file making it incredibly easy to generate custom compilations. thank you all,
Alister

Using tree Command in C++ on C://

I want to use the Windows CMD tree command in my C++ console application. My code:
system("cd c:/");
system("tree");
The problem is that the command tree will execute on the folder path where the program is running and not on C://. Is there a way to fix this?
Why not :
system("tree c:\");
?
TREE [drive:][path] [/F] [/A]
/F Display the names of the files in each folder.
/A Use ASCII instead of extended characters.
You can use SetCurrentDirectory from windows.h. This page has a demonstration:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363806%28v=vs.85%29.aspx
Your problem is that the system("cd c:/") is executed in a shell, and then the shell exits. [It's also wrong, because you use the wrong kind of slash, it should be "cd c:\\" - the double backslash is needed to make one backslash in the output, assuming we're talking about a Windows system].
There are a couple of different ways to do this:
Use chdir() (or SetCurrentDirectory) function call to change the main processes current working directory, and then call system("..."). This is the simplest solution.
Generate all your commands into a batch file, then pass the batch file to system.
Open a command shell with something like _popen() and pass commands into the pipe that you get from that.
Manually create pipes and connect them to the standard in and standard out of a process that runs the command prompt.
Just for programs in Windows, include "windows.h", then
SetCurrentDirectory("c:/");
system("pwd");
While I'm still curious why would you want to do this, you can try to run all commands in one system() call:
system("cd c: && c: && tree");
Second c: is needed to change drive letter, in case if you're not currently on drive c: (because cd doesn't do it).

running a .jar file from the start=>run box in Windows 7

I've written some code and made a .jar file out of it, and I want to be able to run this code from the start=>run box in the Start menu. After a lot of trial and error, I made sure to construct my .jar file the right way, and I set the proper .jar file type association so that my computer recognizes to run the .jar file using java.exe.
Doing all this enabled me to run the .jar from the command window, typing "java -jar myJar.jar", but it won't run from the start=>run box (even when I add in the .jar's filepath). What should I do?
Also, I'm not sure if I could run a .jar from the run box that takes arguments - is it possible to do that?
You'll need to tell Java which class to is the main class. If you intend to distribute the application you should probably make a Manifest file with a main-class attribute.
See: http://download.oracle.com/javase/1.4.2/docs/guide/jar/jar.html#JAR%20Manifest
If you just want to get the damn thing running, this command should work...
java -jar myJar.jar MyClass
... analogous to how you'd normally write...
java MyClass
... at the command line.
BTW, it might be worth mentioning the javaw command, which which works just like the java but launches a graphical application without showing a command prompt, on Windows.
You should have a look at the links below:
http://download.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
http://download.oracle.com/javase/tutorial/deployment/jar/manifestindex.html
You probably did not add the the "Main-Class:..." in your manifest file.
http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
You have to have a special entry in your MANIFEST.MF file inside your .jar that points to the entry point class in your .jar file to make it executable without specifying a class on the command line.
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: [fully qualified path to the class with the main method]

How to bundle C/C++ code with C-shell-script?

I have a C shell script that calls two
C programs - one after the another
with some file handling before,
in-between and afterwards.
Now, as such I have three different files - one C shell script and 2 .c files.
I need to give this script to other users. The problem is that I have to distribute three files - which the users must keep in the same folder and then execute the script.
Is there some better way to do this?
[I know I can make one C code file out of those two... but I will still be left with a shell script and a C code. Actually, the two C codes do entirely different things... so I want them to be separate]
Sounds like you're worried that your users aren't savy enough to figure out how to resolve issues like command not found errors and the like. If absolutely MUST hide "complexity" of a collection of files you could have your script create the other files. In most other circumstances I would suggest that this approach is only going to increase your support workload since semi-experienced users are less likely to know how to troubleshoot the process.
If you choose to rely on the presence of a compiler on the system that you are running on you can store the C code as a collection of cat $STRING >> file.c commands to to create your two C files, which you then compile and use.
If you would want to use pre-compiled programsn instead then the same basic process can be used except instead use xxd to both generate the strings in your script and reverse the conversion process to give you working binaries. Note: Remember to chmod the binary so that it is executable.
use shar command to create self-extracting archive.
or better yet use unzipsfx with AUTORUN option.
This provides users with ONE file, and only ONE command to execute (as opposed to one for untarring and one for execution).
NOTE: The unzip command to run should use "-n" option, that way only the first run would extract the files and the subsequent would skip the extraction.
Use a zip or tar file? And you do realize that .c files aren't executable, you need to compile & link them first?
You can include the c code inside the shell script as a here document:
#!/bin/bash
cat > code.c << EOF
line #1
line #2
...
EOF
# compile
# execute
If you want to get fancy, you can test for the existence of the executable and skip compiling them if they exists.
If you are doing much shell programming, the rest of the Advanced Bash-Scripting Guide is worth looking at as well.