# While loop

**URL:** <https://community.unix.com/t/while-loop/316014>\
**Category:** Shell Programming and Scripting\
**Created:** [August 31, 2012, 3:21am UTC](https://community.unix.com/t/while-loop/316014 "2012-08-31T03:21:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jainitai](https://community.unix.com/letter_avatar/jainitai/32/6_22734026db12803ebb3e059622ac3534.png) [@jainitai](https://community.unix.com/u/jainitai)\
**Post date:** [August 31, 2012, 3:21am UTC](https://community.unix.com/t/while-loop/316014/1 "2012-08-31T03:21:17Z")

</div>

How to copy a file from source location and paste 5 times in destination location using while loop??

Kindly help??

---

<div class="post-metadata">

**Author:** ![bmk](https://community.unix.com/letter_avatar/bmk/32/6_22734026db12803ebb3e059622ac3534.png) [@bmk](https://community.unix.com/u/bmk)\
**Post date:** [August 31, 2012, 3:29am UTC](https://community.unix.com/t/while-loop/316014/2 "2012-08-31T03:29:05Z")

</div>

while pasting filenames in destination location..we can't past same file name you want different file name. Please post the with input data and output data.  
Thanks

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/6_22734026db12803ebb3e059622ac3534.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [August 31, 2012, 3:31am UTC](https://community.unix.com/t/while-loop/316014/3 "2012-08-31T03:31:09Z")

</div>

> [@jainitai](#):
>
> How to copy a file from source location and paste 5 times in destination location using while loop??
> 
> Kindly help??

Didn't clear actually what you are trying to say...

Assuming you want to copy one file to 5 destinations..

Copy those destinations to one file... - Dest\_Path

```plaintext
while read line
do
cp /Source_path/file $line
done<Dest_Path

```

---

<div class="post-metadata">

**Author:** ![jainitai](https://community.unix.com/letter_avatar/jainitai/32/6_22734026db12803ebb3e059622ac3534.png) [@jainitai](https://community.unix.com/u/jainitai)\
**Post date:** [August 31, 2012, 3:36am UTC](https://community.unix.com/t/while-loop/316014/4 "2012-08-31T03:36:10Z")

</div>

ok fine I have 3 file in source directory named as  
sample.dat  
sample1.dat  
sample2.dat  
sample3.dat  
sample4.dat  
so I should copy any file from here to destination location where there are no files in destination directory.

I used the following code but it showing no such file or directory

```nohighlight
#!/bin/ksh
file="/pathname/
dest="/pathname/
while read file
do
echo $file
cp "$file" "$dest"
done < "$file"

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [August 31, 2012, 4:08am UTC](https://community.unix.com/t/while-loop/316014/5 "2012-08-31T04:08:50Z")

</div>

The closing double quotes in the two variable assignments are missing, the loop variable "file" has the same name as the variable that contains the file name. I think you are looking for a "for in loop", not a "while read loop" .

---

<div class="post-metadata">

**Author:** ![jainitai](https://community.unix.com/letter_avatar/jainitai/32/6_22734026db12803ebb3e059622ac3534.png) [@jainitai](https://community.unix.com/u/jainitai)\
**Post date:** [August 31, 2012, 4:13am UTC](https://community.unix.com/t/while-loop/316014/6 "2012-08-31T04:13:38Z")

</div>

is there any way i can achieve this using while read line example

```nohighlight
file="/path"
while read line
do
  echo $line
done <"$file"

```

I just want to read the file 5 times in destination path

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_22734026db12803ebb3e059622ac3534.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [August 31, 2012, 5:02am UTC](https://community.unix.com/t/while-loop/316014/8 "2012-08-31T05:02:03Z")

</div>

Given the threads you opened in ["Homework and Coursework"](http://www.unix.com/homework-coursework-questions/) i suppose this to be part of your homework assignment. This assumption is further bolstered up by your insistence on using a while-loop even when told other alternative solutions.

Homework is not allowed in these boards and i suggest you open a thread in the homework part for this.

Until proven that my assumption is wrong this thread is closed and too complete answers from others\*) are moderated.

bakunin

\_\_\_\_\_\_\_\_\_\_\_\_  
\*) @pamu: this means yours
