EFZ
EFZ

Reputation: 157

Inserting User Inputted String into Linux commands [python script]?

Apologies if this question is trivial, I'm an amateur with python..

I'm currently in the process of attempting to automate a series of linux commands, using a python script. (Specifically in relation to the downloading and analysis of chandra telescope data using ciao commands)

In short what I'm aiming for is that the script asks the user to enter a 5 character long numerical string, which is stored as a variable and subsequently inserted into commands, to be run from the script.

E.G the following commands, where the asterisks would be replaced by the user inputed string.

import os
os.system("download_chandra_obsid *****")
os.chdir("usersc/####/Downloads/*****") 
os.system("dmkeypar acisf*****0N003_full_img2.fits dec echo=yes")

etc

Is there a simple way to implement this?

Thanks in advance...

Upvotes: 1

Views: 564

Answers (2)

Evya
Evya

Reputation: 2375

import os

user_inputted = input('Enter your string')
# Perform validations on the input like length etc.
os.system("download_chandra_obsid {}".format(used_inputted))
os.chdir("usersc/####/Downloads/{}".format(used_inputted))
os.system("dmkeypar acisf{}0N003_full_img2.fits dec echo=yes".format(used_inputted))

Upvotes: 0

M. Volf
M. Volf

Reputation: 1482

Get number from user using number = input("Please type five digit number").strip(). If you want to check if it's a valid number, use this construction:

while True:
    num = input("Please type five digit number").strip()
    if num.isdigit() and len(num) == 5:
         break

Then use .format() with every that string to add the number there like this:

os.system("download_chandra_obsid {0}".format(num))

(This will only work with python 2.6+, I think)
See https://pyformat.info/#simple or python docs for more details on str.format()

Upvotes: 1

Related Questions