4

A little trick to embed python code in a BASH script.

 1 year ago
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.

A little trick to embed python code in a BASH script.

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:

image

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)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK