

A little trick to embed python code in a BASH script.
source link: https://gist.github.com/welbornprod/ccbf43393ecd610032f4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A little trick to embed python code in a BASH script. · GitHub
Instantly share code, notes, and snippets.
Here is a example to make embedded python code working which needs a tty / pty, because it interacts with the user:
I wanted to have a sexy ui in my shell scripts, and found the python library bullet and wrapped it into my bash scripts like above described, but bullet needs a tty / pty an with here documents there is no tty / pty available. I found the following StackOverflow post and combined everything together.
#!/bin/bash
# Name: bselect()
# Arg: selection-items
# Example:
# bselect "root $USER $username"
#
# Example:
# Call it and capture the output
#
# SELECTION=$(bselect "root $USER $username")
# echo "Selected User": $SELECTION
#
bselect(){
read -r -d '' script <<-"----EOF"
import os
from bullet import Bullet
selections = os.environ['PYTHON_ARG'].split()
print(selections)
cli = Bullet(
prompt = "\nPlease choose: ",
choices = selections,
return_index = True
)
result = cli.launch()
print(result)
----EOF
PYTHON_ARG="$1" python -c "$script"
}
Example bash call:
bselect "root $USER $username"
Preview:
Okay updated Code, had some issues with getting it working with return value capturing, and do a workaround - which isn't a nice way when you have two processes using bselect
at the same time... but I can facilitate with that for the moment.
# Name: bselect()
# Arg: selection-items
# Example:
# bselect "root $USER $username"
#
# Example: (wished I get it working to be like this example...)
# Call it and capture the output
#
# SELECTION=$(bselect "root $USER $username")
# echo "Selected User": $SELECTION
#
#
# Example: (working example, workaround with writing the selection result to a file, and in bash script to cat the file)
# Call it and capture the output
#
# bselect "root $USER $username"
# username=$(cat ~/.bselect)
# echo "Selected User": $username
#
bselect(){
read -r -d '' script <<-"----EOF"
import os
import json
from bullet import Bullet
selections = os.environ['PYTHON_ARG'].split()
print(selections)
cli = Bullet(
prompt = "\nPlease choose: ",
choices = selections,
return_index = False
)
result = cli.launch()
# print("export PYTHON_RETURN=%s" % result)
# print(result)
f = open(os.path.expanduser('~/.bselect'), 'w')
f.write(result)
f.close()
----EOF
PYTHON_ARG="$1" python -c "$script"
}
Example bash call:
bselect "root $USER $username"
username=$(cat ~/.bselect)
Recommend
-
14
Bash script to perform super-fast file transfer using netcat and tar Aug 5, 2013 Updated 06-Aug-2013 : Script updated to transfer multiple files in single command [
-
14
Bash scripts. Almost anyone needs to write one sooner or later. Almost no one says “yeah, I love writing them”. And that’s why almost everyone is putting low attention while writing them. I won’t try to make you a Bash expert (since...
-
14
Create Script from Bash History Submitted by NanoDano on Wed, 11/21/2012 - 11:04...
-
20
Send mail with a BASH Shell Script Posted: 2007-06-10 - Last updated: 2019-06-05 Tagged
-
11
Bash Script 的好習慣這篇給了一份 bash script 用的 tempalte,但更重要的反而是裡面提到的 best practice:「Minimal safe Bash script template」。首先是不...
-
5
-
15
Bash File Descriptor Trick Here’s a little bash trick you might not be aware of. If you wrap a command in <( and ), you get back something that looks like a file name; $ echo <(ls)...
-
7
A neat little trick with time decay 2012-10-29Something that pops up pretty frequently is to implement time decay, especially where you have recursive chains of jobs. For instance, say you want to keep track of a popularity score....
-
13
Access denied Access denied Error code 1020 You do not have access to www.codecademy.com.The site ow...
-
5
english-script Embed natural language in your code. Allow me to completely take out of context this great Djikstra essay
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK