I would like to write a short d program that fills the screen with pound symbols. Here is what I have
import std.stdio;
import std.process;
import std.conv;
void main(string[] args){
auto lines = environment.get("LINES");
int line_count = to!int(lines);
for(int a = 1; a <= line_count; a++){
writeln("######################################################################");
}
}
I expected this to work because when I execute "echo $LINES" from the terminal it prints "47". However, LINES appears empty when I run the program via rdmd in the same session. This is on Ubuntu Raring. Any ideas?
If you can grab the output of the command stty size, that's probably more reliable than examining the $LINES and $COLUMNS environment variables.
Or you can invoke the TIOCGWINSZ ioctl as described in this answer.
If you just want a simple fix, you can put export LINES COLUMNS in your ~/.bashrc to make these variables available in your program.
For a proper solution, you could try to invoke the ioctl TIOCGWINSZ, or find a D library that supports querying the terminal (such as ncurses wrappers).
Related
I can use only python 2.6.6 and subprocess is not working so I need to use only os module
Below is the program
import os
server = raw_input("server name:")
var = "symaccess -sid 239 list -type init | grep \"{0}\"".format(server)
wwn = os.system(var)
init = 'symaccess -sid 239 -type init show {0}'.format(wwn)
print init
os.system(init)
above is the script I used to add an output of one os.system to another os.system, I got the first os.system executed but for the second one i.e os.system(unit) is not coming because the output of os.system(var) should be assigned to a variable to wwn. could someone tell how to assign a variable to os.system(init)
Here in this script, the output of var says some X should be assigned to own but it's not taking X it's taking it as 0. So need your help to sort this why it is taking zero instead of X. Finally this X should be placed at init variable at {0}.
os.system does not return the output of the command - it returns the errorlevel.
If you need command output, use
wwn = os.popen(var).read()
That will assign the output from command var to wwn.
Be warned - the output is returned completely, with the trailing newline. You might want to strip() it before using it.
I'm writing a program in D that doesn't need a GUI. I remember that in C++, there was a way to remove a number of characters from console/terminal, but I don't know how to do this in D.
How do I remove a number of characters from the console/terminal?
(This didn't fit into a comment and I think it's what you are referring to)
Do you mean getchar? You have direct access to the entire standard C library in D. For example have a look at this simple script:
void main()
{
import core.stdc.stdio : getchar;
foreach(i; 0..3)
getchar();
import std.stdio;
writeln(readln());
}
When you compile & execute this script (e.g. here with rdmd)
echo "Hello world" | rdmd main.d
it would print:
lo world
But I have to agree with Adam that just slicing readln is easier and looks nicer ;-)
I want to redirect a .txt file from the command line into an exetuable written in D.
$ ./myprogram < data.txt
This text file consist of numbers that I want to print to the screen. So far, my program consists of this:
import std.stdio, std.file;
void main(string[] args) {
string file = args[2];
writeln(read(file));
}
But this is not correct; could someone explain me how redirects work and how I transfer the data in to my D program?
You can use stdin to read input like that:
import std.stdio, std.file;
void main(string[] args)
{
foreach (line; stdin.byLine())
{
writeln(line);
}
}
What you currently have tries to get the input file as an argument but your program never sees that because, when you do input redirection:
The shell attaches that file to your input stream stdio; and
The redirection bit is removed entirely from the command line before you get it.
In other words, when you do input redirection like that, the data shows up on the standard input stream and not via an argument.
Hence you will need to use readln or similar to get the data.
A classic way for UNIXy programs to handle this is to check their arguments. If one exists, it opens the file and processes that. If an argument is not given, it simply uses standard input.
That allows you to run your command in many different ways:
testprog # uses standard input (terminal).
testprog <myfile # uses standard input but from file.
testprog myfile # opens file and uses it.
I would use programming-quote like this in Bash
$ export b=`ls /`
$ echo $b
Applications Library Network System Users Volumes tmp usr var
and now I want to find similar functionality in Matlab. Also, I want to find a command that outputs relative paths, not absolute paths like Matlab's ls -- I feel like reinventing the wheel if I am parsing this with a regex. I need to find this command to debug what is wrong with namespaces here. Familiar-Bash-style functionatilities would be so cool.
For your first question, I think you can get that behavior with anonymous functions:
b = #() ls('C:\'); %I'm on windows
b()
The expression b() now returns the contents of my C drive.
The Matlab equivalent of bash backticks is calling the system() function and using the second output argument. It will run an external command and capture the output.
[status,b] = system('ls /');
If it's a string of Matlab code you want to run and capture the console output of, use evalc.
But to just get a listing of files, you want the Matlab dir function. Lots easier than parsing that output string, and you get more info. See Matlab dir documentation or doc dir for more details.
children = dir('/');
childNames = { children.name };
childIsDir = [ children.isdir ];
I am trying to use an external program from within Scala that accepts its input from the standard input stream.
Code roughly the equivalent to the following runs on Linux but raises an exception when run on Windows (tested on Windows 7). The exception's description states that the echo command cannot be found.
def invokeProgram(data : String) {
import scala.sys.process._
val cmdEcho = Seq("echo", data)
val cmdProgram = Seq("program")
println((cmdEcho #| cmdProgram).!!)
}
The code works correctly on Linux but fails as described on Windows, yet the echo command is common to both platforms and its usage is syntactically the same for my purposes. Is this as simple as echo not being in PATH? Is there even a separate echo.exe on Windows or is it bundled into something else? The invoked program can be made to accept its input from a temporary file which is what I will fall back to if I cannot resolve this issue.
The difference is this:
dcs#shadowfax:~$ which echo
/bin/echo
That is, on Unix the echo command is actually a binary, though most shells implement it as a builtin. On Windows, on the other hand, there's no binary called echo.exe (or echo.com, etc). It's solely a builtin command of the shell.
You don't need to use echo at all. Instead, use the #< method of ProcessBuilder. Here is an example:
import java.io.ByteArrayInputStream
import scala.sys.process._
val data = "hello"
val is = new ByteArrayInputStream(data.getBytes)
"cat" #< is ! //complicated way to print hello