1

I am going to try my best to explain the problem I am facing. I created this custom shell command in linux

#!/bin/bash

function create() {
    python3 .create.py $1
    cd /home/simon/Linux_Storage/Projects/$FILEPATH$1
    code .
    echo "Project created."
    cd
}

What I am trying to do is to call it from a python script by using os.system("create "), but i get this error: sh: 1: create: not found . If I try to use any other default command like os.system("date"), or I just try to call my command from the terminal it works no problem. Is there a way to access custom shell commands from a python file? Or should I abandon the idea?

3 Answers 3

2

Prior to running your python program, you'll need to source the file containing the create script (lets call this create.sh). You'll also need to export the function so that it will be available to any process you spawn. For example:

. create.sh
export -f create
python -c 'import os; os.system("create")'
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried providing the full path to the command?

It looks like you only provided the name for the command. Your shell may know where to find it, but you might need to specify it for Python.

9 Comments

@SimoneLiberti I mean the path to the file that contains the bash code
I actually stored the command inside a file (my_commands.sh). How can I access it?
@SimoneLiberti As per geeksforgeeks.org/python-os-system-method , the system function just runs whatever string you type into it. So you could type the full path, somthing like os.system("/path/to/file/my_commands.sh)"
I tried with os.system("/home/simon/.my_commands.sh") and os.system("/home/simon/.my_commands.sh create ") but now it just does nothing at all
@SimoneLiberti you might consider making the file a script and removing the function.
|
0

You could add the full path as @majneeds2chill mentioned, provided the function is called inside the bash script. You can also add the full path to an alias called "create" to make the custom command feeling.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.