HELP! Who can solve this bash script?

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2007
Posts: 7
Reputation: alexxxprog is an unknown quantity at this point 
Solved Threads: 0
alexxxprog alexxxprog is offline Offline
Newbie Poster

HELP! Who can solve this bash script?

 
0
  #1
Jun 1st, 2007
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 :-)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,858
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: HELP! Who can solve this bash script?

 
0
  #2
Jun 1st, 2007
Have you tried to do it on your own? Or did you just run here hoping that someone would do your homework for you?
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 7
Reputation: alexxxprog is an unknown quantity at this point 
Solved Threads: 0
alexxxprog alexxxprog is offline Offline
Newbie Poster

Re: HELP! Who can solve this bash script?

 
0
  #3
Jun 1st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 151
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: HELP! Who can solve this bash script?

 
0
  #4
Jun 1st, 2007
Originally Posted by alexxxprog View Post
...
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
(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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 7
Reputation: alexxxprog is an unknown quantity at this point 
Solved Threads: 0
alexxxprog alexxxprog is offline Offline
Newbie Poster

Re: HELP! Who can solve this bash script?

 
0
  #5
Jun 2nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 7
Reputation: alexxxprog is an unknown quantity at this point 
Solved Threads: 0
alexxxprog alexxxprog is offline Offline
Newbie Poster

Re: HELP! Who can solve this bash script?

 
0
  #6
Jun 4th, 2007
Originally Posted by ghostdog74 View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 7
Reputation: alexxxprog is an unknown quantity at this point 
Solved Threads: 0
alexxxprog alexxxprog is offline Offline
Newbie Poster

Re: HELP! Who can solve this bash script?

 
0
  #7
Jun 4th, 2007
Originally Posted by ghostdog74 View Post
[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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 151
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: HELP! Who can solve this bash script?

 
0
  #8
Jun 4th, 2007
Originally Posted by alexxxprog View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 151
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: HELP! Who can solve this bash script?

 
0
  #9
Jun 4th, 2007
Originally Posted by alexxxprog View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 7
Reputation: alexxxprog is an unknown quantity at this point 
Solved Threads: 0
alexxxprog alexxxprog is offline Offline
Newbie Poster

Re: HELP! Who can solve this bash script?

 
0
  #10
Jun 6th, 2007
Originally Posted by ghostdog74 View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum


Views: 3977 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC