![]() |
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 i get the line: -sh: /home/user/MyDocs/hw.sh: Permission Denied what am I doing wrong? :( |
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. |
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 |
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 :) |
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? |
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 |
Re: Writing shell scripts
|
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 :) |
Re: Writing shell scripts
Quote:
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 |
Re: Writing shell scripts
Quote:
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