954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

HELP! Who can solve this bash script?

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 :-)

alexxxprog
Newbie Poster
7 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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 :(((

alexxxprog
Newbie Poster
7 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 
... 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
ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

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!

alexxxprog
Newbie Poster
7 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 
# 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?

alexxxprog
Newbie Poster
7 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

[code]

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!

alexxxprog
Newbie Poster
7 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

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.

ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

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
ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 
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?

alexxxprog
Newbie Poster
7 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 
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..:)

alexxxprog
Newbie Poster
7 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You