Questions tagged [shlex]

Use this tag for questions about the python shlex module.

From python.docs:

The shlex class makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell. This will often be useful for writing minilanguages, (for example, in run control files for Python applications) or for parsing quoted strings.

94 questions
1
vote
1 answer

How to get wlan device name with python?

I want only the wlan device name at a linux system with python. I could get the device name with shell scripting: echo /sys/class/net/*/wireless | awk -F'/' '{ print $5 }' So i want to use this at python with subprocess. import shlex import…
ikreb
  • 2,133
  • 1
  • 16
  • 35
1
vote
1 answer

How to escape arguments for the command line in python?

I am writing a script that needs to call an external command using the subprocess module. The problem is I need to pass a file provided by the user to the command so it looks similar to this: p = subprocess.run("command arg1 arg2…
Shai Avr
  • 942
  • 8
  • 19
1
vote
0 answers

Equivalent of shlex.split in bash without python

Is there a bash builtin or expression that is equivalent to python's shlex.split? It would be great if it could handle not only whitespace but any IFS. I have a file with a bunch of simple bash commands. cat a_filename ls a\ filename\ with\…
pgy
  • 291
  • 1
  • 4
1
vote
1 answer

Python custom parser not detecting arguments

I created a parser to extract variables from strings and populate them with values, but am having a lot of difficulties detecting multiple values within a string. Let me illustrate: The following message contains variables 'mass', 'vel', boolean…
Code Monkey
  • 800
  • 1
  • 9
  • 27
1
vote
2 answers

How to parse bash array using Python shlex?

Input: declare -a ForwardPort=([0]="L *:9102:10.0.1.8:9100 # remote laptop" [1]="L *:9166:8.8.8.8:9100 # google") Desired output And I would like to get this output: { 'ForwardPort': [ '"L *:9102:10.0.1.8:9100 # remote laptop"', …
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
1
vote
2 answers

Terminate a subprocess if string is in output

I'm trying to terminate a subprocess pid if a string is in the output, but it is not working. What is wrong? import subprocess import shlex if "PING" in subprocess.check_call(shlex.split("ping -c 10 gogole.com")): …
Jonson
  • 33
  • 5
1
vote
1 answer

How to Pass a list of variables which are strings to stdout via subprocess.communicate

I can successfully hard code my values as follows below: (my app launches and the parameters are passed and it runs as if I hit enter after manually entering in each parameter as I would if I ran it via command line, inputting the parameters…
Sherri
  • 21
  • 2
1
vote
0 answers

shlex splitting on curly/smart quotes

Trying to split a string by whitespace, while preserving contents enclosed in curly/smart quotes (“”), by using shlex module in Python 3.6.3. However, it does not work properly: >>> import shlex >>> text = 'one “two and three” four' >>>…
toringe
  • 1,194
  • 3
  • 12
  • 18
1
vote
1 answer

Error using shlex and subprocess

Hi I am trying to run this command in python's subprocess with shlex split, however, I haven't found anything helpful for this particular case : ifconfig | grep "inet " | grep -v 127.0.0.1 | grep -v 192.* | awk '{print $2}' I get an with ifconfig…
Aboogie
  • 450
  • 2
  • 9
  • 27
1
vote
2 answers

shlex include empty strings

sample = ",," values = shlex.shlex(sample, posix=True) values.quotes = '"' values.whitespace = ',' values.whitespace_split = True received_output = list(values) In the above code sample I would like to have ["", "", ""] as the value of…
gkumar7
  • 325
  • 1
  • 2
  • 10
1
vote
1 answer

Password Management for non-interactive process

The challenge I need a password management tool that will be invoked by other processes (scripts of all sort: python, php, perl, etc) and it will be able to identify and verify the caller script in order to perform access control: either return a…
urban
  • 5,392
  • 3
  • 19
  • 45
1
vote
1 answer

Python Popen shell=False causes OSError: [Errno 2] No such file or directory FFREPORT

I am adding a FFREPORT command on the front of an already working ffmpeg command in python 2.7 on OSX, this is to redirect the report log file, but am getting an error and can't figure out how to fix it. Here is the command: command =…
speedyrazor
  • 3,127
  • 7
  • 33
  • 51
1
vote
2 answers

PHP equivalent of Python's shlex.split

I'm attempting to parse a file containing space-separated key=>value pairs, of a format like so: host=db test="test test" blah=123 Normally, this file is ingested by Python and parsed using shlex.split, but I've been unable to find a PHP equivalent…
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
0
votes
1 answer

Python parse multiline shell commands?

Python provides the library shlex to parse shell commands. This appears not to parse multiline strings. The shell command python3 \ arg is equivalent to subproces.check_call(["python3", "arg"]) However, shlex.split appends to append a newline to…
Att Righ
  • 1,439
  • 1
  • 16
  • 29
0
votes
0 answers

how do you print out errors from curl command

I am trying to use shlex library to run a curl command. I need to print out the errors. I have done the following but I dont see any errors. import shlex url='test.com' args = shlex.split(url) process = subprocess.Popen(args, shell=False,…
user1471980
  • 10,127
  • 48
  • 136
  • 235