Reply
Thread Tools
Posts: 12 | Thanked: 14 times | Joined on May 2010
#1
Hi, I'm trying to write a simple shell script that copies files from
one folder to another.

from what I read saw and on other scripts, it shuld start with the shebang "#!/bin/sh", end with "fi" and be saved as a *.sh file.

I created a little "Hello World!" script:

Code:
#!/bin/sh
#My first script
echo "Hello World!"
fi
when i run the script from the xterm (as root)
i get the line:
-sh: /home/user/MyDocs/hw.sh: Permission Denied

what am I doing wrong?
 
Posts: 161 | Thanked: 70 times | Joined on Feb 2010
#2
check the permission, make it executable:
chmod +x file
./file to execute it
it should be able to execute also with:
sh file

there is no need for fi at the end.
 

The Following User Says Thank You to acvetkov For This Useful Post:
Posts: 130 | Thanked: 24 times | Joined on Jan 2010 @ Israel
#3
fi is needed in order to close an if conditional block, and since u dont have one,its useless

regarding permissions, change them using chown
 

The Following User Says Thank You to StOoZ For This Useful Post:
Posts: 12 | Thanked: 14 times | Joined on May 2010
#4
ok, I just run it with:
sh /home/user/MyDocs/hw.sh
and i works - thanks
I prefer not to mess with the permissions yet.

and thanks for the fi comment
 
Posts: 12 | Thanked: 14 times | Joined on May 2010
#5
Ok, another thing.
how can i tell him to copy from the script folder, and not from his current folder?

I tried with script_dir=$(dirname $0), and then
cp -r $script_dir/test.txt /home/user/MyDocs

it doesn't work.

how can I call the script folder to copy from it?

Last edited by harelm; 2010-05-28 at 13:21.
 
grog's Avatar
Posts: 546 | Thanked: 85 times | Joined on Feb 2008 @ Winnipeg, Canada
#6
Don't use a directory name at all. That assumes the current directory, whatever it happens to be.

Code:
cp -r test.txt /home/user/MyDocs
HTH
__________________
GROG!
N900 | ZAGG Body Armour | 16Gb A-DATA micro-sd
N810 | 2 x Patriot 8gb mini-SD | Boxwave Crystal Clear SS | Black Aluminum case | OTG dongle
N800 | 2 x 8gb OCX SD | Boxwave Anti-glare SS | PDAir book-style case
Holux M-1200 bluetooth GPS | iGo 4-row bluetooth keyboard | Linksys USB 10/100 ethernet | Plantronics Voyager 855 BT Headset
 
Posts: 1,141 | Thanked: 781 times | Joined on Dec 2009 @ Magical Unicorn Land
#7
 
Posts: 12 | Thanked: 14 times | Joined on May 2010
#8
grog - it didn't work, because i run the script through xterm from another folder.

anyway, it works, just windows notpad added strange characters for "enter"... deleted them and it's fine.

stlpaul - thanks, good link to keep
 
cddiede's Avatar
Posts: 1,034 | Thanked: 784 times | Joined on Dec 2007 @ Annapolis, MD
#9
Originally Posted by harelm View Post
Ok, another thing.
how can i tell him to copy from the script folder, and not from his current folder?

I tried with script_dir=$(dirname $0), and then
cp -r $script_dir/test.txt /home/user/MyDocs

it doesn't work.

how can I call the script folder to copy from it?

Absolute paths are your friends.
So if your script_dir directory is in your N900 base Mass Memory base directory it would look like this:

cp -r /home/user/MyDocs/script_dir /home/user/Mydocs
 
Posts: 47 | Thanked: 18 times | Joined on Jun 2008
#10
Originally Posted by harelm View Post
ok, I just run it with:
sh /home/user/MyDocs/hw.sh
and i works - thanks
I prefer not to mess with the permissions yet.

and thanks for the fi comment
Welcome to the scripting world!
Don't fear "chmod" and "chown" especially if you are using a different system to write your scripts. You will use them more than you probably care to as you move scripts from one system to another.
As for your "enter" problem, Microsoft and the *NIX world terminate lines differently. Rather than post a very short script here look up "dos2unix" and it will help you a little with scripting and the many ways people solve the same problem You can get away with out the conversion sometimes, but as in your case don't count on it.
Good luck and have fun
Take care.
 
Reply


 
Forum Jump


All times are GMT. The time now is 01:45.