Cannot run multiple commands in one line [duplicate] - cmd

This question already has answers here:
WinSCP script not executing in batch file
(2 answers)
Closed last year.
I am trying to run multiple commands in one line. The codes are below.
WinSCP.com
open user:pw#address
lcd C:\Users\xx\Desktop
get *.xlsx
exit
If I run them one by one, it will work. However, if I want to run them all together using one line, it will fail, no matter I am using ; or &.
For example, if I run WinSCP.com & open user:pw#address, only the first part will be executed.
WinSCP.com; open user:pw#address doesn't work either.
How to execute them using one line?
Thanks

This is happening, because both command line and winscp.com are programs. When you're entering commands one by one you start winscp.com and next entered commands are going directly to the WinSCP. When you're trying to execute all commands at once it is starting WinSCP and waiting for it to finish and then executing next commands.
You definitely can create a text file with your commands and save it as script.txt
open user:pw#address
lcd C:\Users\xx\Desktop
get *.xlsx
exit
And then pass it to the winscp.com application like this:
winscp.com /script=script.txt
It may be possible to send all the commands at once by using /command cmd1 cmd2 ... where cmd1,2 are your commands, but it might be tricky.
References
Command-line Options :: WinSCP
Parameters

You can put the sequence of commands in a script file used by WinSCP and call it using the command sequence:
WinSCP.com /script="C:\Users\{name}\Documents\Scripts\{SomeFileName}.scp"
I have found it easier to keep track of what you are doing and keeps the command sequence easier to read.

These are all the possible ways of running multiple commands on a single line :
When you use command1 & command2 on the command prompt, it
executes the first command, and then the second command.
command1 && command2 it runs the first command, and then runs the second command only if the first command completed successfully.
command1 || command2 it runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).
(command1 & command2) used to group or nest multiple commands.
Since in your case, you want to run the first command and then do the other operation with-in it; it isn't possible to use any of the above.
You have to write a script which you may call in the same line using a command.
You may do something like this : (WinSCP.com /script=<script_path>) && exit
There are many more options in WinSCP.com command.

Related

running mulitple commands using Subprocess.Popen in the same shell

Hi I am at a loss on how to run multiple commands using popen,
I am trying to automate a series of steps that are normally run on the Windows command line. The basic steps are usually run from the Windows cmd line are
Run a windows command script (.cmd) file to setup environment variables i.e C:\Program Files (x86)\appsettings\setupvariables.cmd
type in the command to connect to the database
type in the command to get data from the database
Stop connection to the database
All these commands must run in the same command line window one after another, not separate processes or separate command line windows. Instead of opening a cmd window and typing in the command I want to use python's subprocess.popen command
So far I have:
args=[]
args.append(r'C:\Program Files (x86)\appsettings\setmyvars.cmd')
args.append(r'start db on db_path="my_url"')
args.append(r'get_data_from_db>c:\temp\output.txt')
args.append(r'stop db on db_path="my_url"')
p=Popen(args,stdout=PIPE,sterr=PIPE,shell=True)
stdout,stderr=p.communicate()
if stderr:
print "you have an error", stderr
else:
print "well done you have data", stdout
This isn't quite working I can see that the first line is run i.e the setmyvars.cmd is executed, but nothing else, none of the other arguments get called, if they did I would see the results in the ouput.txt file.
How do I run a series of commands one after the other using popen. Why is it only the first command seems to be executed and none of the others
I am using python2.7 on Windows
Regards.
You have a couple of issues going on. You still have to tell popen() which program to run. Just using shell=True does not obviate the need to provide cmd.exe as the program to run. If you really want to run all of these commands with one invocation of cmd.exe, then you will need to string them together with &&.
from subprocess import *
args=[]
args.append(r'C:\Windows\System32\cmd.exe')
args.append(r'/C')
args.append(r'(echo 1 && echo 2 && echo 4)')
p = Popen(args,stdout=PIPE,stderr=PIPE,shell=True)
stdout,stderr=p.communicate()
if stderr:
print "you have an error", stderr
else:
print "well done you have data", stdout
It would probably be better to use the %ComSpec% environment variable than it would be to hardcode the location of cmd.exe. The path you have is -usually- correct. :-)

Basic Shell Script call command from another director with feedback

i'm trying to create a shell script so that it calls two commands in 2 seperate directories and then shows their feedback, To call the a command i'm guessing it would be something like this ./directory/ ./script.sh
Thanks in advance for your replies.
If you want to sequentially invoke the commands:
/path/to/command1; /path/to/command2
If you want to call the second command only if the first one succeeded:
/path/to/command1 && /path/to/command2
If you want to run them in parallel:
/path/to/command1 &
/path/to/command2
The output of the commands will be the standard output (most likely the terminal). If you run the two commands in parallel and they produce some output, you might want to redirect it to different files.

execute user inputed Windows (or bash) commands from batch (or bash) file?

Ok, so I have this batch script and what I want to happen is that when you run the script it does some standard stuff like change the path and access files etc... but after it's done that it goes back to being a normal cmd prompt/terminal where I can type in commands at free will.
Can this be done (in either dos or bash)? Is there like an execute command that I can put in an internal while loop or is there a command where when the scirpt ends it can go back to the normal cmd/terminal?
Do you need a full bash prompt?
Or would something like this be enough?
#!/bin/bash
echo -n "Enter cmd: "
read COMMAND
echo ${COMMAND} | bash
Also, in a script, you can just execute bash and get a full prompt in the current environment.
In Dos / windows command prompt if you run the batch file from command line you will get the prompt back always by default. Just like running any other command in command prompt.
Also in windows when the batch file execution is complete you can just put Cmd.exe when everything has finished running I.e at the end of the batch file.
Hope this helps!
E.g
#echo off
Echo running
.
.
.
Cmd.exe
Or even at the end
Echo %command% | Cmd.exe
Never mind, I got a good solution using this code: (for windows)
set /p command=CMD:
%command%

While Using PLink Only First Command Is Executing

I want to connect putty using plink and want to run some specific commands which is present in text file. But I can see only first command getting executed. Do I need to give any command separating literal?
I have this problem too. Solutions that I used.
Sending one line command with && or &
If script is big, send it with pscp and run on remote host with single command/
I beleive there is the third way. Just execute script under -m option :)

batch script print the command that would be executed rather than executing

Is it possible to set a cmd.exe shell / batch file to print what would be executed but not actually execute it?
For example, given a batch file that takes some arguments, based on those arguments selects some other batch files to run, those batch files execute some commands, may or may not call other files/commands etc.
I would like to be able to run the top level batch file with all possible combinations of it's input arguments and capture what each arg combination would execute - without actually trying to execute it.
e.g. conceptually would want to be able to produce something like:
mybatchfile.bat 1 2 3 > mybatchfile_1_2_3.bat
mybatchfile.bat 99 3 42 > mybatchfile_99_3_42.bat
where mybatchfile_99_3_42.bat is the list of everything that WOULD be executed when running mybatchfile.bat 99 3 42 (NOT the output of executing those commands)
If this can't be done solely using cmd.exe is there someway to achieve this by running the batch script in cygwin bash shell
In bash we would use something like -x to print out all possible commands without executing them. how to make bash scripts print out every command before executing The problem is that to my knowledge there's no exact equivalent command for Batch Scripts. I would suggest you try placing:
#echo on
at the beginning of your script and:
#echo off
at the end of your script, that's the best starting place.
If you never want the batch file to actually execute the commands, you can insert echo before each command. It's not a perfect solution by any means, but it may be a work-around for fairly simple scripts.

Resources