Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store filtered output of cmd command in a variable

I am trying to store the output of a cmd command as a variable in python. To achieve this i am using os.system() but os.system() just runs the process,it doesn't capture the output.

import os


PlatformName = os.system("adb shell getprop | grep -e 'bt.name'")
DeviceName = os.system("adb shell getprop | grep -e '.product.brand'")
DeviceID = os.system("adb shell getprop | grep -e 'serialno'")
Version = os.system("adb shell getprop | grep -e 'version.release'")

print(PlatformName)
print(DeviceName)
print(DeviceID)
print(Version)

Then i tried to use the subprocess module.

import subprocess
import os


PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])
DeviceName = subprocess.check_output(["adb shell getprop | grep -e '.product.brand'"])
DeviceID = subprocess.check_output(["adb shell getprop | grep -e 'serialno'"])
Version = subprocess.check_output(["adb shell getprop | grep -e 'version.release'"])

print(PlatformName)
print(DeviceName)
print(DeviceID)
print(Version)

I am getting the following error

FileNotFoundError: [WinError 2] The system cannot find the file specified

How can I store the output of the command as a variable?

like image 715
AutoTester213 Avatar asked Sep 17 '18 08:09

AutoTester213


People also ask

How to store the output of the command in a variable?

The following examples show how you can store the output of the command with option and argument into a variable. Bash ` wc` command is used to count the total number of lines, words, and characters of any file. This command uses -c, -w and -l as option and filename as the argument to generate the output.

How to read output of a command into a batch file variable?

It’s Day Two of Batch File Week. Don’t worry, it’ll be over in a few days. There is no obvious way to read the output of a command into a batch file variable. In unix-style shells, this is done via backquoting. The Windows command processor does not have direct backquoting, but you can fake it by abusing the FOR command. Here’s the evolution:

How do I set the output into a variable?

You can set the output into a variable with the following command line: Used on the command line like this: Should you want to use the FOR within a batch file, rather than command line, you need to change %a to %%a. Show activity on this post. If result.txt has more than 1 line, only the top line of the file is used for %DATA%.

How to print the output of `pwd` command in bash script?

The following script stores the output of `pwd` command into the variable, $current_dir and the value of this variable is printed by using `echo` command. The option and argument are mandatory for some bash commands.


2 Answers

The issues here:

  • passing arguments like this (string in a list, with spaces) is really not recommended
  • passing arguments like this need shell=True for it to have a slight chance to work, and shell=True is known for security issues (and other issues as well, like non-portability)
  • grep is not standard on windows, and the pattern is a regex which means you'd probably have to escape . ("bt\.name").
  • when not found grep returns 1 and would make check_output fail.
  • when found grep returns match(es), and a newline, that you'd have to strip

I'd rewrite this:

PlatformName = subprocess.check_output(["adb shell getprop | grep -e 'bt.name'"])

as:

output = subprocess.check_output(["adb","shell","getprop"])
platform_name = next((line for line in output.decode().splitlines() if "bt.name" in line),"")

The second line is a "native" version of grep (without regexes). It returns the first occurrence of "bt.line" in the output lines or empty string if not found.

You don't need grep here (the above is not strictly equivalent, as it yields the first occurrence, not all the occurrences but that should be okay on your case). And your clients may not have grep installed on Windows.

like image 153
Jean-François Fabre Avatar answered Oct 09 '22 19:10

Jean-François Fabre


Hey I got the same problem as you. Sub-process can do what you want even with the shell=False. The trick is the communicate() method.

with subprocess.Popen(cmdCode,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            cwd = workingDir,
                            bufsize=1,
                            universal_newlines = True) as proc:
    #output is stored in proc.stdout
    #errors are stored in proc.stderr

Now you just need a little function to scan the proc.stdout for the information you need: PlatformName, etc

like image 37
Sharku Avatar answered Oct 09 '22 19:10

Sharku