Oxaric’s Blog

A compendium of amazing things…

Posts Tagged ‘command’

ASCII LCD Number Printer

Posted by oxaric on November 24, 2008

I took some time and hacked out an ascii lcd number printer. It is written in Ruby and contains no comments. The number you want printed must be passed as the first parameter. The size of the output can be changed by passing a second number. The default size is 2.

How to run:

~/test> ruby lcd.rb 0123456789 5


Click to directly download lcd.rb

# filename 'lcd.rb'
# Use: takes a number as the first parameter, 
#      a size value > 0 as the second parameter
# Output: Displays the number as ascii
# By: Louis Casillas, e-mail: oxaric@gmail.com

zero = [" - ",
        "| |",
        "   ",
        "| |",
        " - "]

one =  ["   ", 
        " | ",
        "   ",
        " | ",
        "   "]

two =  [" - ", 
        "  |",
        " - ",
        "|  ",
        " - "]

three = [" - ", 
         "  |",
         " - ",
         "  |",
         " - "]

four = ["   ", 
        "| |",
        " - ",
        "  |",
        "   "]

five = [" - ", 
        "|  ",
        " - ",
        "  |",
        " - "]

six =  [" - ", 
        "|  ",
        " - ",
        "| |",
        " - "]

seven = [" - ", 
         "  |",
         "   ",
         "  |",
         "   "]

eight = [" - ", 
         "| |",
         " - ",
         "| |",
         " - "]

nine = [" - ", 
        "| |",
        " - ",
        "  |",
        " - "]

numbers = ARGV[0].to_s
size = ARGV[1].to_i

output = ""
#1 or 3
for levels in (0...5)
   runs = 0
   
   if (size > 1) && (levels == 1) || (levels == 3)
      runs_for_this_level = size
   else
      runs_for_this_level = 1
   end

   while runs < runs_for_this_level
      for i in (0...numbers.size)
         case numbers[i, 1]
      
            when "0"
               line = zero[levels]
            when "1"
               line = one[levels]
            when "2"
               line = two[levels]
            when "3"
               line = three[levels]
            when "4"
               line = four[levels]
            when "5"
               line = five[levels]
            when "6"
               line = six[levels]
            when "7"
               line = seven[levels]
            when "8"
               line = eight[levels]
            when "9"
               line = nine[levels]
         end

         if (size > 1)
            if (line[1,1] == "-")
            
                  line = line[0,2]

                  for j in (1...size)
                     line += "-"
                  end
                  line += " "
            else
               if (line[0,1] == "|") || (line[2,1] == "|")
               
                  temp = line[2,1]
                  line = line[0,1]

                  for j in (1..size)
                     line += " "
                  end
                  if (temp == "|")      
                     line += "|"
                  else
                     line += " "
                  end
               else
                  if (line[1,1] == "|")
                     temp = (size / 2.0).ceil
                     
                     line = ""

                     for j in (0..temp)
                        line += " "
                     end
         
                     line += "|"
                     
                     if size == 3
                        temp = temp - 2
                     else
                        if size == 2
                           temp = 0

                        else
                           if ((temp % 2) == 0)
                              temp = temp - 1
                           else
                              temp = temp - 2
                           end
                        end
                     end

                     for j in (0..temp)
                        line += " "
                     end
                                       
                  else
                     for j in (1...size)
                        line += " "
                     end
                  end
               end

            end
            for k in (1...size)
               line += " "
            end
      end
   
         output += line

      end
      output += "\n"
      runs = runs + 1
   end
end
puts output

Posted in Programming, Ruby | Tagged: , , , , , , , | Leave a Comment »

Fun With BASH Piping!

Posted by oxaric on November 24, 2008

You may or may not be familiar with piping but if you use or plan to use a Linux machine you should definitely look into it.


Piping allows a command to send it’s output to another command. If you are familiar with programming you could think of this situation as calling a function, getting the output, and sending it directly to another function. Below are examples of two different forms of piping.


These are the commands I’ll use in the examples:


ls – lists all of the files in the current directory, Windows users might know this as the dir command


