Executing Multi-Line Statements in the One-Line Command-Line

Executing multi-line statements in the one-line command-line

You could do

echo -e "import sys\nfor r in range(10): print 'rob'" | python

Or without pipes:

python -c "exec(\"import sys\nfor r in range(10): print 'rob'\")"

Or

(echo "import sys" ; echo "for r in range(10): print 'rob'") | python

Or SilentGhost's answer or Crast's answer.

How can I write multi-line code in the Terminal use python?

Just copy the code and past it in the terminal, and press return. This code works perfect if you do that:

   i = 0 
..
.. while i < 10:
.. i += 1
.. print(i)
..

1
2
3
4
5
6
7
8
9
10

How do I run two commands in one line in Windows CMD?

Like this on all Microsoft OSes since 2000, and still good today:

dir & echo foo

If you want the second command to execute only if the first exited successfully:

dir && echo foo

The single ampersand (&) syntax to execute multiple commands on one line goes back to Windows XP, Windows 2000, and some earlier NT versions. (4.0 at least, according to one commenter here.)

There are quite a few other points about this that you'll find scrolling down this page.

Historical data follows, for those who may find it educational.

Prior to that, the && syntax was only a feature of the shell replacement 4DOS before that feature was added to the Microsoft command interpreter.

In Windows 95, 98 and ME, you'd use the pipe character instead:

dir | echo foo

In MS-DOS 5.0 and later, through some earlier Windows and NT versions of the command interpreter, the (undocumented) command separator was character 20 (Ctrl+T) which I'll represent with ^T here.

dir ^T echo foo

How can I put multiple statements in one line?

Unfortunately, what you want is not possible with Python (which makes Python close to useless for command-line one-liner programs). Even explicit use of parentheses does not avoid the syntax exception. You can get away with a sequence of simple statements, separated by semicolon:

for i in range(10): print "foo"; print "bar"

But as soon as you add a construct that introduces an indented block (like if), you need the line break. Also,

for i in range(10): print "i equals 9" if i==9 else None

is legal and might approximate what you want.

If you are still determined to use one-liners, see the answer by elecprog.

As for the try ... except thing: It would be totally useless without the except. try says "I want to run this code, but it might throw an exception". If you don't care about the exception, leave out the try. But as soon as you put it in, you're saying "I want to handle a potential exception". The pass then says you wish to not handle it specifically. But that means your code will continue running, which it wouldn't otherwise.

Running multiple commands in one line in shell

You are using | (pipe) to direct the output of a command into another command. What you are looking for is && operator to execute the next command only if the previous one succeeded:

cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple

Or

cp /templates/apple /templates/used && mv /templates/apple /templates/inuse

To summarize (non-exhaustively) bash's command operators/separators:

  • | pipes (pipelines) the standard output (stdout) of one command into the standard input of another one. Note that stderr still goes into its default destination, whatever that happen to be.
  • |&pipes both stdout and stderr of one command into the standard input of another one. Very useful, available in bash version 4 and above.
  • && executes the right-hand command of && only if the previous one succeeded.
  • || executes the right-hand command of || only it the previous one failed.
  • ; executes the right-hand command of ; always regardless whether the previous command succeeded or failed. Unless set -e was previously invoked, which causes bash to fail on an error.

Execute multi-line python code inside Windows Batch script

Try like this:

0<0# : ^
'''
@echo off
echo batch code
python %~f0 %*
exit /b 0
'''

import random
import sys

def isOdd():
print("python code")
num = input("Enter Number: ")
num = int(num)
if num%2 == 0:
print("Even")
else:
print("Odd")

def printScriptName():
print(sys.modules['__main__'].__file__)

eval(sys.argv[1] + '()')
exit()

Example usage:

call pythonEmbeddedInBatch.bat isOdd
call pythonEmbeddedInBatch.bat printScriptName

The first line will be boolean expression in python ,but a redirection to an empty label in batch. The rest of the batch code will be within multiline python comment.

How to write a multiline command?

Solution 1 In CMD, Use ^ , example

docker run -dp 3000:3000 ^
-w /app -v "$(pwd):/app" ^
--network todo-app ^
-e MYSQL_HOST=mysql ^
-e MYSQL_USER=root ^
-e MYSQL_PASSWORD=secret ^
-e MYSQL_DB=todos ^
node:12-alpine ^
cmd "npm install && yarn run start"

Solution 2 In PowerShell, Use ` (backtick) , Example

docker run -dp 3000:3000 `
-w /app -v "$(pwd):/app" `
--network todo-app `
-e MYSQL_HOST=mysql `
-e MYSQL_USER=root `
-e MYSQL_PASSWORD=secret `
-e MYSQL_DB=todos `
node:12-alpine ^
cmd "npm install && npm run start"

Entering multiple lines / statements into the IDLE shell?

A Python program is a sequence of statements. In IDLE's Shell, one enters and executes a single statement at a time. This is much more useful than entering a single physical line at a time, as with python.exe running in a Windows console or *nix Terminal. The text you quoted was talking about the latter, not IDLE.

To understand 'statement', we must start with 'line'. A physical line is "a sequence of characters terminated by an end-of-line sequence." A logical line can be two or more physical lines joined either explicitly using a \ character or implicitly using (), [], or {} pairs.

A simple statement comprises one logical line. A compound statement usually comprises multiple logical lines, each of which may be more than one physical line. Your if statement is an example of a compound statement.

In IDLE, one enters a complete statement on one or more physical lines. When a simple statement is complete (when one has entered a complete logical line), IDLE runs it. Since a compound statement can have an indefinite number of logical lines, one enters a blank line to indicate the end.



Related Topics



Leave a reply



Submit