Report the current time in Binary format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Report the current time in Binary format
# 1  
Old 07-21-2012
Question Report the current time in Binary format

Hey fellas,

I am trying to report the current time in a binary format which is overwriting every second. So far I wrote following script which I know looks stupid since I'm a newbie! Smilie

Code:
#!/bin/bash
while true;
do 
	clear; 
	date | 
	awk '{print $4}' | 
	awk -F ":" '{print substr($1,0,1) "\t" substr($1,2,1) "\n" substr($2,0,1) "\t" substr($2,2,1 )"\n" substr($3,0,1) "\t" substr($3,2,1) }' ; 
	sleep 1; 
done

As you see the output is a table like this:
1 3
4 9
0 8

I want to convert this to a binary format to have an output like this:
0001 0011
0100 1001
0000 1000


I know the following command turns a given digit into binary format:
Code:
echo 'obase=2;<digit>' | bc

But I don't know how to use this or any other ways of conversion in a while loop in my script.

Thanks in advance for your kind helps! Smilie

Last edited by @man; 07-21-2012 at 10:25 AM..
# 2  
Old 07-21-2012
Try:
Code:
#!/bin/bash
while true;
do
        clear;
        date |
        awk '{print $4}' |
        awk -F ":" '{print substr($1,0,1) "\t" substr($1,2,1) "\n" substr($2,0,1) "\t" substr($2,2,1 )"\n" substr($3,0,1) "\t" substr($3,2,1) }' | while read a b;do
          a=`echo "obase=2;$a" | bc`
          b=`echo "obase=2;$b" | bc`
          printf "%04d\t%04d\n" $a $b
        done
        sleep 1;
done

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-21-2012
it works perfectly as I needed! Smilie thanks man!

just a question, I don't understand this part:
Code:
"%04d\t%04d\n"

And I was trying to use
Code:
a='echo "obase=2;$a" | bc'

instead of
Code:
a=`echo "obase=2;$a" | bc`

I don't understand the difference between ` and ' !! If you have time to explain I would appreciate it! otherwise thanks again! Smilie
# 4  
Old 07-21-2012
Quote:
Originally Posted by @man
just a question, I don't understand this part:
Code:
"%04d\t%04d\n"

It is part of printf format string. You can read more about printf here: printf format string - Wikipedia, the free encyclopedia
Quote:
Originally Posted by @man
And I was trying to use
Code:
a='echo "obase=2;$a" | bc'

instead of
Code:
a=`echo "obase=2;$a" | bc`

I don't understand the difference between ` and ' !!
Single quotes (') are just one type of quotes. What I used is not quote character, but "backtick", which only looks similar to the single quote character. In shell "backtick" is used to put output of some command into variable.
This User Gave Thanks to bartus11 For This Post:
# 5  
Old 07-23-2012
Another version with a few less callse to clear and bc:

Code:
CLS=$(clear)
while true
do
  echo $CLS `date +%H%M%S | awk '
  BEGIN{split("0000,0001,0010,0011,0100,0101,0110,0111,1000,1001,1010",bin,",") }
  {for(i=1;i<7;i++) printf bin[substr($1,i,1)+1]" ";}'`
  sleep 1
done


Last edited by Chubler_XL; 07-23-2012 at 01:20 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

DB2 convert digits to binary format

Dear Team We use DB2 v10.5 and using DBArtisan tool Can someone please guide how to convert digits to binary numbers using db2 feature. Ex> for number 9 , binary should be 1001 ( 8+1) Any help appreciated. Thanks (2 Replies)
Discussion started by: Perlbaby
2 Replies

2. Shell Programming and Scripting

Current shell session hungs when run another binary

Hi, I am on AIX 7.1 and when I try to login to my account and sudo to other generic account (mqm user) my ssh session just hungs. ================== $ id uid=16150(sxp) gid=179(rax) groups=1179(raxs) $ $ $ sudo su - mqm Password: -------- Changed username to: mqm -------- .... (1 Reply)
Discussion started by: bdpl
1 Replies

3. Shell Programming and Scripting

Convert jpg file to binary format

Hi Team, Here's the requirement. I have a image file in jpg format in unix. Now I need to i. convert the jpg format to binary format ii. followed by loading the binary file to Oracle db. Can anyone help me out? Thanks Krishnakanth Manivannan (4 Replies)
Discussion started by: kmanivan82
4 Replies

4. Shell Programming and Scripting

Convert UTC time into current UNIX sever time zone

Hi guys thanks for the help for my previous posts.Now i have a requirement that i download a XMl file which has UTC time stamp.I need to convert UTC time into Unix server timezone. For ex if the time zone of unix server is CDT then i need to convert into CDT.whatever may be the system time... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

5. Shell Programming and Scripting

Displaying current date time of EDT in IST time

Hi Folks, My server time is in EDT. And i am sending automated mails from that server in which i need to display the current date time as per IST (GMT+5:30). Please advice how to display the date time as per IST. IST time leads 9:30 mins to EDT. and i wrote something like below. ... (6 Replies)
Discussion started by: Showdown
6 Replies

6. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

7. UNIX for Dummies Questions & Answers

getting the current time in Epoch format

Hi everybody, I want to get the current time in epoch format (in UNIX or Korn Shell) and store it in a variable called currentTime. Any response will be highly appreciated:) Thanks in advance, omoyne:D (8 Replies)
Discussion started by: omoyne
8 Replies

8. Programming

how to write a file to binary format in C ?

I'm in the Solaris environment. I want to write data to a file, but I don't want it to be easily read from the C shell. For example, here's my code: main () { FILE *fo; fo = fopen ("filename", "w"); fprintf (fo, "This is a test.\n"); fclose (fo); } Anyone can open up... (3 Replies)
Discussion started by: serendipity1276
3 Replies

9. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

10. Programming

Binary to text format conversion

Hi, Please can any one tell me how to convert binary data to text format and vice versa. If possible give me the algorithm or C program. Thanks in advance Waiting for reply Bye:o (5 Replies)
Discussion started by: manjunath
5 Replies
Login or Register to Ask a Question