LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-05-2011, 10:41 PM   #1
supernova88
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Rep: Reputation: Disabled
Noob questions: making script to copy files to another folder


hello all

I am new here and have just started looking into dcripting, im fairly new into this whole unix enviroment but I am some what getting the hand of it to an extent.

I was asked to make a simple script to copy files from my "hfiles " directory to a "www" folder at leats I think its a directory, it has the permissions as "lrwxrwxrwx " but its like a public branch

So I proceeded to make a file entittled MkPublic to copy the "hfiles" directory to the "www" branch

here is the code i used:

--------------------------

#!/bin/sh
# COPIES FILES

echo "copying files"
if [ $1="hfiles" ]
then cp $1 > $2
if [ $2="www"
then
echo "copying done"
fi
exit 0
"""""""""""""""""""""""""""""""

when i try to run the script it tells me that there is a syntax error and an unexpected end of file


and nothing seems to get copied anywhere so clearly there is something wrong with one im doing, i tried multiple ways : using the quotes, spacing the equal signs, but I seem to still get the same results and its really bugging me that as somet6hing as easy as this and I cant seem to figure it out.

so any help would be greatly appreciated

thanks in advance
 
Old 06-05-2011, 10:54 PM   #2
Rearden888
LQ Newbie
 
Registered: Jan 2010
Location: Anacortes, WA
Distribution: Crunchbang, Debian, Gentoo
Posts: 25
Blog Entries: 2

Rep: Reputation: 3
Quote:
Originally Posted by supernova88 View Post
#!/bin/sh
# COPIES FILES

echo "copying files"
if [ $1="hfiles" ]
then cp $1 > $2
if [ $2="www"
then
echo "copying done"
fi
exit 0
In the if statements you need a semicolon after the "if [condition];" also, you're missing an end square bracket in the second if statement as well.
 
Old 06-05-2011, 11:03 PM   #3
supernova88
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
would this be correct ?

___________________


#!/bin/sh
# COPIES FILES

echo "copying files"
if [ $1="hfiles" ]:
then cp $1 > $2
if [ $2="www" ]:
then
echo "copying done"
fi
exit 0
____________________________

if so I still get the syntax ending error and the files still dont copy, ive switched up the script so many times, probably why i forgot the bracket, still dont get why it isnt working though...

and thanks for the fast reply
 
Old 06-05-2011, 11:12 PM   #4
supernova88
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Im also thinking that the "then" statement isn't right either "then cp $1 > $2" I dont know, but that might be my problem no ?
 
Old 06-06-2011, 12:31 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,295

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
Quote:
In the if statements you need a semicolon after the "if [condition];"
No, you don't. In shell, end-of-line counts as a cmd terminator

You need spaces around '=' for comparisons.

Put the cp cmd on it's own line.
The cp syntax is
Code:
cp src tgt
for a file. If the src & target are dirs and you want the contents copied (ie files)
[code]
cp dir1/* dir2
[code]
Personally I'd advise absolute (complete) path to each dirname
Code:
cp /a/b/dir1/* /a/b/c/dir2
Each 'if' needs a matching 'fi'

The 'exit 0' is redundant unless your calling env is going to check it.
The default exit value is the exit status of the last cmd run.

Read/bookmark these links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Last edited by chrism01; 06-06-2011 at 12:33 AM.
 
1 members found this post helpful.
Old 06-06-2011, 12:52 AM   #6
supernova88
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
I absolutely love those manuals that you have posted on the bottom of your thread, definitely going to read the first one as its seems to be an all guide manual to everything Linux.

Okay I'll try using the absolute path but how would I use the $ method, where $1 and $2 could be my variables for the [ paths ? wouldn't that be less complex?

and I alos put the shell script that ive created into its own "scripts folder" would i be able to run the script from there or should i run it from my home directory ?
 
Old 06-06-2011, 01:08 AM   #7
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
you should be able to run your script from anywhere

for example
Code:
# cd /some/folder | ll
your_script.sh
# ./your_script.sh
or you can run it like this
let's assume your script is in the folder /some/folder
Code:
# /some/folder/your_script.sh
i hope that answers your question?
But don't forget to make your file executable
Code:
chmod +x your_script.sh
or
Code:
chmod 755 your_script.sh
 
Old 06-06-2011, 01:16 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,295

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
That was using explicit values to show you. Of course you can pass those in as params
Code:
/dir/script.sh /a/b/src /a/b/c/tgt
Also, use bash not sh ie
Code:
#!/bin/bash
All variations on 'shell' are slightly different, unless the name has been 'symlinked' to another name (don't worry about that right now; stick to bash).
 
Old 06-06-2011, 01:52 AM   #9
supernova88
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank you so much for the replies guy I definitely wanted to make sure that the script could have ran from anywhere cause perhaps that could have been the problem. How different ios the shell script and the bash script from one another ? are they really that different ?

cause i was always taught to do t6he if statements and use the shell script, so thats why I am a little bit un aware on anything else lol

ill try it out and figure it out once again...

oh and can anyone tell me what my www folder is, or if its even a folder, i cause i remember making like a linked or a short link and named it www (which is in lighht blue ) and the actualy folder name i guess which has

"lrwxrwxrwx 1 cewp08 users 19 May 18 18:31 www -> /public/home/cewp08/"

I only ask because maybe a reason why i it might not be working is because perhaps im using the wrong path? But it shouldn't matter since its a short link anyways...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy files and folder except some files and folders chintan_gaurav8 Linux - General 1 06-08-2010 01:36 AM
i need to copy files from one folder to another folder using shell anurupr Linux - Newbie 17 03-04-2010 09:07 AM
Bash script to copy contents of folder brian.m Linux - General 8 05-01-2009 06:08 PM
making a script that will move a file or files in a trash folder Paxmaster Programming 5 12-12-2004 06:00 PM
How to copy in a script files to a samba folder? Julianus Linux - Networking 1 10-09-2004 07:29 PM


All times are GMT -5. The time now is 06:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration