Small project, big headache. 7 segment display in bash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zacariaz
    New Member
    • Jun 2015
    • 9

    Small project, big headache. 7 segment display in bash

    I've never been very good at bash, but usually I get things done on the rare occasion that I need to. This challenge however, is beyond me.

    Code:
    #!/bin/bash
    
    farr0=("#####" "    #" "#####" "#####" "#   #" "#####" "#    " "#####" "#####" "#####" "    #" "     ")
    farr1=("#   #" "    #" "    #" "    #" "#   #" "#    " "#    " "    #" "#   #" "#   #" "   # " "  #  ")
    farr2=("#   #" "    #" "#####" "#####" "#####" "#####" "#####" "    #" "#####" "#####" "  #  " "     ")
    farr3=("#   #" "    #" "#    " "    #" "    #" "    #" "#   #" "    #" "#   #" "    #" " #   " "  #  ")
    farr4=("#####" "    #" "#####" "#####" "    #" "#####" "#####" "    #" "#####" "    #" "#    " "     ")
    
    function print_datetime {
     	datetime=`date '+%Y%m%d%H%M%S'`
    
     	for i in {0..4}
            do
                # What I'd like: (example for year only)
                # echo -n ${farr{$i}[$datetime[0]]}
                # echo -n ${farr{$i}[$datetime[1]]}
                # echo -n ${farr{$i}[$datetime[2]]}
                # echo -n ${farr{$i}[$datetime[3]]}
                
        done
    }
    print_datetime
    In this case the desired output would be:
    Code:
    ##### #####     # #####
        # #   #     # #    
    ##### #   #     # #####
    #     #   #     #     #
    ##### #####     # #####
    I have tried a number of approached, but none of them have worked out, all of them have been overly complex, and think the reason basically is that I haven't the slightest clue what I'm doing, and am taking the wrong approach.

    Don't get me wrong, I can do this, and have done so using both awk and C, so regardless I have a solution, but I would really like to know where I'm going wrong.

    I know I haven't provided much in term of example code, but the reason is simply that nothing really works. It's not just a matter of syntax error or similar, it's me doing something fundamentally wrong.


    Anyway, if you have something to add, I'd like to hear it. I know it isn't really a question as such, but here goes anyway.


    Best regards.
  • hpmachining
    New Member
    • Oct 2015
    • 15

    #2
    I know this is an old question but I wanted to take a shot at it. Here is what I came up with:
    Code:
    #!/bin/bash
    farr0=("##### " "    # " "##### " "##### " "#   # " "##### " "#     " "##### " "##### " "##### " "    # " "     ")
    farr1=("#   # " "    # " "    # " "    # " "#   # " "#     " "#     " "    # " "#   # " "#   # " "   #  " "  #  ")
    farr2=("#   # " "    # " "##### " "##### " "##### " "##### " "##### " "    # " "##### " "##### " "  #   " "     ")
    farr3=("#   # " "    # " "#     " "    # " "    # " "    # " "#   # " "    # " "#   # " "    # " " #    " "  #  ")
    farr4=("##### " "    # " "##### " "##### " "    # " "##### " "##### " "    # " "##### " "    # " "#     " "     ")
    
    function print_datetime {
         datetime=`date '+%Y%m%d%H%M%S'`
    
         for i in {0..4}
            do
                # What I'd like: (example for year only)
                tmp=farr${i}'['${datetime:0:1}']'
                echo -n "${!tmp}"
                tmp=farr${i}'['${datetime:1:1}']'
                echo -n "${!tmp}"
                tmp=farr${i}'['${datetime:2:1}']'
                echo -n "${!tmp}"
                tmp=farr${i}'['${datetime:3:1}']'
                echo "${!tmp}"
            done
    }
    print_datetime
    I couldn't figure out how to do it without a temporary variable. Here is the output:
    Code:
    ##### #####     # #
        # #   #     # #
    ##### #   #     # #####
    #     #   #     # #   #
    ##### #####     # #####

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      Zacariaz,

      Take a look at the "eval" command built in to the bash shell.

      Secondly, you left out an enclosing pair of braces on the "datetime" array

      try:
      Code:
      eval echo -n "\${farr$i[${datetime:0:1}]}"
      to assign to an intermediate variable, use:
      Code:
      eval intVariable="\${farr$i[${datetime:0:1}]}"
      Luck!
      Oralloy
      Last edited by Oralloy; Sep 1 '16, 05:42 PM. Reason: cleaned up, even more.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Zacariaz,

        I do hope that this isn't one of your homework problems. Unfortunately our policy is that we don't do peoples' homework for them, although we will give advice to help, so you learn how to do things well.

        I think that use of the bash "eval" command is sufficiently obscure that you haven't met it yet.

        If I just stepped on my own toes, please forgive.

        Cheers!
        Oralloy

        Comment

        Working...