-1

I have an installation script running, and the last thing the script does is it runs npm install in my testrepo directory.

This is how my folders looks like: enter image description here

The testrepo is a node project, containing the package.json file.

Now, as part of my install.sh script, I try to do the following:

 echo "Installing test repo node packages and dependencies...." &&
 cd testrepo/ && 
 npm install &&
 echo "Finished installing"

However, this does not seem to run the npm install in the testrepo, and still says in has finished installing.

What is a good work around to fix this issue?

Note: The install.sh script has a lot of tasks to be done inside it.

Venkat.R
  • 7,420
  • 5
  • 42
  • 63
emisqwe
  • 69
  • 1
  • 9
  • please share the error you are getting in command line – Venkat.R Jan 03 '17 at 12:06
  • What's with the `&&`s?! – Biffen Jan 03 '17 at 12:07
  • 1
    Why do you think that "this does not seem to run the `npm install`"? – Ruslan Osmanov Jan 03 '17 at 12:07
  • There is no error. im new bash scripting, and from reading online apparently you add && in order to wait for the command to finish before running the next one. It doesnt run the npm install because it cannot change directory from the script itself, thats why - hence looking for workaround... – emisqwe Jan 03 '17 at 12:10
  • @emisqwe, try this: `dir=$(cd $(dirname "$0"); pwd); cd "$dir/testrepo && npm install"` – Ruslan Osmanov Jan 03 '17 at 12:12
  • try doing `npm install --verbose` or `npm i --verbose` – Venkat.R Jan 03 '17 at 12:12
  • @emisqwe No, no, no. Commands on separate lines will wait for the previous ones to finish per default. If you need to be sure that the previous command was *successful* then you can use `&&` but you *mustn't* put a newline after it. Or you can use `set -e` to get that behaviour (well, sort of) globally. – Biffen Jan 03 '17 at 12:12
  • @Biffen so when do you use && ? – emisqwe Jan 03 '17 at 12:14
  • @emisqwe: `&&` is used mainly in command line – Venkat.R Jan 03 '17 at 12:14
  • @emisqwe, it is the way to check the exit status of the previous command. It is a logical _AND_. If the return status is zero (success), the next command is executed. For other status codes (error), the next commands are ignored – Ruslan Osmanov Jan 03 '17 at 12:15
  • @RuslanOsmanov same issue even with implementing your solution - this is the issue I think, just not sure how to solve it - http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script – emisqwe Jan 03 '17 at 12:15
  • 1
    @Venkatraman I disagree. `&&` can be, and is, used *a lot* in scripts as well. – Biffen Jan 03 '17 at 12:16
  • @RuslanOsmanov it works great if I do it manually, but it has to be done within the install.sh script – emisqwe Jan 03 '17 at 12:18
  • 2
    @emisqwe Could you try removing all the `&&`s, adding `set -ex` at the top of the script (but below the shebang) and showing us the full script, the exact command you use to run it as well as the complete output? – Biffen Jan 03 '17 at 12:21
  • @Biffen, you misunderstood my point. the given line is command level. `&&` in script is scripting level. – Venkat.R Jan 03 '17 at 12:35
  • @Venkatraman I've absolutely no idea what you're trying to say. – Biffen Jan 03 '17 at 12:36
  • @Biffen, `&&` inside is an `operator` and has many usage – Venkat.R Jan 03 '17 at 12:38
  • @Biffen There is no problem putting a newline after `&&`; a command cannot end with a `&&`, so the shell knows to read the next command from the next line. – chepner Jan 03 '17 at 12:43
  • @Venkatraman `&&` is often used in scripts, as Biffen said. – chepner Jan 03 '17 at 12:44
  • @chepner You're right, my bad. – Biffen Jan 03 '17 at 12:45

1 Answers1

0

With the && you create an expression. That is where it goes wrong.

As an example, if my current directory is /tmp, and I execute

(cd .. ; pwd ) && pwd

you can see what happens:

/
/tmp
Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • im afraid even after removing the &&, the issue is still there – emisqwe Jan 03 '17 at 21:09
  • In the script without the `&&`s, try putting `pwd` in before and after your `npm install` (on a separate line). This is will show you what is hapening. – Ljm Dullaart Jan 04 '17 at 10:34