0

I'm trying to recreate a .bat file in a Mac environment. I want an executable file that will navigate to a directory and carry out some steps.

I have created build-env.sh and made it executable using chmod +x build-env.sh. But the script can't seem to properly navigate to a directory location, which is the first step. I've stripped it down so that this is currently the entire contents of the script:

echo $(pwd)
cd ~/Desktop
echo $(pwd)

In the Terminal, I navigate to the directory where the file is located, and I enter ./build-env.sh. When I run this from the Terminal, the output I receive is:

/Users/XXX/Desktop/scripts/python/myfolder
: No such file or directory /Users/XXX/Desktop
/Users/XXX/Desktop/scripts/python/myfolder

I have searched online but come up empty. I've tried so many variations of filepaths after the "cd" (full paths, partial paths, text strings, etc.) but nothing works. Every time, the Terminal reads "No such file or directory."

To be clear, I have no problem navigating to directories when manually using the Terminal. If I copy the location that "doesn't exist" and paste it in after "cd", it works fine.

I have also tried assigning the path to a variable as below. Took these lines from an answer on this post, added them to the end of the script file, and made the same iterations (full/partial/string/etc.) on the variable.

DIR=~/Desktop
if [ -d "$DIR" ]; then
  echo "yes"
fi
if [ ! -d "$DIR" ]; then
  echo "NO"
fi

This doesn't result in any change. Neither "yes" nor "NO" are incorporated into the output.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
TechTyler
  • 13
  • 2
  • 2
    The way the error message is garbled suggests your script file is in DOS/Windows format, which will cause [all sorts of trouble](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings). You need to convert it to unix format (and them use editors that store in unix format). BTW, .bat files are also a DOS/Windows thing; macOS and other unixes use shell scripts, not batch files. "bash" is not the same as "batch". Also, I don't know if this is your intent, but changing directories in a script will not affect the parent shell, just the script itself. – Gordon Davisson Apr 19 '22 at 20:51
  • Please add output of this command to your question (no comment here): `file build-env.sh` – Cyrus Apr 19 '22 at 20:55
  • 1
    FYI, `$(pwd)` is much slower than `$PWD`; every time you use `$(...)` it `fork()`s off a subprocess, creates a pipe between the subprocess and the parent, reads the subprocess's output into the parent and waits for it to exit; whereas `$PWD` is just a variable lookup and basically instant. – Charles Duffy Apr 19 '22 at 21:08
  • that said, I wholeheartedly agree with Gordon; the `:` at the beginning of the error message makes this being a DOS-vs-UNIX-file-format issue basically a sure thing. – Charles Duffy Apr 19 '22 at 21:10

0 Answers0