strange behavior - scripting

I wrote simple script test
echo hello #<-- inside test
if I press one time enter after hello, my script will run, if I don't press - it will not, if two times I'll receive my hello and + command was not found, can somebody please explain me this behavior thanks in advance
This is not a part of the code, this is actual code
and I run it on C-Shell, via editor of Windows
command:
source ./test

Some points:
You should not ask questions tagged with both the [csh] and [bash] tags - these are completely different programs and implement completely different script programming languages
You should never name a script (or any other program) test, as this is the name of a built-in feature of bash
Post the actual code you are asking about, without annotations and show how you run it.

I have tried a similar case. I wrote a script like yours, saved it using Windows Notepad (with CRLF line terminators) and run in bash with the same effect as yours in csh. The problem is bash (so csh as well) does not understand Windows' 2-byte line terminators, which are interpreted as commands, which obviously do not exist.
The solution is: change your editor or configure your current editor to use unix line terminators.
You can try for example Notepad++. Remember to change the line terminators to LF.

Related

Giving unique names to GDB logging files in a script

I would like to log GDB command output to a log file.
This is done using the following command:
set logging file outfulfile.txt
But, instead of simple outfulfile.txt, I would like to give a unique name to the file; for example outfulfile-PID.txt. This is because I will have several processes simultaneously producing the output and I want each one to log to its own unique file.
How can I programmatically derive such a file name in a GDB script?
There area few ways.
One relatively simple way is to use gdb's "eval" command. It substitutes arguments like printf, and then executes the result as a gdb command. This is a new-ish command.
If you don't have "eval" you might still have Python scripting. In this case you can write a short (one line) Python script like:
(gdb) python gdb.execute('set logging file ' + .. your logic here ..)
If you don't have Python scripting, then your gdb is really old and you ought to upgrade. However you can still maybe accomplish what you want, just with some awful gyrations using "shell" and writing out a script that you then "source" back into gdb. Though this technique seems somewhat hard in this case.

How to run a shell command in cocoa and get output?

After repeated searching I have not found an elegant solution to this issue: how to run a shell command in obj-c and get it's output. I have read many questions regarding this, but they all fail to answer my question.
Some get the exit value ( Get result from shell script objective-c ) others only run the command ( Cocoa/ Objective-C Shell Command Line Execution ), and finally others have me write the output to a file ( Execute a terminal command from a Cocoa app ).
I really would like to avoid writing/reading a file as it not a very clean solution.
Is there no way to read the output directly in obj-c? If so how?
The code from "doshellscript" from the first link (Get result from shell script objective-c) actually does return an NSString with the output of the command. If it's not working for you, maybe the command is outputting over stderr rather than stdin? Have you tried this yet? The standard mechanism for running commands in Cocoa is NSTask, so definitely at least start there.
Look at the class PRHTask. It is a replacement of NSTask, with completion blocks. https://bitbucket.org/boredzo/prhtask
Extract from the header:
First, rather than having to set your own pipe for standard output and error, you can tell the task to accumulate the output for you, and retrieve it when the task completes.
Second, when the process exits, rather than posting an NSNotification, a PRHTask will call either of two blocks that you provide. You can set them both to the same block if you want.
If your task needs admin privileges, you might want to look at STPrivilegedTask.

Faster way of testing your prolog program

