5

i have very simple shell script

#!/bin/bash    
cp -rf /var/www/ksite/app2/* /var/www/ksite/app
echo "----"
echo "done"

but seems cp command fails

if i execute

cp -rf /var/www/ksite/app2/* /var/www/ksite/app

from terminal everything work ok. Can someone tell me how to include cp in shell script?

Thanks

12
  • 3
    Do you have any errors in script? Why you are thinking cp fails? Commented Jul 4, 2012 at 12:07
  • 1
    cp command fails in what way? What is the error message? Most likely it is a $PATH problem. Commented Jul 4, 2012 at 12:08
  • Did you make the shell script an eXecutable file? Commented Jul 4, 2012 at 12:10
  • Just covering all the bases .. do the permissions vary between the excutions .. i.e. do you execute one with sudo/root privileges, the other as user (unlikely, but thought I'd ask since we don't know what the exact error is) Commented Jul 4, 2012 at 12:11
  • 1
    @cdarke Not sure how the $PATH could be involved here .. certainly cp is on the path, no? Commented Jul 4, 2012 at 12:14

5 Answers 5

7

We seem to have doubt as to how this script fails. If there is no error message then this is a strange one. I suggest:

  1. On the command line (which works), do a which cp
  2. Whatever the reply, then copy that and use it as the cp in the script (e.g. /bin/cp)
  3. Check the widcard expansion, run your script with bash -x script-name and see if you get what you expect.
  4. echo $? after the copy in the script - if it is zero then it (thinks it) worked.
  5. Do a ls -ld /var/www/ksite/app from your script, maybe someone set a symbolic link?
  6. If it still fails, source the script from the command-line and see if that works . script-name
  7. Double check that the copy did actually fail! (maybe that should be step 1.)
Sign up to request clarification or add additional context in comments.

5 Comments

i tried with bash -x script-name and it worked, so i concluded that script works if called from terminal. Previously i was testing the script called from php file using '$output = shell_exec('bash /var/www/test/b.sh'); echo $output'; Why it is failing when used from php?
File permissions? Presumably PHP is running as a different user.
/bin/bash, /bin/cp and my .sh script are all set to 0755, so anyone can run these
AH HA! bash -x script-name worked you say? Maybe your #! line is wrong, see @unwind's answer above. Maybe your bash script is not running from PHP? PHP programs sometimes run in a weird environment (for security), are you picking up a restricted shell? By the way, it might have helped if you had mentioned PHP from the outset.
i found the problem. missing permission on the destination folder. so cp coundnt create files/folders and failed to execute. from terminal everything was ok as that user had permissions to create files in destination folder.
3

I had similar problem. What helped me:

  1. I used windows and putty to write script, so I had \r\n at the end of lines. Be sure, you have only \n symbol.

  2. I copied files and the only way it worked for me at script was cp <source_dir>/fileName <dest_dir>/fileName whereas at command line cp <source_dir>/fileName <dest_dir> worked well too.

Comments

2

Make sure you really have bash at /bin/bash. I think a batter hash bang is:

#!/usr/bin/env bash

This uses the env command to locate the bash binary and set the environment.

1 Comment

please see my comment above. script works ok. it is just cp command that doesn't anything (not copying files/folders and not showing any errors). The strange bit is that if i run the same in the terminal it works ok. I used absolute path intentionally.
1

Similar issue to Vladmir where the script was created in Windows. I created a new file "my_bash_script.sh" in the linux environment using VIM, then read the contents of my script into the file:

:r file_made_in_windows.sh

Then I saved, closed, then set the file as executable:

chmod 744 my_bash_script.sh

From there, I ran the script:

./my_bash_script.sh

...and it worked. What a weird issue. I was confounded for a moment.

Comments

0

Just covering all the bases .. do the permissions vary between the excutions .. i.e. do you execute one with sudo/root privileges, the other as user (unlikely, but thought I'd ask since we don't know what the exact error is)

1 Comment

@Kupe3 I brought up the permission issue 2 hours ago in a comment :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.