maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Maemo 5 / Fremantle (https://talk.maemo.org/forumdisplay.php?f=40)
-   -   Writing shell scripts (https://talk.maemo.org/showthread.php?t=54224)

harelm 2010-05-28 11:49

Writing shell scripts
 
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? :(

acvetkov 2010-05-28 11:54

Re: Writing shell scripts
 
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.

StOoZ 2010-05-28 11:58

Re: Writing shell scripts
 
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

harelm 2010-05-28 12:14

Re: Writing shell scripts
 
ok, I just run it with:
sh /home/user/MyDocs/hw.sh
and i works - thanks :D
I prefer not to mess with the permissions yet.

and thanks for the fi comment :)

harelm 2010-05-28 12:56

Re: Writing shell scripts
 
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?

grog 2010-05-28 16:52

Re: Writing shell scripts
 
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

stlpaul 2010-05-28 16:59

Re: Writing shell scripts
 
http://www.opengroup.org/onlinepubs/...idx/shell.html

harelm 2010-05-28 17:02

Re: Writing shell scripts
 
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 2010-05-28 17:09

Re: Writing shell scripts
 
Quote:

Originally Posted by harelm (Post 685558)
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

rustler 2010-05-28 17:33

Re: Writing shell scripts
 
Quote:

Originally Posted by harelm (Post 685474)
ok, I just run it with:
sh /home/user/MyDocs/hw.sh
and i works - thanks :D
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" :D 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:D
Take care.


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

vBulletin® Version 3.8.8