Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass variable to subprocess
#1
Hi all,

I want to pass a variable to this command :
VARIABLE=12
v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True);
How can I do that?
I tried:
VARIABLE=12
v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v %s",VARIABLE, shell=True);
But didnt work.
Reply
#2
String formatting and now use f-string new in Python 3.6 ➡
>>> VARIABLE = 12
>>> s = f"cat /tmp/logall | sort -u | grep -v {VARIABLE}"
>>> s
'cat /tmp/logall | sort -u | grep -v 12'
Reply
#3
(Apr-11-2022, 11:53 PM)snippsat Wrote: String formatting and now use f-string new in Python 3.6 ➡
>>> VARIABLE = 12
>>> s = f"cat /tmp/logall | sort -u | grep -v {VARIABLE}"
>>> s
'cat /tmp/logall | sort -u | grep -v 12'

Is there a way to in python 2.7? I cannot use 3 yet.
Reply
#4
(Apr-12-2022, 11:32 AM)paulo79 Wrote:
(Apr-11-2022, 11:53 PM)snippsat Wrote: String formatting and now use f-string new in Python 3.6 ➡
>>> VARIABLE = 12
>>> s = f"cat /tmp/logall | sort -u | grep -v {VARIABLE}"
>>> s
'cat /tmp/logall | sort -u | grep -v 12'

Is there a way to in python 2.7? I cannot use 3 yet.

I found it:
>>> VARIABLE=12
>>> v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v {}".format(VARIABLE), shell=True);
>>>
>>> print(v_num_final_distinct_grid_disks_per_cell_and_dg)
10

>>> VARIABLE=10
>>> v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v {}".format(VARIABLE), shell=True);
>>> print(v_num_final_distinct_grid_disks_per_cell_and_dg)
12

>>>
Reply
#5
4 alternatives:

import subprocess

# VARIABLE=12
# v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True)
# grep -v is invert match


def insecure(variable: str) -> str:
    return subprocess.check_output(f"cat /tmp/logall | sort -u | grep -v {variable}", shell=True, encoding="utf8")

    
def silly(variable: str) -> str:
    """
    Using PIPE to connect stdout with stdin of next process
    """
    cat_proc = subprocess.Popen(["cat", "/tmp/logall"], encoding="utf8", stdout=subprocess.PIPE)
    sort_proc = subprocess.Popen(["sort", "-u"], encoding="utf8", stdin=cat_proc.stdout, stdout=subprocess.PIPE)
    grep_proc = subprocess.Popen(["grep", "-v", variable], encoding="utf8", stdin=sort_proc.stdout, stdout=subprocess.PIPE)
    grep_proc.wait()
    return grep_proc.stdout.read()


def better(variable: str) -> str:
    lines = []
    with open("/tmp/logall") as fd:
        # fd is the the file-object
        # iterating over fd -> lines
        # creating a set from fd -> unique lines
        # sort set -> sorted unique lines
        for line in sorted(set(fd)):
            if variable not in line:
                lines.append(line.rstrip())
    return "\n".join(lines)


def better2(variable: str) -> list[str]:
    with open("/tmp/logall") as fd:
        return [
            line.rstrip()
            for line in sorted(set(fd))
            if variable not in line
        ]
better2 uses a list-comprehension to return all not matching lines as a list. The other functions do return the whole text as a single str.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 853 Jul-27-2023, 12:40 AM
Last Post: tester_V
  how to pass the interactive string to Popen subprocess maiya 1 1,876 Sep-18-2020, 09:36 PM
Last Post: Larz60+
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,551 Sep-07-2020, 08:02 AM
Last Post: perfringo
  How to pass multiple values from one sample to nc variable? Baloch 0 1,862 Jun-01-2020, 09:27 PM
Last Post: Baloch
  How to pass variable as an argument to be used in a function? t4keheart 8 3,583 Jan-16-2020, 01:37 PM
Last Post: buran
  Pass by reference vs Pass by value leodavinci1990 1 2,198 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  How to pass a variable to subprocess.check_call champweller 1 2,807 May-26-2019, 07:10 AM
Last Post: heiner55
  Pass variable script return twice output problem Faruk 8 4,384 Dec-26-2018, 11:57 AM
Last Post: Faruk
  how to pass a variable from a file to a 2d file sylas 1 2,206 May-05-2018, 02:46 AM
Last Post: micseydel
  [subprocess]>Run a cmd command and get output into a variable CSA75 4 22,799 Mar-13-2017, 09:33 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020