Python Forum
using a shell script within python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using a shell script within python
#1
I want to use a C-shell script within a python program, which works with two arguments.

os.system("recall 20170121 ../E725.txt xy 1")

But I want to use it for a stack, so declared the variables like below, but when I call them within the script it gives an error, that the input file doesn' exist. How can I call the variables?

date_ID=(filename[17:25])
fullpath = '../%s' % (filename)
os.system("import_S1_TOPS_modified $date_ID $fullpath vv 1")
Reply
#2
(Oct-10-2018, 09:26 AM)Krszt Wrote: I want to use a C-shell script within a python program, which works with two arguments.

os.system("recall 20170121 ../E725.txt xy 1")

But I want to use it for a stack, so declared the variables like below, but when I call them within the script it gives an error, that the input file doesn' exist. How can I call the variables?

date_ID=(filename[17:25])
fullpath = '../%s' % (filename)
os.system("import_S1_TOPS_modified $date_ID $fullpath vv 1")

As far as Python interpreter is concerned, $date_ID and $fullpath in your command line are just string literals within a string. In order to place values within the command string
  • If you are using version >= 3.6 - modify your string as
    f"import_S1_TOPS_modified ${date_ID} ${fullpath} vv 1"
  • if not
    "import_S1_TOPS_modified ${} ${} vv 1".format(date_ID, fullpath)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Should not be using os.system(),subprocess replace it a better/safer way.
Ideally is better/safer to pass in a list.
import subprocess

subprocess.run(['ls', '-l'])
Passing in a string has to use shell=True
import subprocess

subprocess.run('ls -l', shell=True)
So as shown bye @volcano63 can pass in using f-string or format(),
but the method with list when shell=False is advisable.
import subprocess

arg = '-l'
subprocess.run(f'ls {arg}', shell=True)
The subprocess has a lot stuff that make it better,even run(that replace call) can now also catch output.
import subprocess

out = subprocess.run(['ls', '-l'], capture_output=True)
print(out.stdout.decode())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  cant use ping, sudo or other commands in remote shell script. throwaway34 7 838 May-17-2021, 11:29 AM
Last Post: throwaway34
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 652 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE
Bug Python Shell 3.9.0 - Issue with indentation Earis 17 1,945 Oct-31-2020, 07:00 AM
Last Post: Earis
  How to kill a bash script running as root from a python script? jc_lafleur 4 1,597 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 886 May-28-2020, 05:27 PM
Last Post: micseydel
  Python "Terminal" vs "Shell" SectionProperties 2 1,122 Apr-10-2020, 08:36 AM
Last Post: SectionProperties
  python shell teelado 1 949 Jun-13-2019, 01:49 AM
Last Post: Larz60+
  Package python script which has different libraries as a single executable or script tej7gandhi 1 1,257 May-11-2019, 08:12 PM
Last Post: keames
  Is it possible to have my python shell change the backgrounds colour? OTO1012 2 1,757 Mar-07-2019, 09:32 PM
Last Post: woooee
  running python script from shell invoked with os.system("x-terminal-emulator -e /bin/ markhaus 2 1,507 Feb-21-2019, 11:55 PM
Last Post: markhaus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020