0

My script is as follows:

#!/bin/sh

rm -rfv /home/user/Documents/Exercise/*

cp -rfv /home/user/Documents/ExerciseShare/ExerciseFiles/Word/Advanced/ /home/tp3/Documents/Exercise/

If i was to run these commands individually via Terminal they run ok. I have put them in a script (as above) and when I attempt to tun the script the Terminal windows flashes for about a second and nothing happens.

My attempts at solutions:

  1. Adding wait to the end of the script - no luck
  2. Right Click Script > Properties > Permissions > Execute - set
  3. Attempted to Run and Run in Terminal - no luck

I am using Ubuntu 12.10 64-bit

Mitchell
  • 153
  • Try to delete ` at the start/finish of script, on the other hand, first you delete a folder and in the next step you copy this folder to another location. Maybe first you want to copy the folder and after content its copied, delete it. – smile Mar 20 '13 at 08:22
  • the ` at the start of the script was just meant to add the code tag here on the forum sorry. I have a specific need to delete first. This code will be modified for different folders and files. So the delete command must come first. – Mitchell Mar 20 '13 at 11:16
  • 1
    When you say the Terminal window flashes, what do you mean? How are you running the script? If you're running it by making a Terminal window open just to run it and then close automatically, then what happens when you run your script manually in the terminal, by typing ./script, replacing script with the name of your script. (If that works, then it's especially important for you to explain precisely how you're running the script when it doesn't work.) – Eliah Kagan Mar 20 '13 at 11:21
  • see http://askubuntu.com/questions/38661/how-do-i-run-sh-files-in-terminal and http://unix.stackexchange.com/questions/31760/file-extensions-for-unix-shell-scripts – nutty about natty Mar 20 '13 at 11:36
  • The Terminal windows opens as if it is going to do something then closes almost instantly, hence it appears to flash. Here is the error when I run the script from terminal /home/user/Desktop/copyword.sh bash: /home/user/Desktop/copyword.sh: /bin/sh^M: bad interpreter: No such file or directory – Mitchell Mar 20 '13 at 20:49

2 Answers2

0

After many more hours of tinkering I have found an unlikely answer.

I created a new blank document and added only the two lines I wanted (without the heading of #!/bin/sh) :

rm -rfv /home/user/Documents/Exercise/*
cp -rfv /home/user/Documents/ExerciseShare/ExerciseFiles/Word/Advanced/ /home/tp3/Documents/Exercise/

I then saved the document, right-click > properties > permissions > execute as program

Bingo!

Thanks to all that helped!

Mitchell
  • 153
0

According to your comment, you had introduced an extra carriage return (\r) at the end of your shebang line

/bin/sh^M: bad interpreter

If you open your original file in vim, you'd see the ^M character; you could just remove it. Or you could open it in a hex editor such as hexedit, where you'd see an extra 0D.

Linux newlines are single newline characters (\n, 0x0A), as opposed to Windows newlines which still hark back to the days of typewriters (\r\n, 0x0D 0x0A).