Skip to main content

Executes Cygwin bash scripts/commands in Python, captures and prints the output

Project description

Executes Cygwin bash scripts/commands in Python, captures and prints the output

pip install cygsubprocess

Tested against Windows 10 / Python 3.10 / Anaconda / cygcheck (cygwin) 3.4.6

from cygsubprocess import Bashsubprocess



ba = Bashsubprocess(

    cygfolder=r'C:\cygwin',mirrorsite=None,addtopath=True

) # If cygwin is not installed, it will be installed and added to the PATH

ba.convert_path_cyg2win("/cygdrive/c/Users/hansc/Downloads/ClipAngel 2.09")

# r'C:\Users\hansc\Downloads\ClipAngel 2.09'



ba.convert_path_win2cyg(path=r"C:\Users\hansc\Downloads\ClipAngel 2.09")

#  '/cygdrive/c/Users/hansc/Downloads/ClipAngel 2.09'

cmd1 = ba.execute_capture("ls -la")

print(cmd1.stdout_lines[:5])



cmd2 = ba.execute_capture(["ls -la | grep 'py'"])

print(cmd2.stdout_lines[:5])





cmd1 = ba.execute_print_capture("ls -la")

print(cmd1.stdout_lines[:5])



cmd2 = ba.execute_print_capture(["ls -la | grep 'py'"])

print(cmd2.stdout_lines[:5])



# gist download

scriptexec = r"""

slugify(){ echo "$1" | iconv -t ascii//TRANSLIT | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g | tr A-Z a-z; }

cnt=0; gh gist list --limit 3 | cut -f1,2 | tac | while read id name; do ((cnt++)); gh gist clone $id $cnt-`slugify "$name"`; done

"""

cmd4 = ba.execute_print_capture(scriptexec)

print(cmd4.stdout_lines)





bashscr = """

#!/bin/bash

# set the STRING variable

STRING="Hello World!"

# print the contents of the variable on screen

echo $STRING

"""

bashtofile = ba.exec_sh_to_file(bashscr, printoutput=False)

print(bashtofile.stdout_lines)



bashscr = r"""

#!/bin/bash

salary=1000

expenses=800

#Check if salary and expenses are equal

if [ $salary == $expenses ];

then

    echo "Salary and expenses are equal"

#Check if salary and expenses are not equal

elif [ $salary != $expenses ];

then

    echo "Salary and expenses are not equal"

fi

"""

bashtofile2 = ba.exec_sh_directly(bashscr, printoutput=True)

print(bashtofile2.stdout_lines)



scriptfile='c:\\bashscripttest.sh'

with open(scriptfile,mode='w', encoding='utf-8',newline='\n') as f:

    f.write(bashscr)



ba.installapt() # install apt  https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg

ba.apt_install( package='hunspell-de') # install a package with apt

ba.apt_remove( package='hunspell-de') # remove a package with apt



test2=ba.exec_sh_url( bashscr=r'https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg', printoutput=True)

test3=ba.exec_sh_file( bashscr=scriptfile, printoutput=True)

test4=ba.exec_sh_to_file( bashscr, printoutput=True)

test5=ba.exec_sh_directly( bashscr, printoutput=True)

test6=ba.get_list_of_files_no_stat( folder=r'C:\Python34') # windows and cyg path (escaped and unescaped)

test7=ba.get_list_of_files_with_stat( folder=r'C:\Python34') # windows and cyg path (escaped and unescaped)



....



# b'-rwxr-x---+ 1 Administrators hansc    25749 Mar 31 23:52 trayinfo.py.bak\n'

# b'-rwxr-x---+ 1 Administrators hansc      671 Apr  1 09:50 trayinfotest.py\n'

# b'-rwxr-x---+ 1 Administrators hansc    25878 Apr  1 09:34 traymenuxxxxxxxxxx.py\n'

# b'-rwxr-x---+ 1 Administrators hansc     4329 Apr  1 03:12 tw.py\n'

# b'-rwxr-x---+ 1 Administrators hansc     8095 Apr  1 08:48 win10ctypestoastxxxxxxxxxx.py\n'

# [b'drwxr-x---+ 1 hansc          hansc        0 Apr  1 13:01 1-pyde-py\n', b'drwxr-x---+ 1 hansc          hansc        0 Apr  1 13:14 1-stra-py\n', b'drwxr-x---+ 1 hansc          hansc        0 Apr  1 13:14 10-cygwinco-py\n', b'drwxr-x---+ 1 hansc          hansc        0 Apr  1 13:01 10-tracealllines-py\n', b'drwxr-x---+ 1 hansc          hansc        0 Apr  1 13:01 2-stra-py\n']

# b"Cloning into '1-tracealllines-py'...\n"

# b"Cloning into '2-cygwinco-py'...\n"

# b"Cloning into '3-cygwinco-py'...\n"

# []

# [b'Hello World!\n']

# b'Salary and expenses are not equal\n'

# [b'Salary and expenses are not equal\n']

....

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cygsubprocess-0.12.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

cygsubprocess-0.12-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file cygsubprocess-0.12.tar.gz.

File metadata

  • Download URL: cygsubprocess-0.12.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for cygsubprocess-0.12.tar.gz
Algorithm Hash digest
SHA256 f6d843f777c0e9ef31cc55588e0958898e174eb39c09fed7fea36dbf0e60b0b9
MD5 e988e5bf4b883a2951d4c79991e16fd9
BLAKE2b-256 8d9bda8f4052bcecccd4401fba567404d013395d9a2996d8410e36cee7fa2f63

See more details on using hashes here.

File details

Details for the file cygsubprocess-0.12-py3-none-any.whl.

File metadata

  • Download URL: cygsubprocess-0.12-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for cygsubprocess-0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 6f964a91c8a449b8c0849ee40de37505fdef76190430eec484646bf916f8ae47
MD5 cfc6c98657e38c8e7a6fb1bc9d763ed0
BLAKE2b-256 7a305083a52f5b9cad1a0cdba8c704ecc12131ef1f2a55c334025e061a1add46

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page