I am pretty new to this programming field and am stuck at a place.
This is wat i wanna do
I want to run an .exe from command line with parameters in Windows 7 through c++ and get the output produced by the command into the program.
the problem I am having is with changing the current directory of command prompt to the path of the exe and getting the output from the executed command into the program..
hope some one has ran into something such previously..
Thank you.
You can use System function or ShellExecute function for running exe from command line with parameters using Cpp. You can read about System function here and ShellExecute function here. As far as getting the output is concerned you can redirect the output to a file and read the file, or create pipes and redirect the output to pipe. You can provide the exe name along with the path so cd to directory wont be necessary, but I am not sure if its mandatory for you to run the exe from inside the same directory.
Related
I am currently working on a project in which the deliverable is a .sh file. When I run the file using ./file.sh it works just fine. But according to the prof. we have to run the file using this statement. Batch file.sh. This does not work, and the errors returned are batch accepts no parameters. Which makes sense after reading the man page.
What is the proper way to use the batch command? Is it even possible to run it the way that the professor wants?
According to your description, the file you are trying to execute shouldn't be executed with Batch but Bash/Shell.
Following Batch file, Batch is not supposed to run programs with the sh extension (sh stands for shell).
You can validate what interpreter the program you are trying to use should be run with with the following ways:
Checking the shebang
Using the file command (i.e. file ./your_program.sh)
After finding the correct interpreter, you can run the program with calling it (i.e. sh ./your_program.sh)
I have an executable of a fortran code which I want to run from MATLAB.
I tried using following two options but got errors:
!/home/atrac/code case172.jcl
error:- ls: cannot access ./id: No such file or directory
ls: cannot access ./id: No such file or directory
!gnome-terminal --command "./home/myhome/code case12.jcl"
error: There was an error creating child process for this terminal
Is there a way I can write a shell script to execute the program and pause Matlab till the external program is executed then pass the control back to Matlab?
I am trying to run a genetic algorithm in Matlab, which calls this external software.
Any ideas or help is greatly appreciated.
Thanks,
Yash
Okay, looks like two different things going on here. Both are probably related to Matlab's current directory.
!/home/atrac/code case172.jcl
error:- ls: cannot access ./id: No such file or directory
ls: cannot access ./id: No such file or directory
Here, it looks like you're managing to run your code program, but code or the JCL script is looking for a file named id in the current working directory. When you shell out from Matlab, that's going to be Matlab's current directory. Run pwd from the Matlab command prompt to find out where you are. You can probably fix this by using cd in Matlab to move to the directory where the id file actually exists (I'm guessing it's in /home/atrac), and then running code using the same command line. A better fix would be to rewrite code and/or the JCL script to work when run from any path (maybe by using absolute paths), so your cwd doesn't matter.
!gnome-terminal --command "./home/myhome/code case12.jcl"
error: There was an error creating child process for this terminal
Here, the leading "." is probably messing it up, because it's now looking for home/myhome under the current working directory, instead of under the root directory. Try doing !gnome-terminal --command "/home/myhome/code case12.jcl" (without the ".") instead.
Ideally, this should go as a comment, but I dont have enough reputation.
But nonetheless, the error with the creating the child process is unrelated to MATLAB. The shell is erroring out. Are you able to run the program from the terminal?
Secondly, you are using:
!/home/atrac/code case172.jcl
but you should be using
!./home/atrac/code case172.jcl
You can run a UNIX program from MATLAB when the folder containing that file is not on the UNIX system path that is visible to MATLAB. To determine the system path that is visible to MATLAB, type the following in the Command Window:
getenv('PATH')
You can make modifications to the system path that persist for the current MATLAB session or across subsequent MATLAB sessions, as described in the sections that follow.
Modify the System Path for the Current MATLAB Session. Do one of the following:
Change the current folder in MATLAB to the folder that contains the program you want to run.
Issue these commands using the Command Window:
path1 = getenv('PATH')
path1 = [path1 ':/usr/local/bin']
setenv('PATH', path1)
!echo $PATH
If you restart MATLAB, the folder is no longer on the system path visible to MATLAB.
http://www.mathworks.com/help/matlab/matlab_env/creating-opening-changing-and-deleting-files-and-folders.html#f0-38522
I created executables of a python script (via pyinstaller) for Mac, Windows and Linux. For Linux and Mac, I am running them in the shell since it doesn't have an own interface: just open a shell and type the name of the program.
I am wondering if there is a way to use certain file ending so if the user clicks on the program, it will be automatically executed in the shell or terminal. Alternatively, I would appreciate any other ideas of how to do this.
The way to do this is not to append a certain file ending, but, as pointed out in the comment, make the file executable (chmod +x <file>) and add the magic bytes to the beginning of the file that tell the system how to execute it.
The magic bytes are #! and are followed by the path to executable. So for a python script you would put something like the following at the top of the file:
#!/usr/bin/env python
Okay, now I finally found out the solution to my question. All you have to do to execute the program upon clicking on it in the file browser is to add the ending .command and make it executable
E.g., exampleprogram.command. Clicking on it will execute the program in the shell
So I am not experienced in dealing with a plethora of file types, and I haven't been able to find much info on exactly what .sh files are. Here's what I'm trying to do:
I'm trying to download map data sets which are arranged in tiles that can be downloaded individually: http://daymet.ornl.gov/gridded
In order to download a range of tiles at once, they say to download their script, which eventually leads to daymet-nc-retrieval.sh: https://github.com/daymet/scripts/blob/master/Bash/daymet-nc-retrieval.sh
So, what exactly am I supposed to do with this code? The website doesn't provide further instructions, assuming users know what to do with it. I'm guessing you're supposed to paste the code in to some other unmentioned application for a browser (using Chrome or Firefox in this case)? It almost looks like something that could be pasted in to Firefox/Greasemonkey, but not quite. Just by a quick Google on the file type I haven't been able to get heads or tails on it.
I'm sure there's a simple explanation on what to do with these files out there, but it seems to be buried in plenty of posts where people are already assuming you know what to do with these files. Anyone willing to just simply say what needs to be done from square one after getting to the page with the code to actually implementing it? Thanks.
What is a file with extension .sh?
It is a Bourne shell script. They are used in many variations of UNIX-like operating systems. They have no "language" and are interpreted by your shell (interpreter of terminal commands) or if the first line is in the form
#!/path/to/interpreter
they will use that particular interpreter. Your file has the first line:
#!/bin/bash
and that means that it uses Bourne Again Shell, so called bash. It is for all practical purposes a replacement for good old sh.
Depending upon the interpreter you will have different languages in which the file is written.
Keep in mind, that in UNIX world, it is not the extension of the file that determines what the file is (see "How to execute a shell script" below).
If you come from the world of DOS/Windows, you will be familiar with files that have .bat or .cmd extensions (batch files). They are not similar in content, but are akin in design.
How to execute a shell script
Unlike some unsafe operating systems, *nix does not rely exclusively on extensions to determine what to do with a file. Permissions are also used. This means that if you attempt to run the shell script after downloading it, it will be the same as trying to "run" any text file. The ".sh" extension is there only for your convenience to recognize that file.
You will need to make the file executable. Let's assume that you have downloaded your file as file.sh, you can then run in your terminal:
chmod +x file.sh
chmod is a command for changing file's permissions, +x sets execute permissions (in this case for everybody) and finally you have your file name.
You can also do it in your GUI. Most of the time you can right click on the file and select properties; in XUbuntu the permissions options look like this:
If you do not wish to change the permissions, you can also force the shell to run the command. In the terminal you can run:
bash file.sh
The shell should be the same as in the first line of your script.
How safe is it?
You may find it weird that you must perform another task manually in order to execute a file. But this is partially because of a strong need for security.
Basically when you download and run a bash script, it is the same thing as somebody telling you "run all these commands in sequence on your computer, I promise that the results will be good and safe". Ask yourself if you trust the party that has supplied this file, ask yourself if you are sure that you have downloaded the file from the same place as you thought, maybe even have a glance inside to see if something looks out of place (although that requires that you know something about *nix commands and bash programming).
Unfortunately apart from the warning above I cannot give a step-by-step description of what you should do to prevent evil things from happening with your computer; so just keep in mind that any time you get and run an executable file from someone you're actually saying, "Sure, you can use my computer to do something".
If you open your second link in a browser you'll see the source code:
#!/bin/bash
# Script to download individual .nc files from the ORNL
# Daymet server at: http://daymet.ornl.gov
[...]
# For ranges use {start..end}
# for individul vaules, use: 1 2 3 4
for year in {2002..2003}
do
for tile in {1159..1160}
do wget --limit-rate=3m http://daymet.ornl.gov/thredds/fileServer/allcf/${year}/${tile}_${year}/vp.nc -O ${tile}_${year}_vp.nc
# An example using curl instead of wget
#do curl --limit-rate 3M -o ${tile}_${year}_vp.nc http://daymet.ornl.gov/thredds/fileServer/allcf/${year}/${tile}_${year}/vp.nc
done
done
So it's a bash script. Got Linux?
In any case, the script is nothing but a series of HTTP retrievals. Both wget and curl are available for most operating systems and almost all language have HTTP libraries so it's fairly trivial to rewrite in any other technology. There're also some Windows ports of bash itself (git includes one). Last but not least, Windows 10 now has native support for Linux binaries.
sh files are unix (linux) shell executables files, they are the equivalent (but much more powerful) of bat files on windows.
So you need to run it from a linux console, just typing its name the same you do with bat files on windows.
Typically a .sh file is a shell script which you can execute in a terminal. Specifically, the script you mentioned is a bash script, which you can see if you open the file and look in the first line of the file, which is called the shebang or magic line.
I know this is an old question and I probably won't help, but many Linux distributions(e.g., ubuntu) have a "Live cd/usb" function, so if you really need to run this script, you could try booting your computer into Linux. Just burn a .iso to a flash drive (here's how http://goo.gl/U1wLYA), start your computer with the drive plugged in, and press the F key for boot menu. If you choose "...USB...", you will boot into the OS you just put on the drive.
How do I run .sh scripts?
Give execute permission to your script:
chmod +x /path/to/yourscript.sh
And to run your script:
/path/to/yourscript.sh
Since . refers to the current directory: if yourscript.sh is in the current directory, you can simplify this to:
./yourscript.sh
or with GUI
https://askubuntu.com/questions/38661/how-do-i-run-sh-scripts/38666#38666
https://www.cyberciti.biz/faq/run-execute-sh-shell-script/
open the location in terminal then type these commands
1. chmod +x filename.sh
2. ./filename.sh
that's it
I installed ender js and am running it from the command line. It generates a file called ender.js. Whenever I run the command "ender info" or anything with ender in it, it opens the ender.js file in notepad.
How can I stop the file from opening and get the command to run? It runs when I delete the file, but I need the file.
Windows runs it's own JS executor in the command line. This is in the PATHEXT system variable. This system variable stores an order for file extensions to be executed if there are files in the current directory that match. When I remove JS from that list, the command I am trying to call works. It does mean I won't be able to execute JS files in the browser now, but I have never needed to do that so far.
So uou are trying to run a javascript from the command line? that is what i infered here sorry if i am incorrect. That is done with the C:> cscript jslint.js command from the command line.
You can usually get around this by typing ender.cmd or ender.bat (depends how it is installed).