943,913 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jun 1st, 2007
0

HELP! Who can solve this bash script?

Expand Post »
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:

Shell Scripting Syntax (Toggle Plain Text)
  1. createdata /data 20
  2.  
  3. result:
  4.  
  5. /data
  6. tlc01
  7. collection001
  8. ttretdgdfgasjojgvao.jpg
  9. grtghgfhysystudbbnh.jpg
  10. ...
  11. ...
  12. collection019
  13. ...
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 :-)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexxxprog is offline Offline
7 posts
since Jun 2007
Jun 1st, 2007
0

Re: HELP! Who can solve this bash script?

Have you tried to do it on your own? Or did you just run here hoping that someone would do your homework for you?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 1st, 2007
0

Re: HELP! Who can solve this bash script?

yes I tried this:
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/sh
  2. mkdir $1
  3. number=$2
  4. while $number > 0; do
  5. mkdir collectionnumber$number
  6. numbermaxfile4folder=here I have to assign a RANDOM BETWEEN 300 AND 600 (how ???)
  7. firstpartoffile=$(date)
  8. chars=$RANDOM
  9. echo chars > $firstpartoffile000000.jpg (to fill up a file of an amount of bytes..it should be between 10 and 30..)
  10. done
  11. number=$number-1
  12. 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 ((
Last edited by alexxxprog; Jun 1st, 2007 at 5:32 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexxxprog is offline Offline
7 posts
since Jun 2007
Jun 1st, 2007
0

Re: HELP! Who can solve this bash script?

Click to Expand / Collapse  Quote originally posted by alexxxprog ...
...
numbermaxfile4folder=here I have to assign a RANDOM BETWEEN 300 AND 600 (how ???)
...
Shell Scripting Syntax (Toggle Plain Text)
  1. # start=300
  2. # end=600
  3. # echo $(($RANDOM % ( $end - $start + 1 ) + $start))
  4. 401
Quote ...
(to fill up a file of an amount of bytes..it should be between 10 and 30..)
Shell Scripting Syntax (Toggle Plain Text)
  1. chars=(
  2. 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 \
  3. 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 \
  4. 1 2 3 4 5 6 7 8 9 0
  5. )
  6.  
  7. function random_string {
  8. length=${1:-10}
  9. for ((i = 0; i < length; i++)); do
  10. echo -n ${chars[ (( ${RANDOM} % ${#chars[@]} )) ]}
  11. done
  12. echo
  13. }
  14.  
  15. random_string 20
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Jun 2nd, 2007
0

Re: HELP! Who can solve this bash script?

thank you very much ghostdog74!

My code then is this:
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/sh
  2. mkdir data
  3. cd $data
  4. number=$2
  5. while $number > 0; do
  6. mkdir collection$number
  7. cd $collection$number
  8. numberoffilemax=$(($RANDOM % ( $600 - $300 + 1 ) + $300))
  9. while $numberoffilemax > 0; do
  10. firstpartoffile=$(date)
  11. chars=random_string()
  12. echo chars > $firstpartoffile000000.jpg
  13. numberoffilemax=$numerofilemax-1
  14. done
  15. cd ..
  16. number=$number-1
  17. done
  18.  
  19. chars=(
  20. 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 \
  21. 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 \
  22. 1 2 3 4 5 6 7 8 9 0
  23. )
  24.  
  25. function random_string {
  26. length=${1:-10}
  27. for ((i = 0; i < length; i++)); do
  28. echo -n ${chars[ (( ${RANDOM} % ${#chars[@]} )) ]}
  29. done
  30. echo
  31. }

Thank you again!
Last edited by alexxxprog; Jun 2nd, 2007 at 6:42 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexxxprog is offline Offline
7 posts
since Jun 2007
Jun 4th, 2007
0

Re: HELP! Who can solve this bash script?

Click to Expand / Collapse  Quote originally posted by ghostdog74 ...
Shell Scripting Syntax (Toggle Plain Text)
  1. # start=300
  2. # end=600
  3. # echo $(($RANDOM % ( $end - $start + 1 ) + $start))
  4. 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexxxprog is offline Offline
7 posts
since Jun 2007
Jun 4th, 2007
0

Re: HELP! Who can solve this bash script?

Click to Expand / Collapse  Quote originally posted by ghostdog74 ...
[code]
Shell Scripting Syntax (Toggle Plain Text)
  1. chars=(
  2. 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 \
  3. 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 \
  4. 1 2 3 4 5 6 7 8 9 0
  5. )
  6.  
  7. function random_string {
  8. length=${1:-10}
  9. for ((i = 0; i < length; i++)); do
  10. echo -n ${chars[ (( ${RANDOM} % ${#chars[@]} )) ]}
  11. done
  12. echo
  13. }
  14.  
  15. random_string 20
how to assign the value of the function in a shell script?

thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexxxprog is offline Offline
7 posts
since Jun 2007
Jun 4th, 2007
0

Re: HELP! Who can solve this bash script?

Click to Expand / Collapse  Quote originally posted by alexxxprog ...
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.
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Jun 4th, 2007
0

Re: HELP! Who can solve this bash script?

Click to Expand / Collapse  Quote originally posted by alexxxprog ...
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
Shell Scripting Syntax (Toggle Plain Text)
  1.  
  2. # function test() { return 1; }
  3. # var=$(test)
  4. # echo $?
  5. 1
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Jun 6th, 2007
0

Re: HELP! Who can solve this bash script?

Click to Expand / Collapse  Quote originally posted by ghostdog74 ...
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?
Last edited by alexxxprog; Jun 6th, 2007 at 6:45 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexxxprog is offline Offline
7 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: What does @ mean???
Next Thread in Shell Scripting Forum Timeline: Fedora Resolution





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC