LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-15-2018, 06:23 PM   #1
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Rep: Reputation: 73
use bash to sequentially call 6 python routines


Every week I have to insert scores for 7 classes, so I am trying to automate this as much as possible.

I now have 6 python routines which all work in my idle3 shell and in a bash terminal in Ubuntu 18.04 and do everything I need.

The routines are:

copyOldData.py
insert3Cols.py
getScoresInsertScores.py
calculatePercentScores.py
paintColumnsYellow.py
insertPhotos.py

They are all in /home/pedro/insertScores/python/

How can I get bash to call each one in this order?
 
Old 09-15-2018, 06:35 PM   #2
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Here's one way to do it.
Code:
find . -name "*.py" -exec \{} \;
EDIT: Why don't you put the functionality of each file into their own functions in a single file?

Last edited by individual; 09-15-2018 at 06:36 PM.
 
Old 09-15-2018, 06:39 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
pyscripts = "copyOldData.py insert3Cols.py getScoresInsertScores.py \
calculatePercentScores.py paintColumnsYellow.py insertPhotos.py"
for a in $pyscripts
do
    python $a
done
the \ is for new line breaks in string for readability, you do not have to use it. Instead you can put them all on the same line if you want to.

Last edited by BW-userx; 09-15-2018 at 06:40 PM.
 
Old 09-15-2018, 06:45 PM   #4
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by BW-userx View Post
pyscripts = "copyOldData.py insert3Cols.py getScoresInsertScores.py \
calculatePercentScores.py paintColumnsYellow.py insertPhotos.py"
Can't have a space between '=' when assigning a variable. Other than that, this way works as well.

Last edited by individual; 09-15-2018 at 06:46 PM. Reason: Removed extraneous code tag.
 
Old 09-15-2018, 07:16 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
If you can get what you want by executing each in sequence, then just write a script that runs each in sequence...
Code:
#!/bin/bash
cd /home/pedro/insertScores/python/
copyOldData.py
insert3Cols.py
getScoresInsertScores.py
calculatePercentScores.py
paintColumnsYellow.py
insertPhotos.py
Assumptions:
all of the python scripts have a correct #!
all are executable
 
Old 09-15-2018, 11:26 PM   #6
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Thank you all! As a non-geek, I struggle with this!

Quote:
Why don't you put the functionality of each file into their own functions in a single file?
@ individual

I tried, but none of the individual routines need a parameter.

I thought I could define each routine as a function:

def copyOldData()
mycode ...
def insert3Cols()
mycode ...
def getScoresInsertScores()
mycode ...
def calculatePercentScores()
mycode ...
def paintColumnsYellow()
mycode ...
def insertPhotos()
mycode ...

Then define main()
def main()
copyOldData()
insert3Cols()
getScoresInsertScores()
calculatePercentScores()
paintColumnsYellow()
insertPhotos()

Then just write:

go = main()

But this does not work, it stops at


Code:
 pedro@pedro-newssd:~/insertScores/python$ ./allStepsIn1.py
    File "./allStepsIn1.py", line 6
    def copyOldData
    ^
    SyntaxError: invalid syntax
    pedro@pedro-newssd:~/insertScores/python$
EDIT: I got it working after a tip: I forgot to put a : after the function brackets, like some_function():

Works like a dream now!

Last edited by Pedroski; 09-16-2018 at 12:17 AM.
 
Old 09-16-2018, 07:31 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I do not know Python, but could it not also be done like this, if one can call another python script within it, then call the next one when the prior one is finished doing whatever it is doing within the prior script. as redundant as that maybe, it might too me just one other way for doing this, so they are chained together calling for the next script to be ran as when it is finished.

though programming is programming, I have not seen nor know what it is doing per se' but putting everything into one script too should be possible. it just makes for a longer script on one sheet of Epaper.

Last edited by BW-userx; 09-16-2018 at 07:44 AM.
 
Old 09-16-2018, 07:54 AM   #8
Field95
LQ Newbie
 
Registered: Sep 2018
Location: xmpp:zemri@dismail.de
Posts: 13

Rep: Reputation: Disabled
Considering this is the working method.

Quote:
def copyOldData():
mycode ...
def insert3Cols():
mycode ...
def getScoresInsertScores():
mycode ...
def calculatePercentScores():
mycode ...
def paintColumnsYellow():
mycode ...
def insertPhotos():
mycode ...

Then define main()
def main():
copyOldData()
insert3Cols()
getScoresInsertScores()
calculatePercentScores()
paintColumnsYellow()
insertPhotos()
Alternatively to copy and pasting all your code, if you went through each original file and wrapped it in a function (like you did after you copied the code to the new file)

Eg for insertPhotos.py
Code:
def insertPhotos():
... insertPhotos.py original code ...
Then in a new file (__main__.py) you could do something like
Code:
from copyOldData import copyOldData                                             
from insert3Cols import insert3Cols                                             
from getScoresInsertScores import getScoresInsertScores                         
from calculatePercentScores import calculatePercentScores                       
from paintColumnsYellow import paintColumnsYellow                               
from insertPhotos import insertPhotos

def main():
    copyOldData()                                                               
    insert3Cols()                                                               
    getScoresInsertScores()                                                     
    calculatePercentScores()                                                    
    paintColumnsYellow()                                                        
    insertPhotos()

============

I suspect this could be more integrated then 6 wholly separate functions without arguments.
If OP posts the source code, I may be able to provide some examples

Last edited by Field95; 09-16-2018 at 07:59 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] rename as download wget sequentially bash ytd_usr Linux - Newbie 6 12-31-2015 01:02 PM
[SOLVED] Python TypeError when calling Python program from a Bash script noppeli Programming 2 01-15-2013 08:06 AM
[SOLVED] Bash script to run sequentially dpynn Linux - Software 11 02-27-2012 07:51 AM
how system call can be used in python deepmala8 Linux - Newbie 4 09-28-2011 12:34 AM
mulitple copy a existing directory and rename it at the end sequentially in BASH neo2k Linux - Newbie 2 08-20-2008 10:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration