Assign Linux Command to Variable in Groovy - linux

I'm attempting to run a linux command, curl, through groovy, and would like the output to be assign to a variable. I'd like to do this, so I can extract specific data from the curl output, and use in in m y groovy script.
Any ideas, how I can do this.
I've tried,
def after = "curl \"https://test.com\"".execute()
def after = "curl \"https://test.com\"".execute().text
but the variable is always empty. Any help would be appreciated.

linux command with spaces needs to be separated.
["/bin/sh", "-o", "your_command"].execute()
In your case it would be something like below:
["curl", "https://test.com"].execute()
OR
def after = ["curl", "https://test.com"].execute().text
Try and see if it works.

Related

Accept arbitrary arguments and options with Click

I'm writing a Python wrapper around another program. I want the user to be able to specify a few options for the wrapper and then pass the rest of the command-line through to the wrapped program. Something like this:
#click.command()
#click.option("--port", type=int)
#click.argument("args", nargs=-1)
def main(port, args):
call_the_wrapped_program(port=port, args=args)
But this dies with Error: no such option: -k because it treats any command-line switch as something it should parse rather than an argument that can be added to args.
Is this possible?
Assuming you invoke the program with something akin to a cli command, have you tried simply calling it like this?
cli --port 8080 -- -k arg1 arg2 -r etc
If I print args with that invocation, I get all the arguments out, albeit as a string, but I'm hoping that whatever third party you want to delegate to might be able to run its own parsing over that.
The Forwarding Unknown Options section in the documentation covers this use case. You should be able to write something like this:
#click.command(context_settings=dict(
ignore_unknown_options=True,
))
#click.option("--port", type=int)
#click.argument("args", nargs=-1, type=click.UNPROCESSED)
def main(port, args):
call_the_wrapped_program(port=port, args=args)

Can I pass a variable from python to bash file?

I have a bash file with a bunch of sed commands like this :
sed -i 's/hello my name is Thibault/hello my name is Louis/g' "$1"
so for now i'm doing all of this "by hand", however, I have a python script with a tkinter GUI and several input fields for the user. I would like to find a trick so that if the user inputs "hello my name is Olivia" in the text field then the regex would look like this:
sed -i 's/hello my name is Thibault/hello my name is Olivia/g' "$1"
So I was thinking that i could store the python text input result in the variable to have the regex look like this:
sed -i 's/hello my name is Thibault/$my_variable/g' "$1"
but i don't know how or if this is even possible. Lastly I want to mention that i know i could just ask for the user input in the bash script but this is for my first internship and I have to go through the python GUI.
Edit: i'm on windows 10 if this is any important
Try it like this :
import os
original_text = 'hello my name is Thibault'
new_text = 'hello my name is Louis'
filename = 'test.txt'
os.system (f'sed -i "s/{original_text}/{new_text}/g" {filename}')
For passing data (in your case: some string) from your Python program to a subprocess running a bash script, you have first of all the same options like when calling one bash script from another one: Either design the called script to expect positional parameters (use it as $1 for example) and pass the string as parameter. For instance, if the string is stored in the Python variable parameter, it would look like:
import subprocess
subprocess.call ['bash', './script_to_be_called', parameter]
The other possibility is to design the bash script so that it expects the string to be stored in a variable of a certain name (use it as $PARSTRING for instance) and pass the data via the environment:
import os
os.environ['PARSTRING']=parameter
subprocess.call['bash', './script_to_be_called']
If the "script" executes only a single command, you could alternatively synthesize the command line in your Python program. Assume that you have a string bashcommand, which already holds the complete command which is supposed to be executed by bash, you could do a
import subprocess
subprocess.call ['bash', '-c', bashcommand]
While this should answer your question, I can't help but pointing out, that for executing a single external command, I would not create a shell process, but invoke this program directly as a child process. Also don't forget that spawning a child process takes time, and if you have many such invocations, it might make sense to redesign your approach, for instance by doing everything inside Python, or having only one child prcocess which gets as input the data for all the substitutions to be performed (typically via a file).