find – when used with no parameters it will list every file inside the current directory including files inside of other directories


rm – deletes a file, Windows users might know this is as the del command


grep – searches a string looking for a given string or pattern





Piping Example



~/test> ls | grep a.out


What happens:

First, the ls command will find and try to display all the files in the current directory. However, the bar symbol | causes the ls output to be piped to the grep command. The grep command will then take the input from the pipe and search the piped lines for any string that contains “a.out“. If grep finds any such strings it will then directly display them.


Using the bar symbol, |, is the most common form of piping. But another form of piping exists called substitution piping.


Substitution piping can be used with commands that do not work with the direct piping method shown above. An example of such a command is rm which will not accept a direct piping of file names or directories to delete. The rm command only accepts file names and directories if they are directly given in it’s parameters while calling it. This is the beauty of substitution piping. It allows you to perform a command and send the output line-by-line as a parameter to another command.


Substitution Piping Example




~/test> rm $(find | grep a.out)


What happens:

First, the commands inside of $() will run and produce a list of all files in the current directory and in any directories inside the current directory that have “a.out” in their file name. As you can see, both forms of piping can be used together. Once the commands inside the substitution pipe are completed the output will be passed line-by-line as a parameter to the first command rm which will delete every file passed to it.





Caveat emptor!
Be careful!



Piping can be a force for good and evil. It is extremely easy to be mistaken about the exact output of a command and end up deleting precious files or doing all sorts of crazy things. Test all of your commands before piping.

And remember, a wise old man once said, “To use commands is human, to pipe divine.” ;)

Posted in Linux, Tips | Tagged: , , , , , , | 2 Comments »

Linux BASH Aliases

Posted by oxaric on November 17, 2008

If you use bash then right now you can create aliases. An alias allows you to define a command that will call another command that is much longer or doesn’t automatically have the options you want.


If you want to list all of the files in your current directory you would use the command "ls". However, this command by itself will not show you hidden files. In Linux any file that starts with a period “.” is considered a hidden file. If you want to see all the files in your current directory including normal and hidden files you would need to use the command "ls -A".


Typing "ls -A" a few times isn’t such a big deal but if you are constantly using this command it would be quicker to create a bash alias. To create a bash alias we use the command “alias”. Makes sense huh? :)


Here is the basic format for the alias command:
alias new_command_name='old_command -plus_the_options_you_want'


NOTE: The new command name does not have any quotes around it but the old command with the added options has single quotes around it.


To make an alias for "ls -A" you need to type the following in your bash shell:

~/test> ls alias ls=’ls -A’ grep a.out



Once you’ve typed this line hit enter and the alias is created. Now during your current bash session if you want to see both the hidden and normal files in your current directory you only need to type "ls".


But wait!



If you create an alias manually like above you will lose all your aliases when you close the bash shell. If you want these aliases to be a permanent feature of your bash shell you need to do two things.

  1. Add the following line to your "~/.bashrc" file:
    if [ -f ~/my_bash_additions ]; then
       . ~/my_bash_additions
    fi



    NOTE: To edit this file you will need to be root or use "sudo".

  2. Create a file in ~/ called “my_bash_additions” and put your alias commands into this file.



Once you’ve done this all that’s left is to restart your bash shell. All of your aliases will be saved and ready for use. If you typed any commands wrong or anything is amiss you will probably get an error message when your bash shell is loading. If you get any errors go back into your “my_bash_additions” file, fix the errors, and re-start your bash shell. But I’m sure you’ll never make any mistakes so you’ll never have to do this. :)


Extra



Another cool thing you can do with your bash shell is to edit the way your command prompt looks. You can make your prompt colorful, plain, add extra information such as the current time, lots of stuff.


Here is a site with good information about editing your command prompt.


I’ve uploaded my own “my_bash_additions” file. Feel free to download it and try it out. It has a lot of features I like. It doesn’t have too many comments but it shouldn’t be too hard to understand.


Click to directly download my_bash_additions

Have fun playing!

Posted in BASH, Linux, Tips | Tagged: , , , , , , , | Leave a Comment »