-1

Trying to make a script to automate activating a virtual environment for my Django project. I am running 'BIT BASH' on the windows 10 operating system. I have a feeling Windows 10 may be where my issues lie. Could anyone confirm this? Here is my script which I save as activatevirt.sh.
I run it by starting up the 'bit bash' command line program. Then to run the script, I enter the line:

sh activatevirt.sh

Commands such as echo work, but for some reason file system navigation commands are what seem to fall apart. This is why I suspect the issue to be the fact I'm doing this in windows 10 whereas others trying to offer help may be running on a Unix system. Please consider this if offering a solution.
If someone could simply give a simple brief bit of code that does nothing more than to change the directory in Bit Bash using a .sh script, I would be very grateful as I need to automate much more than just this, but this is where I am currently stuck. Below here is the code which doesn't work atm!

cd c:
cd virt
cd scripts
source activate
whoami

Errors which show up: line 1 no such file or directory c:

Caesar
  • 6,733
  • 4
  • 38
  • 44
  • `C:` is not a directory on Windows, it's a drive letter – knittl Jan 29 '22 at 09:25
  • When you run `sh script`, that starts a new copy of `sh`. All your `cd`s and `activate`s happen only to that new interpreter, not to whatever shell you started it from, so even if you fixed the immediate bug around `c:`, this script still wouldn't do anything useful (your command prompt would still be in the old directory without the virtualenv activated). – Charles Duffy Jan 30 '22 at 15:25

1 Answers1

1

I think you mean "git" bash? Try /C, or possibly /mnt/c, instead of c:.

You can use a single path too:

cd /C/virt/scripts

Or just source it without changing directory:

source /C/virt/scripts/activate

Note unix style (forward) slashes to separate directories, as opposed to Microsoft style backslashes (\).

There's also sh /C/virt/scripts/activatevirt.sh for example.

dan
  • 4,846
  • 6
  • 15
  • The OP is expecting their interactive shell to be in the new directory after the script runs. (Coming from Windows, that's not an unreasonable expectation, as `cmd` automatically `source`s batch files; but of course, it doesn't work that way here) – Charles Duffy Jan 30 '22 at 15:23
  • @CharlesDuffy Perhaps. I don't think it's clear if that's actually the intention or not, so I included both examples. – dan Jan 31 '22 at 09:38
  • yea i expected the interactive shell to end up in the new directory that the script directs to... but as you have now explained, obviously it is not happening. Is it possible for the script to influence the interactive shell in the way I am intending it to? – Not Applicable Feb 02 '22 at 02:47
  • @NotApplicable If you source a script (from an interactive shell) which changes the working directory (`cd`), the new directory will persist after the script ends. This doesn't happen if you execute the script as new process (eg `bash myscript.bash`). – dan Feb 02 '22 at 05:24
  • i want it to persist after the script is done running. How do i do that? You say to source the script from an interactive shell? How is that done? – Not Applicable Feb 03 '22 at 07:48
  • Just run this command in the shell: `source /path/to/script`. Running a script in this fashion is equivalent to typing out each line interactively. Sourced scripts run in the same execution environment from which they are called (such as an interactive shell), meaning any modification of that environment (variables, functions, the current working directory) will persist. Note also that `source` is a bash synonym for the more portable `. /path/to/script` (literally a dot). – dan Feb 03 '22 at 12:10
  • HEY HEY!!!! IT WORKS!!! – Not Applicable Feb 06 '22 at 08:44
  • OKAY SO NOW I UNDERSTAND!!! I had been told to run the script in Git Bash by typing "sh 'name of script.sh'" and this was giving me the issue as it was running it in a SEPARATE SHELL I assume because of the 'sh' at the beginning. Now that you kind folks have shown me the way of typing "source 'name of script.sh'" that I now have the script runnning in the source environment instead of a separate shell environment... am i understanding this correctly now? either way, it works how I intend and need it to now! Thanks a million guys! – Not Applicable Feb 06 '22 at 08:47
  • @NotApplicable Yeah `sh my-script` or `bash my-script` executes `my-script` in a new shell process. That's the usual way to run a script. But if you want to execute `my-script` in the current shell `source my-script` is essentially the same as typing out each line manually, in the current shell. – dan Feb 06 '22 at 09:13
  • Perfect! Took a little bit of finagling, but it seems we've gotten around to the solution! Thank you all. – Not Applicable Feb 16 '22 at 21:55