With the cmd.exe, i call a programm that I have not write myself. I cannot modify it. I would like to disable the message printed in the shell by this programm.
My OS is Windows 7 enterprise 32 bits.
Thank you
If it's outputting to standard error and output, you should be just able to redirect them:
dodgy_prog >nul: 2>nul:
(or use /dev/null under UNIXy operating systems - it's unclear which variant you want since you talk about the Windows cmd.exe but your question is tagged bash).
Related
I have a Perl script on Win10 that uses the system() command to run a couple of different command line processes, including:
Start Windows Media Player with a specified .mp3 file.
my $cmd = "start call wmplayer.exe myRadoShow.mp3";
system($cmd);
Start another Perl program that does something else at the same time (specifically reads and broadcasts a set of timecodes and titles).
my $cmd2 = "secondScript.pl some_params";
system($cmd2);
All of this works correctly; the minor problem is that #1 above starts up a new command line window each time it executes the system($cmd) command. I have to later go back and close those windows.
If I don't use "start call" the Perl script doesn't continue to #2.
Is there a preferred way to execute #1 that doesn't leave these windows open?
I realize this question may be more about Windows commands than Perl.
Firstly, If youre using active perl versions, there is a wperl.exe you can call your script with instead of the default perl.exe. Not to sure if this will hide sub processes created by your script but you can give it a go.
If that doesnt work, then maybe you can use Win32::GUI:
use Win32::GUI;
my $hw = Win32::GUI::GetPerlWindow();
Win32::GUI::Hide($hw);
there are other modules that provide this functionality as well. Good luck!
I noticed that Windows 7 enables to execute .sh files as if they were .bat files. That got me wondering whether it is possible to write a .sh file such that it can be executed in Windows and Linux (let's say bash).
The first thing that comes to my mind is to fabricate an if-statement such that Windows and Ubuntu can deal with it and jump into the according block to execute plattform-specific commands. How could this be done?
Note: I know this is not good practice. I also know that scripting languages like Python are far better suited to solve this problem than a mixed-syntax command line script would be. I'm just curious...
You could use this:
rem(){ :;};rem '
#goto b
';echo sh;exit
:b
#echo batch
It's valid shell script and batch, and will execute different blocks depending on how it's run.
Modify the echo and #echo lines to do what you want.
AFAIK, you can't directly run .sh files from Windows' cmd.exe. For that you'll need a *nix emulation layer to run the shell. Check out Cygwin or Msys/MinGW
I need to do a fair bit of scripting in my job as a SQL Server DBA. Sometimes, I need to deploy a fix script to a very restricted environment, where the only option for scripting may be DOS Batch. In one such environment, even VBScript/WSH isn't a possibility, let alone PowerShell. Anyone who has written enough batch files on DOS and Windows knows that it's very limited and a huge PIA when you need to do anything too complicated. This is especially true for folks who have worked with Unix shell scripting, Perl, Tcl, Python, Ruby, etc.
A possible solution to this would be a CMD preprocessor that would add some of the useful functionality from more capable scripting languages. I've tried to find such a utility, but so far I've had no luck.
Which finally leads to my question: is anyone aware of a such a CMD preprocessor? If not, what functionality would you like to see in one?
Addendum:
If you're unfamiliar with the idea of a preprocessor see this Wikipedia entry.
To clarify, I'm thinking of a tool that would add features like:
Functions
Backtick (`) ala Unix shell
...and possibly others. Those are two features I've wished CMD had and can think of a way to implement them with a CMD preprocessor. Functions could be implemented with env vars and GOTO/labels; backticks by piping to a temp file and using set /p =< to read in the result to an env var.
You can already achieve these same ends, but it gets to be very tedious and verbose- which is how I came to the idea of having a preprocessor handle the boilerplate for features like those.
Example
Using the example of backticks, here is an example of unprocessed code from my hypothetical Batch++ and processed vanilla batch script, ready to be run by CMD.exe:
Batch++ Source (test.batpp)
copy `dir /b /s c:\ | find "CADR-README.htm"` \\srv01\users
Run it through the preprocessor
bpp test.batpp > post_test.bat
Resulting CMD/BAT code (post_test.bat)
dir /b /s c:\ | find "CADR-README.htm" > _bt001.tmp
set /p _BT001 =< _bt001.tmp
copy %_BT001% \\srv01\users
set _BT001=
del _bt001.tmp
I am not sure to interpret correctly your question. If you run in a controlled environment that doesn't allow you to run any scripting extension, how are you going to access such a preprocessor?
However, and in regard of the two features you request, you are OK with .BATs. Both features are supported by BAT processing in current Windows versions.
Functions: you have the extended CALL syntax, that supports parameter passing thru argument references %1 .. %9, and enhanced with expansion substitution using %~ syntax. Read HELP CALL.
Backtick: not sure what you want but, in the FOR /F command you may pass a backticked string to be run and its output captured. Read HELP FOR.
What is the best way to programatically determine if a Perl script is executing on a Windows based system (Win9x, WinXP, Vista, Win7, etc.)?
Fill in the blanks here:
my $running_under_windows = ... ? 1 : 0;
From perldoc perlvar:
$OSNAME
$^O
The name of the operating system under which this copy of Perl was built, as determined during the configuration process. The value is identical to $Config{'osname'}. See also Config and the -V command-line switch documented in perlrun.
In Windows platforms, $^O is not very helpful: since it is always MSWin32, it doesn't tell the difference between 95/98/ME/NT/2000/XP/CE/.NET. Use Win32::GetOSName() or Win32::GetOSVersion() (see Win32 and perlport) to distinguish between the variants.
$^O eq 'MSWin32'
(Source: The perlvar manpage)
Use Devel::CheckOS. It handles all of the logic and special cases for you. I usually do something like:
use Devel::CheckOS qw(die_unsupported os_is);
die "You need Windows to run this program!" unless os_is('MicrosoftWindows');
The 'MicrosoftWindows' families knows about things such as Cygwin, so if you are on Windows but not at the cmd prompt, os_is() will still give you the right answer.
This is very quick and dirty, and wouldn't bet it's 100% portable, but still useful in a pinch.
Check for presence of back slashes in the PATH Env variable, since PATH is common to both Windows and Unix.
So - in Perl:
if ( $ENV{PATH}=~m{\\} ) {
#Quick and dirty: It's windows!
print "It's Windows!";
} else {
print "It's Unix!";
}
Is there a way, to send commands to another command-line program?
'Cause i have a special command-line program, but I can't send commands to it using syntax like program.exe something_to_do
the program executes something like this: ("here syntax" is where i want to input text to and also enter to start)
TheWhateverCommandLineProgram
Version 1.1
Give an option: "here syntax"
the program in code looks something like this:
echo TheWhateverCommandLineProgram
echo Version 1.1
Set opt=
set /p opt=Give an option:
if %opt%==command1 goto com1
if %opt%==command2 goto com2
...
Well, i guess so cause it wasnt me who made it (btw: off course its not called TheWhateverCommandLineProgram)
If you just want to give keyboard input to a commandline program you can just use echo and pipe it:
echo some text | program.exe
If you need more lines, then write them to a file and use input redirection:
echo one line > file
echo second line >> file
program.exe < file
I'm not 100% sure I understand what you're looking for. Here's two options:
You have two windows, each running a batch program. Let's say they are called myscript1.bat and myscript2.bat. You want to send a set of commands from myscript1.bat to be executed by myscript2.bat
You have a single batch script named myscript.bat, which executes a single program named program.exe. You want program.exe to execute some commands, or do some something.
Are either of these what you're looking for? Here's some idea:
Make myscript1.bat create a third file, mycommands.bat. Once myscript2.bat sees the file mycommands.bat exists, it will execute it and delete it. (Wow. Lame.)
Use Windows Scripting Host command (it's built in to Windows since Win2K) or Powershell (usually on most computers nowadays, if they have been updated). Either of these can send keystrokes to another program. Using those keystrokes, you can control the other program.
In what form does the other program take input? From the command prompt?
If the latter then I recommend Autohotkey: http://www.autohotkey.com/
You can use Autohotkey as a bridge and it will send the command as keypresses to the window of the other batch file.
You can ask for help in their forum. They are quite helpful.