Sinisterly

Full Version: how may i do to using bash scripting inside a python project?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi
i want to learn about how may i do an application in python that read, write and even do some functions by calling bash commands in linux

imagine this:

an app that allows you to put a value, then run a command with that value as a "-" variable, so you can do something like

Code:
#!bin/bash
freq=10 #max
freq2=2

freq3=3
freq4=4
freq5=5
freq6=6 #rec
freq7=7 #warning
freq8=8 #warning
freq9=9 #warning
freqmax=10 #warning
freqmin=1

cpu0=/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
cpu1=/sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq
cpu2=/sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq
cpu3=/sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq

echo $freqmax > $cpu0
echo $cpu1 $cpu2 $cpu3 | xargs -n 1 cp $cpu0

but... by choosing or writing the freq value using a gui or even options in a command-line python script.

something like "processor.py -freq 1"
With read write I supposed you mean reading/writing files? There's a method in python called open.
Code:
file = open( "testfile", "w" ) # Open the file in writing mode
file.write( "first line\n" ) # Write to it
file.write( "second line\n" ) # Write another line
file.close() # Close the file handle

file = open( "testfile", "r" ) # Open the file in reading mode
for i, line in enumerate( file ): # Loop through all the lines in the file
    print( "Line: %s  Value: %s" % ( i, line ) )


To run a command in Python you could use the subprocess module
Code:
import subprocess

subprocess.call( [ "ls", "-la"] )
or the os module
Code:
import os

os.system( "ls -la" )

About being able to pass them as arguments to the python script itself you've got a lot of options. The simplest is to use the  sys module and optionally parse the arguments yourself depending on how advanced you want it. Here's an example of a script that prints the first argument that you pass to the script, basically an echo script, but we set it as a positional argument and force it to be passed.
Code:
import sys

if __name__ == "__main__":
    if len( sys.argv ) < 2:
        print( "Usage: %s [text]" % ( sys.argv[0] ) ) # sys.argv[0] is the name of the script
        sys.exit( 1 ) # exit with status code 1

    print( sys.argv[1] ) # Print the first argument passed to the script
But as I mentioned above you'd need to parse it yourself if you want to have options starting with "-". In this case it'd be simpler to use the argparse module where you can choose what options you want and what values they need to be( int, string ) or if they're just boolean options. A basic google search would've gotten you all the answers - suggestion would be to do a trial and error and just test your way forward until you get the basic understanding of how things work, get familiar with the modules( os, sys ).

If you want to have a gui you'd need to use a library like tkinter, Qt, Gtk+, Kivy etc.
(11-21-2019, 01:07 AM)obnoxious Wrote: [ -> ]With read write I supposed you mean reading/writing files? There's a method in python called open.
Code:
file = open( "testfile", "w" ) # Open the file in writing mode
file.write( "first line\n" ) # Write to it
file.write( "second line\n" ) # Write another line
file.close() # Close the file handle

file = open( "testfile", "r" ) # Open the file in reading mode
for i, line in enumerate( file ): # Loop through all the lines in the file
    print( "Line: %s  Value: %s" % ( i, line ) )


To run a command in Python you could use the subprocess module
Code:
import subprocess

subprocess.call( [ "ls", "-la"] )
or the os module
Code:
import os

os.system( "ls -la" )

About being able to pass them as arguments to the python script itself you've got a lot of options. The simplest is to use the  sys module and optionally parse the arguments yourself depending on how advanced you want it. Here's an example of a script that prints the first argument that you pass to the script, basically an echo script, but we set it as a positional argument and force it to be passed.
Code:
import sys

if __name__ == "__main__":
    if len( sys.argv ) < 2:
        print( "Usage: %s [text]" % ( sys.argv[0] ) ) # sys.argv[0] is the name of the script
        sys.exit( 1 ) # exit with status code 1

    print( sys.argv[1] ) # Print the first argument passed to the script
But as I mentioned above you'd need to parse it yourself if you want to have options starting with "-". In this case it'd be simpler to use the argparse module where you can choose what options you want and what values they need to be( int, string ) or if they're just boolean options. A basic google search would've gotten you all the answers - suggestion would be to do a trial and error and just test your way forward until you get the basic understanding of how things work, get familiar with the modules( os, sys ).

If you want to have a gui you'd need to use a library like tkinter, Qt, Gtk+, Kivy etc.

thank you so much for your help! Heart