A general-purpose program that formats Linux command strings in python

background

For example, CPU temperature is added to the DB regularly using Linux commands. At this time, I searched variously with just the command to try to reduce the code as much as possible, but I created it because it was quite difficult.

specification

I want to make it usable for general purposes, so I want to realize a movement like Format of python.

code

Now this.

import sys;
print(sys.argv[1].format(*sys.argv[2].split(" ")) )

How to Use

mosquitto_sub -t "#" -v |xargs [email protected] python3 /home/hashito/command/text2format.py "insert into mqtt.msg (t,m,tm)values(\'{}\',\'{}\',UNIX_TIMESTAMP(NOW()))" @ | xargs [email protected] mysql -e "@"

I will put the contents of mqtt into Maria DB

Postscript

We received a comment from @shiracamus. It seems that you can do it in the following form with only commands.

$ echo "a b" | xargs echo | xargs printf "insert into mqtt.msg (t,m,tm)values('%s','%s',UNIX_TIMESTAMP(NOW()))"
insert into mqtt.msg (t,m,tm)values('a','b',UNIX_TIMESTAMP(NOW()))

I've been looking for this for a long time, but I was saved because I didn't have my own knowledge! Thank you very much!

Recommended Posts

A general-purpose program that formats Linux command strings in python
A program that removes duplicate statements in Python
A Python program in "A book that gently teaches difficult programming"
Hit a command in Python (Windows)
I tried "a program that removes duplicate statements in Python"
When writing a program in Python
A program that determines whether a number entered in Python is a prime number
[Python] A program that creates stairs with #
I made a payroll program in Python!
Write a Caesar cipher program in Python
A program that plays rock-paper-scissors using Python
[Python] A program that rounds the score
A memo that I wrote a quicksort in Python
A simple Pub / Sub program note in Python
A Vim plugin that automatically formats Python styles
I made a Caesar cryptographic program in Python.
[Beginner] What happens if I write a program that runs in php in Python?
Compare strings in Python
Reverse strings in Python
[Python] A program that counts the number of valleys
I made a prime number generation program in Python
Write a super simple molecular dynamics program in python
Specify a subcommand as a command line argument in Python
Receive dictionary data from a Python program in AppleScript
Try embedding Python in a C ++ program with pybind11
What's in that variable (when running a Python script)
In Python, create a decorator that dynamically accepts arguments Create a decorator
A shell program that becomes aho in multiples of 3
I made a prime number generation program in Python 2
How to execute a command using subprocess in Python
[Python] A program that compares the positions of kangaroos.
A Python program that converts ical data into text
I implemented a Vim-like replacement command in Slackbot #Python
MALSS, a tool that supports machine learning in Python
[Python] A program that finds the shortest number of steps in a game that crosses clouds
A note that runs an external program in Python and parses the resulting line
Create a function in Python
Create a dictionary in Python
A function that divides iterable into N pieces in Python
Published a library that hides character data in Python images
Loop through a generator that returns a date iterator in Python
Let's create a script that registers with Ideone.com in Python.
Execute external command in python
Search for strings in Python
A program that automatically resizes the iOS app icon to the required image size in Python
Summary of points to keep in mind when writing a program that runs on Python 2.5
[Python] A program that creates a two-dimensional array by combining integers
I made a program to collect images in tweets that I liked on twitter with Python
Make a bookmarklet in Python
Create code that outputs "A and pretending B" in python
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
[python] Manage functions in a dictionary (command table, function table, function pointer)
[Python] A program that finds the most common bird types
External command execution in Python
Draw a heart in Python
A Python program that aggregates time usage from icalendar data
A set of script files that do wordcloud in Python3
A Python program that collects tweets containing specific keywords daily and saves them in csv
[Python] I tried to make a simple program that works on the command line using argparse.
A memo that handles double-byte double quotes in Python regular expressions
I made a program that solves the spot the difference in seconds