Cat file several times without a loop

This is NOT a domestic question, this is a question from the old exam, so anyone who answers will not contribute to academic dishonesty. For those who are still skeptical, I'm just looking for which team I could use to do this.

You have a file named one_mb exactly 1 megabyte in size. You want to create a 128 megabyte file from it. Please write a shell script to do this with a maximum of 9 lines and no loops if instructions, recursion, or any other logical control structure. Each command, including parameters, must be less than 100 characters long.

I started exploring xarg, but couldn't find a good way to use it to achieve this.

+6
source share
6 answers

Assuming bash, you can use a one-line extension hack:

cat one_mb{,}{,}{,}{,}{,}{,}{,} > 128_mb 
+12
source

Not sure if this counts, but it occurred to me:

 seq 1 128 | xargs -Inone cat one_mb >> 128_mb 

No loops were used, only pipe and xargs .

+6
source

A big hint that this could be "no more than 9 lines." Since 2 ^ 7 = 128, you just need to double the file size 7 times:

 cat one_mb one_mb > two_mb cat two_mb two_mb > four_mb ... cat 64_mb 64_mb > 128_mb 
+4
source

With 100 characters per command, you can slightly reduce it:

 cat one_mb one_mb one_mb one_mb one_mb one_mb one_mb one_mb >mb8 cat mb8 mb8 mb8 mb8 >mb32 cat mb32 mb32 mb32 mb32 >mb128 rm -f mb8 mb32 
+4
source
 dd oflag=append conv=notrunc if=/dev/zero of=one_mb bs=1MB count=127 

This will save the contents of the file and add a bunch of “null” entries to make it 128 MB. At

 ls -ltrh one_mb 

to check if it is really 128 MB, otherwise you may need to change the parameter "count = 127".

+2
source

I think this will be enough with the limitations that you have. You can define a function that takes an integer as a parameter. If it is greater than 0, the cat file also calls the same function again, but with a reduced parameter.

Then you just call the function with the value you need, and you're done.

Good recursion :)

(Sorry, too lazy for coding, and there are many other working answers, just wanted to avoid forgetting recursion :))

-1
source

All Articles