Why can't bash see my files?

SuperAce99

I have a simple bash script that allows cron to execute a series of Python scripts in a virtualenv. The script keeps raising No such file or directory errors.

~/nightly.sh works fine:

#!/bin/bash
source virt_env/myproject/bin/activate
cd virt_env/myproject/main
python script1.py
python script2.py

I want to keep everything in ~/virt_env/myproject/main/ to simplify deployment. I thought I could call bash virt_env/myproject/main/nightly.sh on this:

#!/bin/bash
MAINDIR=`dirname $0`
cd $MAINDIR
source ../bin/activate
python script1.py
python script2.py

but I get the No such file or directory. If I manually cd to ~/virt_env/myproject/main/, then I can run the main commands no problem. Clearly I'm missing something about how dirname and cd work in this context.

How can I point bash at the right place?

Solution

As proposed in the accepted answer, it's best to avoid calling cd from within the script and use an explicit path variable instead. Here's the working version of virt_env/myproject/main/nightly.sh:

#!/bin/bash
MAINDIR=`dirname $0`
echo "The main directory is" $MAINDIR

# Activate virtual environment
source $MAINDIR/../bin/activate

# Run Python scripts
python $MAINDIR/python1.py
python $MAINDIR/python2.py

Because the Python scripts are now called from an arbitrary path, I needed to update the python scripts to be smarter about path awareness as well.

This code fails because os.path.basename omits path information:

# works when called with "python python1.py"
# fails when called with "python $MAINDIR/python1.py"
CONFIG_FILE = os.path.basename(__file__)[:-3] + ".config"
f = open(CONFIG_FILE,"r")

Updating it to use os.path.abspath fixes the problem:

# works regardless of how it is called
CONFIG_FILE = os.path.abspath(__file__)[:-3] + ".config"
f = open(CONFIG_FILE,"r")
Fred Mitchell

Perhaps it would simply be better to eliminate the 'cd' command. Invoke everything from a full path specification. In your example add $MAINDIR/ to the executables.

Your bash script can then be in any directory where the executables are reachable. You are not exposed to the problems of what happens when cd fails.

Example:

cd yourdir
rm -f yourglob   # oops things got removed from where you started if yourdir did not exist.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why I can't see my files inside a docker container?

From Dev

Can't see my files with "ls" anymore

From Dev

Why can't I see all of the files that are in my workspace in xcode in the left sidebar?

From Dev

Why can't I see all of the files that are in my workspace in xcode in the left sidebar?

From Dev

Anonymous user can't see my uploaded files on vsftpd

From Dev

Why xcode can't see these files, however they physically exist

From Dev

Why can't my script find the files?

From Dev

Why can't my script find the files?

From Dev

Why can't I delete my files?

From Dev

Why can't I see the parameters for my script in PowerShell?

From Dev

Why can't apache see my python module?

From Dev

Why can't Sass see my .scss file in an Express app?

From Dev

Why can't my Windows Home Server see the internet?

From Dev

Why can't I see remote video in my WebRTC app?

From Dev

Why can't I see my taken pictures in gallery?

From Dev

why can't linux see my run.sh command?

From Dev

Why can't I see my php code rendered on page?

From Dev

Why can't I see the parameters for my script in PowerShell?

From Dev

Why can't I see my file in RAM?

From Dev

Why can't I see my script on ps program output?

From Dev

Why others can't see my wordpress website

From Dev

Why can't I see my internal network in VM?

From Dev

why I can't see my tables in cloudboost?

From Dev

Why can't I see my other partitions on Windows 10?

From Dev

Why can't Sass see my .scss file in an Express app?

From Dev

I can't see why my scanner in java is throwing a "NoSuchElementException"

From Dev

Why can't my project see Firebase as an import

From Dev

Why can’t see the preview of my custom view in storyboard?

From Dev

Why my program can't see that there is a registry key?

Related Related

  1. 1

    Why I can't see my files inside a docker container?

  2. 2

    Can't see my files with "ls" anymore

  3. 3

    Why can't I see all of the files that are in my workspace in xcode in the left sidebar?

  4. 4

    Why can't I see all of the files that are in my workspace in xcode in the left sidebar?

  5. 5

    Anonymous user can't see my uploaded files on vsftpd

  6. 6

    Why xcode can't see these files, however they physically exist

  7. 7

    Why can't my script find the files?

  8. 8

    Why can't my script find the files?

  9. 9

    Why can't I delete my files?

  10. 10

    Why can't I see the parameters for my script in PowerShell?

  11. 11

    Why can't apache see my python module?

  12. 12

    Why can't Sass see my .scss file in an Express app?

  13. 13

    Why can't my Windows Home Server see the internet?

  14. 14

    Why can't I see remote video in my WebRTC app?

  15. 15

    Why can't I see my taken pictures in gallery?

  16. 16

    why can't linux see my run.sh command?

  17. 17

    Why can't I see my php code rendered on page?

  18. 18

    Why can't I see the parameters for my script in PowerShell?

  19. 19

    Why can't I see my file in RAM?

  20. 20

    Why can't I see my script on ps program output?

  21. 21

    Why others can't see my wordpress website

  22. 22

    Why can't I see my internal network in VM?

  23. 23

    why I can't see my tables in cloudboost?

  24. 24

    Why can't I see my other partitions on Windows 10?

  25. 25

    Why can't Sass see my .scss file in an Express app?

  26. 26

    I can't see why my scanner in java is throwing a "NoSuchElementException"

  27. 27

    Why can't my project see Firebase as an import

  28. 28

    Why can’t see the preview of my custom view in storyboard?

  29. 29

    Why my program can't see that there is a registry key?

HotTag

Archive