LinuxQuestions.org
Help answer threads with 0 replies.
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 04-25-2013, 08:41 PM   #1
Yaesu
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Rep: Reputation: Disabled
Can you help me with my shell script I'm trying to write?


I need to create a script in linux that will check to see if a file exists and if it does then make 10 copies of itself in another directory. This is what I have but its not working. Can someone check this out for me.

Code:
#!/bin/sh
 if [ $1 -e ]
 do
 N=1
 while [$N -le 10 ]
 do
 cp $1 $2
 done
 else
 echo File does not exist.
 fi
 
Old 04-25-2013, 08:52 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Here's a few trs
1. indent for easier reading
Code:
#!/bin/sh    # try #!/bin/bash, its more capable than sh
# add the next line to enable the debugger
set -xv

 if [ $1 -e ]     # if [[ -e $1 ]]
 do               # delete
     N=1
     while [$N -le 10 ]      # use [[ ]] or even (( ))
     do
         cp $1 $2         # why $2? create a new name with eg numeric suffix
     done
 else
     echo File does not exist.
 fi
bookmark & read
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

HTH & Welcome to LQ
 
Old 04-25-2013, 09:02 PM   #3
Yaesu
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
I am using $2 as the second argument that way the user can enter where they want the files to be copied. I changed my code around and its not working.

Code:
#!/bin/bash
 
if [[ -e $1 ]]

   N=1
   while [[ $N -le 10 ]]
   do
      cp $1 $2
   done
   
else
     echo File does not exist.
fi

Last edited by Yaesu; 04-25-2013 at 09:08 PM.
 
Old 04-25-2013, 09:05 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
If you read those links, they'll show you everything you need.
Have a good attempt first, then come back if you're really stuck.
If/when you do, post your new code and show what happened when you ran it.
 
Old 04-25-2013, 09:11 PM   #5
Yaesu
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you for the links I am checking them out now.
 
Old 04-25-2013, 10:16 PM   #6
cortman
Member
 
Registered: Jan 2012
Location: ZZ9 Plural Z Alpha
Distribution: Crunchbang 11, LFS 7.3, DSL 4.1.10, Lubuntu 12.10, Debian 7
Posts: 219

Rep: Reputation: 43
Your copy could be better implemented with a for loop rather than while, i.e.,

Code:
for i in {1..10} ; do
    echo "frodo"
done
Can't recommend the Bash Beginner's Guide highly enough. My favorite CLI resource.
 
Old 04-26-2013, 12:19 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
When learning scripting you should include the following as your first line of code after the shebang every time (as indicated by chrism01):
Code:
set -xv
 
  


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
SHELL SCRIPT Write at the right of the shell window solo9300 Linux - General 3 09-29-2009 05:56 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 07:24 AM
help to write shell script vikrambhimbar Linux - Software 2 04-03-2006 05:43 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 04:00 PM


All times are GMT -5. The time now is 02:45 PM.

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