Copy files in even order first file into second coderinme

Copy files in even order first file into second

A shell script receives even number of filenames. Suppose four
filenames are supplied then the first file should get copied into second
file, the third file should get copied into fourth file, and so on. If odd
number of filenames are supplied then no copying should take place and
an error message should be displayed.It means we have to copy files in even order.

For example below script is saved as
now in terminal if we would provide 3 files then output will be Argument length is an odd number.Operation incomplete.Exiting
if it’s even then operation will be completed.
e.g file32 file34 file41 file22 filex5 filex3
after execution of q31, file32 will be copied into file 34 , file 41 into file 22 etc.

if [ $# -lt 2 ]; then
echo "Usage: $0 file1 file2 [file3 ...]"
exit 1
if (($#%2==0)); then
for((i=0;i<$#;i=i+2)); do
let j=$i+1
cat ${arr[$i]} >> ${arr[$j]}
echo "Contents of file "${arr[$i]} "is copied into file "${arr[$j]}
echo "Argument length is an odd number.Operation incomplete.Exiting"

For more programs in shell, visit our Shell Bash Archives
copy files in even order

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without prior written permission of website admin, except in case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me

Leave a reply:

Your email address will not be published.