LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 07: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, 07:52 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 08: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 08:08 PM.
 
Old 04-25-2013, 08:05 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 08: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, 09: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-25-2013, 11:19 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:23 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
Open Source Consulting | Domain Registration