1

OK, I know that there is a lot like this question but I literally get nothing. The thing i need is a script that cd's to /home/project then runs python3 pyscript.py I created an alias to cd alias name='cd /home/project but when I run a shell script I get an error:

: not found: 3: script: script.sh: name ' [Errorno 2] No such file or directory : not found: 5: script.sh

My sh script is:

#!/bin/bash name python3 pyscript.py

Thanks for your help.

3
  • why do you need it to cd to the directory? why not just run it straight from there e.g /home/project/pyscript.py
    – alpha
    Commented Jul 23, 2018 at 11:46
  • Don't we need python3 front of pyscript.py to run it. I changed script to /home/project/pyscript.py (I think you meant this) then it gave: not found 3: not found 4:
    – akkaygin
    Commented Jul 23, 2018 at 11:50
  • 1
    @DeclanGallagher We don't know if the script uses relative paths, in which case it would have to run with a particular working directory.
    – Kusalananda
    Commented Jul 23, 2018 at 12:06

1 Answer 1

0

The error in your bash is the unknown command name. I don't know what your intention with this command is, so I can not comment further on it.


To run your Python script with /home/project as the working directory, directly on the command line:

( cd /home/project && python3 pyscript.py )

The command is in parentheses so that the cd does not affect the working directory of the interactive shell session.

As an alias:

alias mypyscript='( cd /home/project && python3 pyscript.py )'

As a shell function:

mypyscript () (
    cd /home/project && python3 pyscript.py
)

As a shell script:

#!/bin/sh
cd /home/project && python3 pyscript.py

In all of the above, python3 pyscript.py would not be invoked if the cd failed.

2
  • So I changed lines to cd /home/project && python3 pyscript.py but its still giving me Errorno 2 no such file or directory. I'm 100% sure that path to directory is correct. Also sorry for bothering you with this easy problem.
    – akkaygin
    Commented Jul 23, 2018 at 11:57
  • @AKKaygin That's the error that Python gives when it can't find the script file. You should look at the contents of /home/project to make sure that the script file pyscript.py lives there and that you are spelling the name of it correctly.
    – Kusalananda
    Commented Jul 23, 2018 at 12:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .