The title says it all. I want to compile a program (say with gplc). Then pass an input file to it. The program will read the file, does some computation and writes the output to another file. In C, I would do something like this:
gcc -o prog prog.c
./prog input.txt output.txt
Can I do the same in Prolog? If it is, how can I read input arguments from commandline?
Thanks.
It might depend on the implementation you are using, but there is a builtin called current_prolog_flag/2 (read the manual) that can be called like this to access the command line arguments passed to a program:
...
current_prolog_flag(argv, Argv),
...
Argv will be unified with a list of atoms representing the command line arguments.
To tell Prolog which goal to run, again, consult the manual of the implementation you are using:
http://gprolog.univ-paris1.fr/manual/html_node/gprolog009.html#sec18
Related
I've built a little command interpreter (in C++) which can be invoked either directly, or in a script via shebang (#!). It can take arguments on the command line (which appear as argc/argv in my code).
Trouble is, when invoked via shebang, the script itself gets passed to my program as argument 1. That's problematic; I don't want my command interpreter trying to process the script that it was invoked from. But I can't see any easy way to tell when this is the case.
EDIT: As an example, if I have a script called "test" which starts with #!/usr/local/bin/miniscript, and then invoke it as ./test --help -c -foo, I get 5 arguments in my C code: /usr/local/bin/miniscript, ./test, --help, -c, and -foo. If I invoke it directly, then I get four arguments: /usr/local/bin/miniscript, --help, -c, and -foo
How can I tell when my program was invoked via a shebang, or otherwise know to skip the argument that represents the script it was invoked by?
My question was based on a wrong assumption. I believed that two things were happening when a program was invoked via shebang:
Path to that program was passed as the first argument.
Contents of that program were piped to stdin.
So I was essentially worried about processing the content twice. But only item 1 is true; item 2 does not happen (as pointed out by helpful commenters on my question). So if the C code accepts the name of a file to process as a first argument, and ignores any initial line starting with a shebang, then all is right with the world.
I am trying to automate one process which involves giving interactive input to a .exe utility. It expects the user-input for each step. I wanted to give all those values at single time while giving that command only. For eg: ./test.exe 8\n1\n0 etc. I have tried multiple ways to give input to the 'test.exe' utility like 8\n1\n0 | ./test.exe and 8,1,2 | ./test.exe. None of these worked. Any help how to pass these options 8,1,2 to the interactive utility test.exe in single line so that it will be helpful for my automation
There is no set way to automate a 3rd party program with Powershell. Not all utilities even offer the ability to do so.
I'd look in the utility documentation for any switches.
Try the following to see if you can get any built in help: test.exe -h, test.exe /h, test.exe /?, test.exe -?
use the sysinternals strings utility to try and find anything that look like command line switches inside the exe that you can take advantage of.
https://technet.microsoft.com/en-us/sysinternals/strings.aspx?f=255&MSPPError=-2147217396
The answer depends entirely on how your executable works.
If the executable reads from standard input, you can redirect input to it as follows (PowerShell):
PS C:\> 8,1,2 | .\test.exe
This won't work if the executable doesn't read from standard input or if it clears the console input buffer.
The executable may also let you provide command-line arguments that specify the needed input, but this depends on the executable.
I'm stuck on this little problem. I was wondering if it is possible to pass a variable of a bash-shell script to a f90 code?
I am pretty sure it was discussed here before, but I cannot find an exact duplicate.
You can pass arguments directly as arguments to the program
./program arg1 arg2
you can retrieve the values in the program as character strings in Fortran 2003 using subroutines GET_COMMAND ARGUMENT and COMMAND_ARGUMENT_COUNT. Click on the links to get useful examples.
In older Fortran you have to use non-standard extensions, such as the subroutines GETARG and IARGC.
You can also read an environment variable which was set in the script
VAR1 = ...
VAR2 = ...
./program
using Fortran 2003 subroutine GET_ENVIRONMENT_VARIABLE. In older Fortran you have to use some non-standard extension, such as the subroutine GETENV.
You can also redirect a file to the standard input of the program and read the data using the READ statement.
You can choose between the following possibilities:
In bash use export of the variable
myvar="example"; export myvar
Add them as argument to the fortran call
myFortran "${myvar}"
Write them to a file and read the file
Worst solution, just to mention them all
Write it to stdin of fortran program
echo "${myvar}" | myFortran
And you can use the following procedures to read an environment variable: get_environment_variable
https://gcc.gnu.org/onlinedocs/gfortran/GET_005fENVIRONMENT_005fVARIABLE.html
Or read the number and value of the command argument: command_argument_count,get_command_argument. See:
https://gcc.gnu.org/onlinedocs/gfortran/Intrinsic-Procedures.html#Intrinsic-Procedures
I have the following code, which is intended to run a java program on some input, and test that input against a results file for verification.
#!/bin/bash
java Program ../tests/test"$#".tst > test"$#".asm
spim -f test"$#".asm > temp
diff temp ../results/test"$#".out
The gist of the above code is to:
Run Program on a test file in another directory, and pipe the output into an assembly file.
Run a MIPS processor on that program's output, piping that into a file called temp.
Run diff on the output I generated and some expected output.
I made this shell script to help me automate checking of my homework assignment for class. I didn't feel like manually checking things anymore.
I must be doing something wrong, as although this program works with one argument, it fails with more than one. The output I get if I use the $# is:
./test.sh: line 2: test"$#".asm: ambiguous redirect
Cannot open file: `test0'
EDIT:
Ah, I figured it out. This code fixed the problem:
#!/bin/bash
for arg in $#
do
java Parser ../tests/test"$arg".tst > test"$arg".asm
spim -f test"$arg".asm > temp
diff temp ../results/test"$arg".out
done
It turns out that bash must have interpreted a different cmd arg for each time I was invoking $#.
enter code here
If you provide multiple command-line arguments, then clearly $# will expand to a list of multiple arguments, which means that all your commands will be nonsense.
What do you expect to happen for multiple arguments?
Is it possible to read binary in a Ruby file and execute it directly in memory?
For example, I want to do something like this:
x = IO.read('/bin/ls')
execute(x)
I tried system(x) but it returned:
ArgumentError: string contains null byte
I don't think you're going to be able to do that. When an executable starts, the dynamic linker needs to do quite a lot of fixing links up.
The simplest solution is to write the executable to a temporary disk file somewhere, and execute that.
system() and exec() both pass the command string to the OS to have it load and launch an external command, which isn't what you're asking to do.
For instance, this is from the system() documentation:
Executes command... in a subshell.
command... is one of following forms.
commandline:
command line string which is passed to the standard shell
cmdname, arg1, ...:
command name and one or more arguments (no shell)
[cmdname, argv0], arg1, ...:
command name, argv[0] and zero or more arguments (no shell)
Something could probably be written in C, as that is how Ruby is extended. There is lots of information on Google.