mardi 7 décembre 2021

How to store values to a file in shell script when executing the shell script from python file?

I need to trigger a shell script from a python file. That shell script needs to execute a few commands and store the data in another file.

Problem: I'm able to trigger the shell script from python but the commands getting executed inside the shell script is giving empty response and storing "Cg==" in respective file.

Shell script: randscript.sh

Generating random value and storing it to newfile.

echo Pass = $(echo $RANDOM | base64 | head -c 20)>>newfile

Python: randfile.py

Executing python file to trigger the shell script.

import os
import subprocess
def rand_func():
  try:
    path = '/home/ubuntu/execution'
    os.chdir(path)
    script_exec = subprocess.call(['sh','./randscript.sh'])
    print(script_exec)
    return True
  except Exception as err:
    print("Exception")
    return False
func_exec = rand_func()

Note: When the command was executed manually, it is returning the expected value (giving random value). But via python script, it is returning "Cg=="(which is a newline character). Could anyone please help me to understand this?




Aucun commentaire:

Enregistrer un commentaire