Issue
I have a simple shell script script.sh
:
echo "ubuntu:$1" | sudo chpasswd
I need to open the script, read it, insert the argument, and save it as a string like so: 'echo "ubuntu:arg_passed_when_opening" | sudo chpasswd'
using Python.
All the options suggested here actually execute the script, which is not what I want.
Any suggestions?
Solution
You would do this the same way that you read any text file, and we can use sys.argv to get the argument passed when running the python script.
Ex:
import sys
with open('script.sh', 'r') as sfile:
modified_file_contents = sfile.read().replace('$1', sys.argv[1])
With this method, modified_file_contents
is a string containing the text of the file, but with the specified variable replaced with the argument passed to the python script when it was run.
Answered By - John Paul R