disable cmd output in lua

2.2k views Asked by At

I used following lua code to create some files and write some text append the the game.txt

function makeFiles()
os.execute( "mkdir season\\week1" )
newFile = io.open( "season\\week1\\game.txt", "a+" )
newFile:write('123')
newFile:close()
end

makeFiles()

The cmd told me something like: "season\week1" already exists (translated from German). I am searching for a way to disable the output from the CMD via lua. After running my script there shouldn't be a output in the CMD telling me messages, I need to run the code in a silent way, the user shouldn't see this. I ask because I need to deactivate outputs from cmd generally using lua.

3

There are 3 answers

0
Etan Reisner On

That's not a lua question. If you want to silence the output from shell commands you silence the shell command.

The simplest way to do that for this specific instance is to use (assuming your system has it) the -p argument to mkdir which silences the error on already existing directories.

You could also (if -p doesn't exist for example) simply test for the directory first os.execute("test -d season\\week1 || mkdir season\\week1").

Finally, and more generally, if you don't want output from a command redirect the output to /dev/null: os.execute("mkdir season\\week1 >/dev/null 2>&1").

The above assumes a linux-like environment. I don't know if cmd.exe has similar redirection capabilities though I imagine it does. (>nul 2>nul apparently according to YuHao's answer)

3
Yu Hao On

On Windows you can redirect standard output and standout error to nul like this:

os.execute( "mkdir season\\week1 >nul 2>nul")

On Unix, it's similar:

os.execute( "mkdir season/week1 &> /dev/null" )
0
Pekinese On

I need to disable output to user, but I need to get the result from running a cmd command. I used this code now:

 f = assert (io.popen ('HELP'))
 --runs commands directly in cmd/terminal

 for line in f:lines() do
 --print(line)
end

f:close()