I am new to Prolog, and the task of launching the prolog interpreter from the terminal, typing consult('some_prolog_program.pl'), and then testing the predicate you just wrote is very time consuming, is there a way to run a scripted test to speed up development?
For example in C I can write a main where I would use the functions I defined, I can then execute:
make && ./a.out
to test the code, can I do something similar with Prolog?
You can have the interpreter always open and then recompile the file.
You can auto-run a predicate after compiling the file:
:- foo(4,2).
This will run foo(4,2) when the line is encountered in the file.
There are flags that can be used while launching (most) Prolog interpreters that allow you to compile a file and run predicates (check the man page). This way you could make a Bash script. The following will consult file.pl and run foo/0 using SWI-Prolog:
#!/bin/sh
exec swipl -q -f none -g "load_files([file],[silent(true)])" \
-t foo -- $*
This predicate will unify Arguments with a list of the flags you gave at the command line:
current_prolog_flag(argv, Arguments)
But unless you are going to run a lot of tests, I don't think that writing all this extra code will be faster.
Personally I really like the flexibility of testing any predicate at any time with or without tracing (see trace/0) without having to write extra code to call them (unlike in C).
P.S. about reloading the file without leaving the interpreter: You might have some problem if you have used dynamic predicates or global variables; you will have to do some cleaning.
You can invoke a test file from the command-line with prolog +l <file>
Also, you can build a single run_tests predicate that exercises a series of calls and validates the actual results against expected results. Here's an article with a good worked-out example: http://kenegozi.com/blog/2008/07/24/unit-testing-in-prolog
In SWI, you can load things as usual. Then, when you edit your files you simply say make. on the toplevel and it checks all dependencies automatically and only reloads the modified files.
For bigger projects it does make a lot of sense to use makefiles. In particular to do unit testing. See SWI's package plunit.
For simple scripts in SWI-Prolog, using REPL to test the code manually is usually good enough. Changed files can be reloaded via make/0 (?- make. on toplevel). Just keep the Prolog REPL running while editing, then save the edits, run make. in the REPL and hit ↑, ↑, Enter to execute the last query before the make. from history.
The main benefit of REPL is its interactivity:
You may fiddle with the arguments.
Transition to debugging or tracing (both command line and graphical) is easy.
You don't need to perform I/O to print the result. Output is handled by the toplevel, which prints the substitution. You see the whole substitution, not only its part you just happen to print (possibly accidentally overlooking other parts).
You may interactively choose how many substitutions you want to see for a goal that succeeds multiple times.
It is obvious if there is a choice point left after the last result returned by a non-deterministic predicate, which is hard to observe otherwise. In that case, false. is printed when backtracking beyond the last result.
If you need to preserve the test calls to repeat them later, create a protocol (transcript or "log" of the interactive session) and edit it to become a script, or even a test suite (see below). The protocol is a plain text file with escape sequences for the terminal, containing a verbatim copy of what you see during the interactive session. View the protocol using cat protocol.txt on Linux (and other *NIXes) or type protocol.txt on Windows.
If interactivity is not needed, perform the test calls from the command line non-interactively. Let's test the CLP(FD) factorial example n_factorial/2, saved in factorial.pl (don't forget to add :- use_module(library(clpfd)). when copying the code):
$ swipl -q -t "between(0, 9, N), n_factorial(N, F), format('~D ', F), fail." factorial.pl
1 1 2 6 24 120 720 5,040 40,320 362,880
On Windows, you may need to specify full path to swipl.exe as it's not in the PATH, probably.
If the call is always the same, you may save it to a shell script or Makefile (run would be a good name for the target).
In your current workflow for testing functions in C, you create a new program and call the function under test from its entry point (main function). Prolog scripts can have an entry point, too. See library(main). Prolog does not require compilation, so you can just directly call the script (./test.pl) without calling Make first.
For larger projects, you may want to create a less ad-hoc test suite. A unit testing framework like PlUnit is needed. Its use is beyond the scope of this answer; see the documentation.

program's printf backslash n does not carriage return when compiled with Eclipse

I handed in a C program which contained a lot of verbose printf debug lines. I always compiled it command line with gcc.
Now it's been turned into an Eclipse-CDT (Helios) project, and my
\n
no longer do carriage returns. I get an unreadable "staircase" in my console.
RCINAHFM. Is there a check box in the IDE I need to modify or do I need to go back and carefully modify hundreds of lines of code?
Any help greatly appreciated.
Bert
RCINAHFM=Remaining calm / I need a hug from Mom
Eclipse does not compile C all by itself. It uses an external compiler for that, usually gcc. So it’s highly unlikely that the compiled program is incorrect, unless the compiler configuration within Eclipse does something very, very weird.
If you get a “staircase”, it sounds as if the new line part is carried out, but no carriage return happens. This might happen under systems that use CR/LF as their line ending, such as DOS/Windows.
Unfortunately, you give way to little detail. Are you using Unix or Windows? Where does the program run, in an XTerm, a Windows DOS console, within the Eclipse console? If the answer is “Eclipse console”, then have you tried running it in another terminal instead; or tried running your original program in the Eclipse console? Are you using printf or some other function?

How to write and execute programs in terminal/command prompt?

For example:
http://www.youtube.com/watch?v=tKTZoB2Vjuk (# 5 min)
I have no idea how one would go about writing code and making it work without an IDE but it seems like a useful thing to know how to do. Thanks!
Open an editor. Edit the code. Invoke the compiler. Run the executable.
Can't say much more without further details on your part.
What they're showing in the video though is the Python REPL.