How can I have subprocess or Popen show the command line that it has just run?

Currently, I have a command that looks something like the following:
my_command = Popen([activate_this_python_virtualenv_file, \
"-m", "my_command", "-l", \
directory_where_ini_file_for_my_command_is + "/" + my_ini_file_name], \
stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=False,
universal_newlines=False, cwd=directory_where_my_module_is)
I have figured out how to access and process the output, deal with subprocess.PIPE, and make subprocess do a few other neat tricks.
However, it seems odd to me that the standard Python documentation for subprocess doesn't mention a way to just get the actual command line as subprocess.Popen puts it together from arguments to the Popen constructor.
For example, perhaps my_command.get_args() or something like that?
Is it just that getting the command line run in Popen should be easy enough?
I can just put the arguments together on my own, without accessing the command subprocess runs with Popen, but if there's a better way, I'd like to know it.
It was added in Python 3.3. According to docs:
The following attributes are also available:
Popen.args The args argument as it was passed to Popen – a sequence of
program arguments or else a single string.
New in version 3.3.
So sample code would be:
my_args_list = [] # yourlist
p = subprocess.Popen(my_args_list)
assert p.args == my_args_list

Linux-wget command

I need a quick help on customizing my wget command in a shell script:
The wget command looks something like this:
wget http://infamvn:8081/nexus/content/groups/LDM_REPO_LIN64/com/infa/com.infa. products.ldm.ingestion.server.scala/10.0.0.135.527-SNAPSHOT/com.infa.products.ldm.ingestion.server.scala-10.0.0.135.527-20150622.210643-1-sources.jar
Here I'd like to add the 10.0.0.135.527 in a variable, so I created a script something like this:
n = 10.0.0.135.527
wget http://infamvn:8081/nexus/content/groups/LDM_REPO_LIN64/com/infa/com.infa.products.ldm.ingestion.server.scala/"$n"-SNAPSHOT/com.infa.products.ldm.ingestion.server.scala-"$n"-20150622.210643-1-sources.jar
but this is not working, any idea what's wrong here?
Try this to create a string variable n, with no leading whitespace (thanks #011c):
n="10.0.0.135.527"
wget http://infamvn:8081/nexus/content/groups/LDM_REPO_LIN64/com/infa/com.infa.products.ldm.ingestion.server.scala/"$n"-SNAPSHOT/com.infa.products.ldm.ingestion.server.scala-"$n"-20150622.210643-1-sources.jar

Start EXE-file with parameters via Groovy (problems with return value)

I am trying to start an external executable file via Groovy but got some problems with it! I just want to start the rs.exe with several parameters to create a PDF-file using the SSRS.
But as soon as I try to get the return value/exit-code it doesn't work anymore! But I want to grab the generated file and add it to a database, so I need a return value to know when its generated. This works totally fine for generating:
def id = 1
def cmd = """ C://Program Files (x86)//...//rs.exe
-i C:\\export.rss
-s http://localhost/ReportServer_SQLEXPRESS
-v ID=${id}
-e Exec2005 """
def proc = cmd.execute()
But I don't get any return value/exit-code. I already tried different way, e.g.
proc.waitFor()
but I or
cmd.execute().value
but nothing worked. When I start the rs.exe with all my provided data in Windows I get the return "Process succesfully ended". Any Groovy-specialists here that can help me out?
Try executing command when it's defined in the following way:
def cmd = ['cmd', '/c', 'C://Program Files (x86)//...//rs.exe', '-i', 'C:\\export.rss', '-s', 'http://localhost/ReportServer_SQLEXPRESS', '-v', "ID=${id}", '-e', 'Exec2005']
def proc = cmd.execute()
I was able to run it now and get the exit-code. But without those escape forward-slashes I wasn't able to let it run. To get the exit-codes I just used "proc.text" and it works perfectly now.
It was working before but I didn't know how to get the exit-codes, ".text" solved it completely. Thanks for your help!

Resources