1

First off I am very very new to shell scripting. I am trying to write a script that takes in one parameter and then copies a folder in a different directory naming it using the parameter. This is the current code that I have:

#!/bin/sh
cd /var/www/html/fbplugin/chrome
sudo mkdir temp/$1
sudo cp -rf "/var/www/html/fbplugin/chrome/fbplugin" "/var/www/html/fbplugin/chrome/temp/$1"

When I run this code it says can't cd to /var/www/html/fbplugin/chrome. I'm not sure why it is saying this because I know the directory exists. I have copied the line directly and it works in terminal. If anyone could help me out that would be great.

If it matters in order to run the script I am typing "sh build.sh"

Raj3m8
  • 69
  • 3
  • 8
  • Since you have `#!/bin/sh`, the normal way to run the script is `./build.sh`; `sh build.sh` ignores the [shebang](http://en.wikipedia.org/wiki/Shebang_(Unix)). But try `bash build.sh`; I don't expect it to solve the problem, but I find that it will give a more specific error message. – Keith Thompson Feb 29 '12 at 23:29
  • When running bash build.sh I get the following: No such file or directorywww/html/fbplugin/chrome – Raj3m8 Feb 29 '12 at 23:35
  • Based on the answer you've accepted, the problem is that the script and the command you typed at the terminal were executed by different users. For future reference, this is critical information, and mentioning it in your question would have saved some time. – Keith Thompson Mar 01 '12 at 00:36
  • I'm really sorry, like I said I'm very new to shell scripting so I did not know that it was that the information was that valuable. I will definitely do so next time. – Raj3m8 Mar 01 '12 at 00:38
  • Exact duplicate: http://stackoverflow.com/questions/4918249/cd-does-not-work-in-shell-script – Anderson Green Sep 05 '12 at 16:12

2 Answers2

2

If that directory really exists, then you must have executed that script with a different user (cron, webserver, etc).

Check the rights for that directory.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

I don't know why you're getting the error about cd, but it looks like you could just use absolute paths throughout. That would solve the larger problem of the script working correctly.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
  • Yeah I've tried making them all absolute, it should work either way if the cd would just not give me this error. – Raj3m8 Feb 29 '12 at 23:25