1

This is my entire script. it is simply to avoid having to type it again and again

cd api
rails s -p 3001 -b 0.0.0.0
cd ..

When I am in the directory of the script and run cd api it works just fine. However when I run the script via ./start_server It does not work. Here is the output of ls -al:

mendel@DESKTOP-LIKG5E5:/mnt/c/Projects/chaverim-update$ ls -al
total 8
drwxrwxrwx 0 root root  512 Apr 13 12:32 .
drwxrwxrwx 0 root root  512 Apr  5 12:40 ..
drwxrwxrwx 0 root root  512 Apr 13 12:09 api
-rwxrwxrwx 1 root root 1237 Apr  5 12:40 boxfile.yml
drwxrwxrwx 0 root root  512 Apr  8 16:54 .bundle
drwxrwxrwx 0 root root  512 Apr  8 16:54 client
drwxrwxrwx 0 root root  512 Apr 13 12:09 .git
-rwxrwxrwx 1 root root   11 Apr  5 12:40 .gitignore
-rwxrwxrwx 1 root root 1097 Apr  5 12:40 LICENSE
drwxrwxrwx 0 root root  512 Apr  5 12:40 nginx
-rwxrwxrwx 1 root root   82 Apr  5 12:40 Procfile
-rwxrwxrwx 1 root root   67 Apr  5 12:40 README.md
-rwxrwxrwx 1 root root  188 Apr  5 12:40 run_tests
-rwxrwxrwx 1 root root   28 Apr  5 12:40 start_client
-rwxrwxrwx 1 root root   41 Apr 13 12:52 start_server
drwxrwxrwx 0 root root  512 Apr  8 16:54 vendor

As you can see there is a folder called api at the top and the start_server script is set to have execution permissions.

Menachem Hornbacher
  • 2,080
  • 2
  • 24
  • 36
  • 3
    There's no point to `cd ..` at the end of a script -- a directory change is scoped to the shell it happens in (the one running the script), so it won't change the interactive shell that *started* the script-executing shell regardless. – Charles Duffy Apr 13 '18 at 17:00
  • BTW, "does not work" is generally rather uselessly vague. I'd strongly suggest showing an actual transcript showing the specific error received, and describing how it differs from expected behavior. – Charles Duffy Apr 13 '18 at 17:00
  • What does adding `pwd` at the start of that script show? – match Apr 13 '18 at 17:00
  • 1
    I'd also strongly suggest `cd api || exit`, to not even try to run `rails` (in the wrong directory) if the `cd` fails. Also note that your script should have a shebang (`#!/bin/bash` or similar) added; otherwise, it may not be able to be started *except* from an interactive shell. – Charles Duffy Apr 13 '18 at 17:01
  • 2
    (Also, `chmod 777` is a Really Bad Idea -- it defeats your system's entire security model, by letting even completely unprivileged processes -- a category that includes things like sandboxes running authentication code for untrusted network connections -- modify files that other processes can then execute). – Charles Duffy Apr 13 '18 at 17:02
  • @CharlesDuffy I added cd .. becuase on my old ubuntu machine I would be left in api for some reason :-(. – Menachem Hornbacher Apr 13 '18 at 17:24
  • @match Here is what `pwd` at the top of the script returns (second line, added `#!/bin/bash` to try and fix it): `./start_server: line 2: $'pwd\r': command not found` – Menachem Hornbacher Apr 13 '18 at 17:25
  • @MHornbacher, that means your script is saved as a DOS text file, not a Unix one. – Charles Duffy Apr 13 '18 at 17:26
  • @MHornbacher, ...it's literally the **very first thing** in the "Before Asking About Problematic Code" section of https://stackoverflow.com/tags/bash/info – Charles Duffy Apr 13 '18 at 17:27
  • @MHornbacher, ...wrt. "left in `api` for some reason" -- unless you were either (1) defining this as an alias or a function instead of a script, or (2) running it with `source scriptname` or `. scriptname` instead of `./script`, or (3) starting a new interactive shell *from within the script*, that's... exceedingly unlikely. It *is* how batch files work in the DOS world, but this isn't there. – Charles Duffy Apr 13 '18 at 17:28
  • @CharlesDuffy Sorry for not adding it before. I thought it was unnecessary when running it from the interactive shell. The file is written using vim in the Windows Subsystem For Linux (WSL) I'm not sure if that ends with NIX style endings or DOS style endings. Ubuntu broke down on me so I'm on windows till I can get my hands on a system that linux will play nice with – Menachem Hornbacher Apr 13 '18 at 17:31
  • 1
    `:set fileformat=unix`, and then saving with `:w`, will tell vim to convert to a UNIX text file. – Charles Duffy Apr 13 '18 at 17:32

0 Answers0