0

In Linux in Bash, there is a script, a part of the script is this

while true ; do
    echo
    awk -v x=$(<"$TEMPDIR"size_container_in_byte) -v n=$(<"$TEMPDIR"parts) 'BEGIN{srand(); while (--n) {$++NF = int(rand() * x / n); x -= $NF}; $++NF = x; print}' > "$TEMPDIR"numbers_in_one_line

    cat "$TEMPDIR"numbers_in_one_line |
                          tr " " "\n" |
                                  rev |
              sed -E "s/(...)/\\1./g" |
                       sed "s/\.$//g" |
                                  rev | xargs printf '| %15s  |\n'  > "$TEMPDIR"numbers_line_by_line_right_dots

    cat "$TEMPDIR"numbers_line_by_line_right_dots
    echo
    printf 'go on with y/Y - any other key for a new suggestion - strg+c for exit'
    echo
    echo
    read answer
    if [ "$answer" != "${answer#[yY]}" ] ;then
        read -a numbers <"$TEMPDIR"numbers_in_one_line
        j=0
        for i in "${numbers[@]}"; do fallocate -l "$i" ""$TEMPDIR"files/file$((++j))" ; done
        break
    else
        echo "new suggestion :"
    fi
done

it creates with fallocate some files, all with different random size given by x and n parts.

how can that be change to create splittet files?

for example, there will be created files like this

 3748M file1
25997M file2
  841M file3
  247M file4

How to change that to create with fallocate

file1 with 3x 1000M and the rest as file1.1, file1.2, file1.3 and file1.4 with the rest

file2 with 25x 1000M (and the rest) as file2.01, file2.02 .... file2.26

but file3 with 8x 100M (and the rest) in file3.9

and file4 the same scheme...

is this done, how can file1.1 .... file1.4 join with dmsetup linear together to file1 ? file2.* and all otheres by the same way? but its random there can be more than file4

-

for joining files with dmsetup i have this script

#!/bin/sh

usage() {
    echo "Usage: ${0##*/} <target name> <component...>" >&2
    exit 64 # EX_USAGE
}

if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "-?" ] || [ "$1" = "--help" ]
then
    usage
fi

if [ -e "/dev/mapper/$1" ]
then
    echo "ERROR: target $1 already exists." >&2
    exit 73 # EX_CANTCREAT
fi
MAPNAME="$1"
shift

DMMAP=$(mktemp)
trap "rm -f $DMMAP" EXIT

sum=0
while [ "$1" != "" ]
do
    if [ ! -e "$1" ]
    then
        echo "ERROR: $1 does not exist"
        exit 66 # EX_NOINPUT
    fi
    if [ ! -b "$1" ]
    then
        echo "ERROR: $1 is not a block device"
        exit 69 # EX_UNAVAILABLE
    fi 
    srcN="$1"
    sizeN=$(blockdev --getsz "$srcN")
    echo "$sum $sizeN linear $srcN 0" >>$DMMAP
    sum=$(expr $sum + $sizeN)
    shift
done

dmsetup create $MAPNAME < $DMMAP

but how to put this script into the main script?

:edit

i will not realy split a file, i will create files they are together form a place to copy in there a file, so becouse i am talking about "spliting", but i mean preparing.

do you mean 3748M file1 25997M file2 .... ? the M stands for Megabyte.

in my script it beginns all with the size of the file they i want to copy into the joined files.

i do start the script give along a real existing file they i would copy in.

the script check the size of the file(s), than comes a part where i enter how many unevenly sized parts i want to create.

the script creates a list of numbers, these numbers together are the size of the file they i want to copy in.

the list is stored in "$TEMPDIR"numbers_in_one_line.

these list i want to use to check if over 1000M create 1000M parts, and if the size under 1000M than create 100M parts.

these 1000M and 100M parts should are named file*.1, file*.2, ...

* = is for how many unevenly sized parts i have entered in the script.

in this example here, a new one, i have start the script with along with a file with the size 30833014492. in the script i do enter 4 to become 4 random unevenly sized parts. the script generates the file "$TEMPDIR"numbers_in_one_line in this file is one line with these numbers 24997117901 4848337945 541113465 446445181 (together = 30833014492) now i am looking for a way to create, with fallocate, files based with the numbers from "$TEMPDIR"numbers_in_one_line` , but if the number over 1000 megabyte, i want to create 1000 megabyte parts, and if the number under 1000M i want to create 100M parts.

if this done, i am looking for a way insterd the dmjoin script into my script to join file1.1, file1.2.... to file1. file2.1, file2.2.... to file2 and so forth.

dont know if it importand for better understanding, i want to have two layers. one with unevenly sized parts, and the other, the second one with 1000M or 100M parts. but first i must start with creating the 100M/1000M parts and with these can be the unevenly sized parts formed.

2
  • This is really confusing. Can you perhaps give an example input and desired output? Your script is hard to understand when we don't know the context or what the various variable hold. What file do you want to split? Where would that come from? What are these M values? File sizes? Number of lines? How are the two scripts related? Can you simplify this to only the core request and focus on that? Commented Apr 19 at 16:35
  • i have edit my post, hope you understand my question now better, sorry i do sometimes not found the rright words in my head for my problems.... Commented Apr 19 at 23:57

1 Answer 1

0

Your "fallocate loop" could be this, calling out to split:

for i in "${numbers[@]}"; do 
    outfile="$TEMPDIRfiles/file$((++j))" 
    if fallocate -l "$i" "$outfile"; then
        split_size=1000M
        if (( $(stat -c '%s' "$outfile") < 10**9 )); then
            split_size=100M
        fi
        split --bytes="$split_size" --numeric-suffixes=1 "$outfile" "${outfile}."
    fi
done

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.