Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.85 KB | None | 0 0
  1. #!/bin/bash
  2. ################# this script creates an outfile with "val" number of lines of   digits with the columns aligned . val can be set to what you want
  3. ################# but formating only works to 999 so far , but its eay to extend,
  4. #################  WARNING: you will still need to edit the outfile to add or remove the final last digits to the desired final number
  5. #################        since this script cant perfectly write  to 64,128,256,512... and such
  6. ################# mike , undergrad, friday,4th,2011.
  7. ################# hope it makes your work easier!!
  8.  
  9.  
  10. if [ $# -eq 0 ]
  11. then
  12.    echo " "  
  13. fi
  14.  
  15. myarray=(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
  16. count=0
  17. n=0
  18. val=25                  ######### change this to the number of lines you want ie if 1..50 , val~3 since there are
  19.                         ######### roughly five  lines of digits
  20.  
  21. echo -n " even or odd ? "
  22. read -e TYPE
  23.  if [ "$TYPE" =  "even" ]
  24.   then let  type=2
  25.   else let  type=1
  26.  fi
  27.  
  28. tens=99
  29. hundreds=999            ######### you would need to add other variables like this to extend column alignment to greater than 999
  30.  
  31. i=0
  32. if [ "$type" -eq 2 ]; then    ###########      for the even list of digits
  33.  
  34.  echo >> outfile$type
  35.  for i in ${myarray[@]}
  36.            do
  37.         if (( ("$i" % 2) ==  "0" )); then
  38.            sed '$s/$/ '$i'/' -i outfile$type
  39.           # echo $i
  40.          if   [ "${myarray[$i]}" -gt  "10" ] ; then                                                                                                                    
  41.                sed '$s/$/  /' -i outfile$type                                                                                                                          
  42.            else   sed '$s/$/   /' -i outfile$type   ######this adjusts the space between the ones digits , space is added or removed bbetween the forward slashes      
  43.            fi                                                                                                                                                          
  44.         fi                                                                                                                                                            
  45.            done                                                                                                                                                        
  46.   echo >> outfile$type                                                                                                                                                
  47.   while [ $n -le $val ]                                                                                                                                                
  48.    do                                                                                                                                                                  
  49.         for i in ${myarray[@]}                                                                                                                                        
  50.            do                                                                                                                                                          
  51.             myarray[$count]=`expr $i + 20`                                                                                                                            
  52.             ((count++))
  53.            done
  54.          count=0
  55.  
  56.  
  57.        for i in ${myarray[@]}
  58.            do
  59.          if (( ("$i" % 2) == "0" )) ; then          ################        check if its odd or even
  60.            sed '$s/$/ '$i'/' -i outfile$type
  61.               if [ "${myarray[0]}" -le "$tens" ] ; then
  62.                   sed '$s/$/  /' -i outfile$type         ######### this adjusts space between the tens digits to be aligned with the ones digits
  63.    
  64.                 elif [ "${myarray[0]}" -le "$hundreds" ]; then
  65.                  sed '$s/$/ /' -i outfile$type           ######### this adjusts the space between the hundreds digits to be aligned with the ones & tens
  66.  
  67.                          ######## you would add such elifs from here to extend the column alignment beyond 999
  68.                else                                  
  69.                  sed '$s/$/ /' -i outfile$type            ######## adjusts space after hundreds
  70.               fi
  71.          fi
  72.            done
  73.            echo >> outfile$type
  74.  
  75.          let n++
  76.   done
  77.  
  78.  
  79.  
  80. else                                          ############## for odd numbers
  81.  
  82. echo >> outfile$type
  83.  for i in ${myarray[@]}
  84.            do
  85.         if (( ("$i" % 2) !=  "0" )); then
  86.            sed '$s/$/ '$i'/' -i outfile$type
  87.            if  [ "${myarray[$i]}" -gt  "10" ]; then
  88.                sed '$s/$/  /' -i outfile$type
  89.            else   sed '$s/$/   /' -i outfile$type   ######this adjusts the space between the ones digits , space is added or removed bbetween the forward slashes
  90.            fi
  91.         fi
  92.            done
  93.  
  94.   echo >> outfile$type
  95.  
  96.   while [ $n -le $val ]
  97.    do
  98.  
  99.         for i in ${myarray[@]}
  100.            do
  101.             myarray[$count]=`expr $i + 20`
  102.             ((count++))
  103.            done
  104.          count=0
  105.  
  106.  
  107.        for i in ${myarray[@]}
  108.            do
  109.          if (( ("$i" % 2) != "0" )) ; then          ################        check if its odd or even
  110.            sed '$s/$/ '$i'/' -i outfile$type
  111.               if [ "${myarray[0]}" -le "$tens" ] ; then
  112.                   sed '$s/$/  /' -i outfile$type         ######### this adjusts space between the tens digits to be aligned with the ones digits
  113.  
  114.                 elif [ "${myarray[0]}" -le "$hundreds" ]; then
  115.                  sed '$s/$/ /' -i outfile$type           ######### this adjusts the space between the hundreds digits to be aligned with the ones & tens
  116.  
  117.                          ######## you would add such elifs from here to extend the column alignment beyond 999
  118.                else
  119.                  sed '$s/$/ /' -i outfile$type            ######## adjusts space after hundreds
  120.               fi
  121.          fi
  122.            done
  123.            echo >> outfile$type
  124.  
  125.          let n++
  126.   done
  127. fi
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. this is the error message thought the script does what its intended to do:
  138.  
  139.  
  140.  ./Onetofive: line 40: [: : integer expression expected
  141.  
  142. funny thing is that it only gives the error message when i choose the "even"
  143. choice , but still does what its intended, the "odd" does not give any error message
  144.  
  145. i'v highlighted line 40
  146.  
  147.  
  148. here is what it gives with "set -x"
  149.  
  150. + ./Onetofive
  151.  
  152. even or odd ? even
  153. ./Onetofive: line 40: [: : integer expression expected
  154. ++ echo -ne '\033]0;mike@localhost:~/work/worksub/scripts'
  155.  
  156.  
  157.  
  158. here is part of the debug output:
  159. ...
  160. + for i in '${myarray[@]}'
  161. + ((  (15 % 2) ==  0  ))
  162. + for i in '${myarray[@]}'
  163. + ((  (16 % 2) ==  0  ))
  164. + sed '$s/$/ 16/' -i outfile2
  165. + '[' 17 -gt 10 ']'
  166. + sed '$s/$/  /' -i outfile2
  167. + for i in '${myarray[@]}'
  168. + ((  (17 % 2) ==  0  ))
  169. + for i in '${myarray[@]}'
  170. + ((  (18 % 2) ==  0  ))
  171. + sed '$s/$/ 18/' -i outfile2
  172. + '[' 19 -gt 10 ']'
  173. + sed '$s/$/  /' -i outfile2
  174. + for i in '${myarray[@]}'
  175. + ((  (19 % 2) ==  0  ))
  176. + for i in '${myarray[@]}'
  177. + ((  (20 % 2) ==  0  ))
  178. + sed '$s/$/ 20/' -i outfile2
  179. + '[' '' -gt 10 ']'
  180. Onetofive: line 40: [: : integer expression expected
  181. + sed '$s/$/   /' -i outfile2
  182. + echo
  183. + '[' 0 -le 5 ']'
  184. ...
  185.  
  186.  
  187.  
  188. everything seems to go ok until ${myarray[@]} seems to evaluate to an empty ''  how does does that happen? and why just for "even" the array just has digits 1..20
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. 2    4    6    8    10   12   14   16   18   20  
  196. 22   24   26   28   30   32   34   36   38   40  
  197. 42   44   46   48   50   52   54   56   58   60  
  198. 62   64   66   68   70   72   74   76   78   80  
  199. 82   84   86   88   90   92   94   96   98   100  
  200. 102  104  106  108  110  112  114  116  118  120
  201. 122  124  126  128  130  132  134  136  138  140
  202. 142  144  146  148  150  152  154  156  158  160
  203. 162  164  166  168  170  172  174  176  178  180
  204. 182  184  186  188  190  192  194  196  198  200
  205. 202  204  206  208  210  212  214  216  218  220
  206. 222  224  226  228  230  232  234  236  238  240
  207. 242  244  246  248  250  252  254  256  258  260
  208. 262  264  266  268  270  272  274  276  278  280
  209. 282  284  286  288  290  292  294  296  298  300
  210. 302  304  306  308  310  312  314  316  318  320
  211. 322  324  326  328  330  332  334  336  338  340
  212. 342  344  346  348  350  352  354  356  358  360
  213. 362  364  366  368  370  372  374  376  378  380
  214. 382  384  386  388  390  392  394  396  398  400
  215. 402  404  406  408  410  412  414  416  418  420
  216. 422  424  426  428  430  432  434  436  438  440
  217. 442  444  446  448  450  452  454  456  458  460
  218. 462  464  466  468  470  472  474  476  478  480
  219. 482  484  486  488  490  492  494  496  498  500
  220. 502  504  506  508  510  512  514  516  518  520
  221. 522  524  526  528  530  532  534  536  538  540
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement