0

I have been having issues changing directories using a shell script in windows 10.

In notepad I created a file, test.sh, which has the following script:

#!/bin/bash
cd '/mnt/p/WMScriptTest/'

I then mounted the Ubuntu terminal to the location of the test.sh file by typing in cd /mnt/p/ which works. Next, I typed in ./test.sh and keep getting the following error message:

: No such file or directory/p/WMScriptTest/

I am new to using ubuntu and any help/suggestions are greatly appreciated. Thank you

dessert
  • 39,982
  • Please [edit] and add the output of ls -l /mnt/p. In a script one normally doesn’t use cd, but rather absolute paths. You can store paths in variables if that makes it easier, e.g. mypath=/mnt/p/WMScriptTest and then mkdir "$mypath/mydir" and cp "$mypath/file1" "$mypath/mydir". – dessert Jun 04 '18 at 18:08
  • I just seem to be unable to change directories from code in my shell script. One of the folders listed from ls -l /mnt/p/ is WMScriptTest that I am trying to change directories to. If I change directories within the terminal itself it works though. – Justin S. Jun 04 '18 at 18:44
  • Is the error message really (literally) : No such file or directory/p/WMScriptTest/? That looks mangled somehow… – dessert Jun 04 '18 at 18:46
  • Yes, that is the exact message that I get – Justin S. Jun 04 '18 at 18:47
  • 2
    Maybe a problem with line endings? https://askubuntu.com/q/803162/507051 – dessert Jun 04 '18 at 18:48
  • Thank you for your help so far. That may be what the issue is. However, when I try to get dos2unix by typing "sudo apt-get install dos2unix" I get a message that it is "unable to locate package dos2unix" – Justin S. Jun 04 '18 at 18:54
  • I’m clueless when it comes to repositories in WSL – try the sed approach maybe? ;P Oh, does it work in the command line directly btw? Please try bash -c "cd '/mnt/p/WMScriptTest/' && pwd", this should print “/mnt/p/WMScriptTest”. – dessert Jun 04 '18 at 19:01
  • Yes, that does print "/mnt/p/WMScriptTest" Also I was able to install dos2unix and used it on the file, but now when I run it in the terminal it gives no message at all and the directory remains unchanged. – Justin S. Jun 04 '18 at 19:13
  • So it works, great! :) A script runs in a subshell, it can’t change the directory of the shell it’s started from. If you want to test your script, add a line with pwd after the cd line. If you actually want to do something completely different, please [edit] your question and change it accordingly. – dessert Jun 04 '18 at 19:18
  • You’re welcome! Did you manage to solve the problem? If so, please add an answer below. – dessert Jun 05 '18 at 08:49
  • Yes everything is solved now! – Justin S. Jun 05 '18 at 13:10
  • Please add an answer below explaining the solution and possible misunderstandings so that it can help others as well. – dessert Jun 05 '18 at 13:24

0 Answers0