You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. function generate(){
  3. # NAME
  4. # generate - generate a matrix of a specified size
  5. # SYNOPSIS
  6. # generate ROWS COLS RANGE
  7. # DESCRIPTION
  8. # Outputs a matrix with ROWS and COLS as dimensions. If RANGE is an integer, the entries
  9. # of the matrix will be random integers in (-RANGE,+RANGE). If RANGE is a string starting
  10. # with 'Z' or 'z', the matrix will be populated with zeroes. If RANGE is a string starting
  11. # with 'O' or 'o', the matrix will be populated with ones. If RANGE is a string starting
  12. # with 'D' or 'd', the matrix will be a diagonal matrix with ones along the diagonal entries.
  13. # AUTHOR
  14. # Written by Ryan Gambord (gambordr@oregonstate.edu)
  15. [ $# -ne 3 ] && echo "invalid number of arguments" >&2 # This is equivalent to using an if statement.
  16. # The right side of the && operator will not
  17. # be evaluated unless the left side is true.
  18. for arg in $1 $2
  19. do
  20. [[ $arg =~ [^0-9]+ ]] && echo "argument '$arg' is not an integer" >&2
  21. done
  22. y=0
  23. while [ $y -lt $1 ]
  24. do
  25. x=0
  26. ((y++))
  27. while [ $x -lt $2 ]
  28. do
  29. ((x++))
  30. case $3 in
  31. [oO]*) echo -n 1;;
  32. [zZ]*) echo -n 0;;
  33. [dD]*) [ $x -eq $y ] && echo -n 1 || echo -n 0;;
  34. *) if [[ "$3" =~ [^0-9]+ ]]
  35. then
  36. echo "invalid RANGE" >&2
  37. exit 1
  38. else
  39. echo -n $(( (RANDOM - 32767 / 2) % $3 ))
  40. fi;;
  41. esac
  42. if [ $x -ne $2 ]
  43. then
  44. echo -ne "\t"
  45. else
  46. echo
  47. fi
  48. done
  49. done
  50. }
  51. generate $1 $2 $3