Thank you very much for who can solve this bash script:
"Create a bash procedure createdata that creates collections (directories) that that will have to contain a random number, comprised between 300 and 600, of binary files of dimension comprised between 10 Kbytes and 30 kbytes.
Also the names of the files are random and are composed from 20 chars plus the jpg extension."

Working sample:

createdata /data 20

result:

/data
      tlc01
             collection001
                             ttretdgdfgasjojgvao.jpg
                             grtghgfhysystudbbnh.jpg
                             ...
             ...
             collection019
                             ...

it must create therefore beyond to the directory given (data) the subdirectory tlc01, then the 20 collections (subdirectories of tlc01)

Sorry for my english, hope you understand..
Thanks indeed for who can help me :-)

Have you tried to do it on your own? Or did you just run here hoping that someone would do your homework for you?

yes I tried this:

#!/bin/sh
mkdir $1
number=$2
while $number > 0; do
    mkdir collectionnumber$number
    numbermaxfile4folder=here I have to assign a RANDOM BETWEEN 300 AND 600 (how ???)
    firstpartoffile=$(date) 
    chars=$RANDOM
    echo chars > $firstpartoffile000000.jpg (to fill up a file of an amount of bytes..it should be between 10 and 30..)
done
number=$number-1
cp -r collection$number /data

can anyone help me to solve the full version? This is incomplete and I think also wrong..
Please help I'm totally newbie :(((

...
numbermaxfile4folder=here I have to assign a RANDOM BETWEEN 300 AND 600 (how ???)
...

# start=300
# end=600
# echo $(($RANDOM % ( $end - $start + 1 ) + $start))
401

(to fill up a file of an amount of bytes..it should be between 10 and 30..)

chars=(
    a b c d e f g h i j k l m n o p q r s t u v w x y z \
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
    1 2 3 4 5 6 7 8 9 0
)

function random_string {
    length=${1:-10}
    for ((i = 0; i < length; i++)); do
        echo -n ${chars[ (( ${RANDOM} % ${#chars[@]} )) ]}
    done
    echo
}

random_string 20

thank you very much ghostdog74!

My code then is this:

#!/bin/sh
mkdir data
cd $data
number=$2
while $number > 0; do
    mkdir collection$number
    cd $collection$number
    numberoffilemax=$(($RANDOM % ( $600 - $300 + 1 ) + $300))
    while $numberoffilemax > 0; do
        firstpartoffile=$(date) 
        chars=random_string()
        echo chars > $firstpartoffile000000.jpg
        numberoffilemax=$numerofilemax-1
    done
    cd .. 
    number=$number-1
done

chars=(
    a b c d e f g h i j k l m n o p q r s t u v w x y z \
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
    1 2 3 4 5 6 7 8 9 0
)

function random_string {
    length=${1:-10}
    for ((i = 0; i < length; i++)); do
        echo -n ${chars[ (( ${RANDOM} % ${#chars[@]} )) ]}
    done
    echo
}

Thank you again!

# start=300
# end=600
# echo $(($RANDOM % ( $end - $start + 1 ) + $start))
401

tryrandom.bash:

#!/bin/bash
start=300
end=600
echo $(($RANDOM % ( $end - $start + 1 ) + $start))

tryrandom.bash: line 2: 19345 % -1 + : syntax error: operand expected (error token is " ")


why?

chars=(
    a b c d e f g h i j k l m n o p q r s t u v w x y z \
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
    1 2 3 4 5 6 7 8 9 0
)

function random_string {
    length=${1:-10}
    for ((i = 0; i < length; i++)); do
        echo -n ${chars[ (( ${RANDOM} % ${#chars[@]} )) ]}
    done
    echo
}

random_string 20 

how to assign the value of the function in a shell script?

thanks!

tryrandom.bash:

#!/bin/bash
start=300
end=600
echo $(($RANDOM % ( $end - $start + 1 ) + $start))

tryrandom.bash: line 2: 19345 % -1 + : syntax error: operand expected (error token is " ")


why?

try typing the commands on the shell prompt and see.

how to assign the value of the function in a shell script?

thanks!

put a return statement in the functoin. then call it and use $? to catch the value

# function test() { return 1; }
# var=$(test)
# echo $?
1

try typing the commands on the shell prompt and see.

yes typing it works but if I execute the shell script "bash myscript.bash" give me that error!
What to do?

yes typing it works but if I execute the shell script "bash myscript.bash" give me that error!
What to do?

solved by myself, it was a end-line character problem..:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.