Sunday, May 31, 2015

Beginning c/c++ compiling.

How can I compile a simple C or C++ program on Linux operating systems using bash Terminal application?

Tutorial details

Difficulty: easy
Root privileges No
Requirements GNU C/C++ compiler
Estimated completion time 10m

GNU C and C++ compiler collection
Development tools
Development libraries
IDE or text editor to write programs


Part 1: Install C/C++ compiler and related tools

To compile a C or C++ program on any Linux distro such as Ubuntu, Red Hat, Fedora, Debian and other Linux distro you need to install:

>Fedora, Redhat, Centos, Or Scientific linux:


# yum groupinstall 'Development Tools'

>Debian, Ubuntu, or Mint

$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev

To verify the install, type the following commands to display the version number and location of the compiler on Linux:

$ whereis gcc
$ which gcc
$ gcc --version

Sample outputs:

$ whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc /usr/bin/X11/gcc

$ whereis g++
g++: /usr/bin/g++ /usr/bin/X11/g++

$ gcc --version
gcc (Debian 4.7.2-5) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc
gcc: fatal error: no input files
compilation terminated.
$


Part 2:  GNU C compiler on Linux. Create a file called demo.c using a text editor such as vim or nano
 
#include<stdio.h>
/* demo.c: My first C program on a Linux
 */
int main(void) {
printf("Hello! This is a test program.\n");
return 0;
}

Save as demo.c

How do I compile the program on Linux?

Use any one of the following syntax to compile the program called demo.c:

$ cc program-source-code.c -o executable-file-name

OR

$ gcc program-source-code.c -o executable-file-name

In this example, compile demo.c, enter:
$ cc demo.c -o demo

If there is no error in your code or C program then the compiler will successfully create an executable file called demo in the current directory, otherwise you need fix the code. To verify this, type:

$ ls -al demo*
-rwxr-xr-x 1 eddie eddie 4884 May 31 20:35 demo
-rw-r--r-- 1 eddie eddie  149 May 31 20:35 demo.c

How do I run or execute the program called demo on Linux? Just simply type the the program name

 $ ./demo

OR

$ /path/to/demo

 $ ./demo
Hello! This is a test program.


Part 3: GNU C++ compiler on Linux

Create a program called demo.cpp as follows:

#include "iostream"
// demo2.C - Sample C++ program
int main(void) {
std::cout << "Hello! This is a C++ program.\n";
return 0;
}

To compile this program, enter:

$ g++ demo.cpp -o demo


To run this program, type: ./demo

$ ./demo
Hello! This is a C++ program.


Part 4: More information.
How do I generate optimized code on a Linux machine?

The syntax is as follows C compiler:
cc -O input.c -o executable
The syntax is as follows C++ compiler:
g++ -O -Wall input.C -o executable
How do I compile a C program that uses math functions?

The syntax is as follows when need pass the -lm option with gcc to link with the math libraries:
cc myth1.c -o executable -lm
How do I compile a C++ program that uses Xlib graphics functions?

The syntax is as follows when need pass the -lX11 option with gcc to link with the Xlib libraries:
g++ fireworks.C -o executable -lX11
How do I compile a program with multiple source files?

The syntax is as follows if the source code is in several files (such as light.c, sky.c, fireworks.c):
cc light.c sky.c fireworks.c -o executable
C++ syntax is as follows if the source code is in several files:
g++ ac.C bc.C file3.C -o my-program-name

See gcc(1) : Linux and Unix man page for more information.

$ man gcc

Shakespeare?


2B | !2B

Spacebar.


Friday, May 29, 2015

Wifi and the Arduino.

Good for arduino to arduino.

Have some of the nrf24l0 wifi units to play with. will test them with the Arduino first. One person suggest that a 100 nf capacitor be placed between plus five and ground for more power stability.

nrf24l01




NRF24L01-Receiver


More information at: http://playground.arduino.cc/InterfacingWithHardware/Nrf24L01
Experimental code:
https://github.com/tmrh20/RF24

Note had to set up and SPI directory and copy the files over to the Arduino folder.
The you had to tell the arduino  about the library


----------------------------------------------------------------------------------------------
Compatible to computer networks.


What is the ESP8266?

The ESP8266, is a cheap WiFi module which you can address using the UART protocol. It’s been featured on Hackaday and other DIY electronics blogs very recently, and lots of DIY electronics tinkers are very excited at the doors this module opens for their projects. The module is:
Small Footprint
Easy to integrate with (using UART)
Takes care of all other overheads for getting WiFi up-and-running (TCP/IP Stack etc…)
. . . and, perhaps more importantly than all the other factors, very cheap – it can be purchased from China in a quantity of one for less than £4 !

There are no market equivalents for the DIY community – the XBee has been a popular choice for adding WiFi to DIY projects up until now, but the ESP8266 module is close to one tenth the price making it easily accessible for all types of low cost applications.

The ESP8266 is the name of the chip it’s self manufactured by a company called Espressif, but it is sold in modules manufactured in China which look as seen below,



The module can be addressed using a series of AT commands. These are simple commands sent UART at 115200 baud. For example, once the module is wired up, you can send the command

AT+RST will perform a software reset on the device. AT+CWLAP will display all of the currently available WiFi networks etc. – simple enough!
How do I set up the ESP8266 Module?








Here is a simple pin-out for the module (the pin numbers are defined in the table
and relate to the image):

Pin Number Pin Function Pin Number Pin Function
1RX 5GPIO2
2VCC6CH_PD
3 GPIO0 7 GND
4RST 8TX


Note: Under normal operation Pin 6 (CH_PD) should be tied high (3V3) to ensure correct operation

As such, you can easily wire up the module to an Arduino or just a simple USB to serial converter and start sending AT commands (Remember: RX on the module joins to TX on the connection device and vice versa!).
What are the drawbacks of using this module?

As we’ve seen above, this is shaping up to be a very good value-for-money solution for adding WiFi capabilities to DIY projects, so what are the limitations.
The module is rather ‘power thirsty’. The current supplied via USB to an Arduino is barely enough to power this module. Other websites have recommended that you should have easily 1A available, but I have not measured this myself. This makes integrating the module slightly more tricky as a second, separate voltage regulator is required and it also rules out any battery powered projects.
It is temperamental – I’ve had a chance to play with this module, and although it works most of the time, all of the sample code I have found provides far from a solid solution – there are plenty of quirks to using it.

Code examples:  https://github.com/esp8266/Arduino/archive/esp8266-sdk-1.0.zip

Adding support for the esp8266 in the arduino ide 1.6.4 Depending on the speed of your internet connection how long it will take

Installing with Boards Manager

Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32 and 64 bit). The 1.7.3 version did not have this option.
  • Install Arduino 1.6.4 from the Arduino website.
  • Start Arduino and open Perferences window.
  • Enter http://arduino.esp8266.com/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  • Open Boards Manager from Tools > Board menu and install esp8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).




More info later...

One ipaddress into many.



Ever go somewhere and you need to have several systems be able to access their network, but they say all you can have is one Ipaddress. An Ipaddress is like a phone number for a computer.  You can only have one per system under normal circumstances. Here with an extra system, we will show you how to do this.

A multitude of reasons exist as to why one would want to build a custom router vs. suffer with the performance, reliability issues, and limitations of an off-the-shelf solution. In the spirit of keeping this post short, I won’t launch into a long diatribe on the pros and cons of each here, but I have plenty of thoughts on this, so if you are interested, just ask.

What we are about to do is configure an incredibly fast and stable router/gateway solution for your home/office in about 15 minutes. (Note: This post assumes you already have your machine loaded up with a fresh copy of Ubuntu 14.04 or an equivalent and you have the two needed NICs installed.) This is not a firewall.

First, let’s make three initial assumptions:

    eth0 is the public interface (the Cable/DSL modem is attached to this NIC)
    eth1 is the private interface (your switch is connected to this NIC)
    All of the client computers, servers, WAPs, etc. are connected to the switch

Let’s get started with the configuration. Set your timer and type quickly! :)

1.) Configure the network interfaces
You may need to make sure for older systems that plug and play os is enabled. Change the “address”, “netmask”, and “broadcast” values to match your internal network preferences. They must be different from the host aka wan network.

sudo nano -w /etc/network/interfaces
# The external WAN interface (eth0) public
allow-hotplug eth0
iface eth0 inet dhcp

# The internal LAN interface (eth1) private
allow-hotplug eth1
iface eth1 inet static
   address 10.1.10.1
   netmask 255.255.255.0
   network 10.1.10.0
   broadcast 10.1.10.255

2. Install and configure DNSmasq
DNSmasq is DNS forwarder and DHCP server. Change “domain” to the FQDN of your network and “dhcp-range” to the desired range of DHCP addresses you would like your router to serve out to clients.

sudo apt-get install dnsmasq

nano -w /etc/dnsmasq.conf
interface=eth1
listen-address=127.0.0.1
domain=home.andreimatei.com
0dhcp-range=10.1.10.100,10.1.10.110,12h

3.) Enable IP Forwarding
Uncomment the following line:

sudo nano -w /etc/sysctl.conf
net.ipv4.ip_forward=1

4.) Configure iptables
We create a file called /etc/iptables.rules and put this rule set inside of it.  As an example, this set includes allowing tcp traffic in from the outside world on port 222 (I run SSH on this alternate port) and also port-forwards tcp port 50,000 to an internal machine with the ip of 10,1,10.3 Use this as a guide for your own rules.

Note that when you do this access to the system will be locked down amd ssh not work. In fact a ping to the system will be ignored.

sudo nano -w /etc/iptables.rules

*nat
-A PREROUTING -i eth0 -p tcp -m tcp --dport 50000 -j DNAT --to-destination 192.168.0.3:50000
-A POSTROUTING -o eth0 -j MASQUERADE
COMMIT

*filter
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 222 -j ACCEPT
-A INPUT -i eth0 -j DROP
-A FORWARD -i eth0 -p tcp -m tcp --dport 50000 -m state --state NEW -j ACCEPT
COMMIT

5.) Activate your iptables rules

iptables-restore < /etc/iptables.rules

6.) Ensure iptables rules start on boot
Insert the following line into your /etc/network/interfaces file right underneath “iface lo inet loopback”

nano -w /etc/network/interfaces
pre-up iptables-restore < /etc/iptables.rules

7.) Reboot and Verify
That’s it! After a reboot, you should now have a very basic Linux Router/Gateway for your network.

Run through

router:~$ sudo nano -w /etc/iptables.rules
[sudo] password for eddie:

router:~$ sudo iptables-restore < /etc/iptables.rules

router:~$ sudo nano -w /etc/network/interfaces

router:~$ sudo reboot

router:~$
Broadcast message from router
    (/dev/pts/0) at 8:44 ...

The system is going down for reboot NOW!
Connection to 192.168.1.102 closed by remote host.

Connection to 192.168.1.102 closed.

You should be able to use your router now.

Note: you should be able to do this with most any distro, but using different commands. Want to try this with a Pentium 1 and Slackware. Did not have time to trying before putting this article out.

oedt01:~$ ssh 192.168.1.102
192.168.1.102's password:
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-53-generic i686)

 * Documentation:  https://help.ubuntu.com/
Last login: Fri May 29 07:39:02 2015

router:~$ cd /etc

router:/etc$ sudo vim  dnsmasq.conf
[sudo] password for eddie:

router:/etc$ sudo vim /etc/network/interfaces

router:/etc$ sudo vim  dnsmasq.conf

router:/etc$ sudo service dnsmasq restart

 * Restarting DNS forwarder and DHCP server dnsmasq                      [ OK ]

router:/etc$ sudo service dnsmasq status
 * Checking DNS forwarder and DHCP server dnsmasq                                * (running)

Connect to the client side. We used a tablet and crossover adapter to check dnsmasq. You could use a network switch also.

Note: if you get a usb to ethernet adapter, you can make a mice little Raspberry Pi router. You could also use a wireless card for the lan, but that requires a bit more security,

 -------------------------------------------

You may also want to setup webmin to make managing the router easier.


It is not required, but installing webmin can make managing the router easier.

router:/etc$ cd

router:~$ wget http://prdownloads.sourceforge.net/webadmin/webmin_1.750_all.deb
--2015-05-29 08:15:22--  http://prdownloads.sourceforge.net/webadmin/webmin_1.750_all.deb
Resolving prdownloads.sourceforge.net (prdownloads.sourceforge.net)... 216.34.181.59
Connecting to prdownloads.sourceforge.net (prdownloads.sourceforge.net)|216.34.181.59|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://downloads.sourceforge.net/project/webadmin/webmin/1.750/webmin_1.750_all.deb [following]
--2015-05-29 08:15:22--  http://downloads.sourceforge.net/project/webadmin/webmin/1.750/webmin_1.750_all.deb
Resolving downloads.sourceforge.net (downloads.sourceforge.net)... 216.34.181.59
Connecting to downloads.sourceforge.net (downloads.sourceforge.net)|216.34.181.59|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://hivelocity.dl.sourceforge.net/project/webadmin/webmin/1.750/webmin_1.750_all.deb [following]
--2015-05-29 08:15:22--  http://hivelocity.dl.sourceforge.net/project/webadmin/webmin/1.750/webmin_1.750_all.deb
Resolving hivelocity.dl.sourceforge.net (hivelocity.dl.sourceforge.net)... 74.50.101.106
Connecting to hivelocity.dl.sourceforge.net (hivelocity.dl.sourceforge.net)|74.50.101.106|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26195366 (25M) [application/octet-stream]
Saving to: ‘webmin_1.750_all.deb’

100%[======================================>] 26,195,366   586KB/s   in 42s   

2015-05-29 08:16:04 (614 KB/s) - ‘webmin_1.750_all.deb’ saved [26195366/26195366]

router:~$ sudo dpkg -i webmin_1.750_all.debSelecting previously unselected package webmin.
(Reading database ... 57184 files and directories currently installed.)
Preparing to unpack webmin_1.750_all.deb ...
Unpacking webmin (1.750) ...
dpkg: dependency problems prevent configuration of webmin:
 webmin depends on libnet-ssleay-perl; however:
  Package libnet-ssleay-perl is not installed.
 webmin depends on libauthen-pam-perl; however:
  Package libauthen-pam-perl is not installed.
 webmin depends on libio-pty-perl; however:
  Package libio-pty-perl is not installed.
 webmin depends on apt-show-versions; however:
  Package apt-show-versions is not installed.

dpkg: error processing package webmin (--install):
 dependency problems - leaving unconfigured
Processing triggers for ureadahead (0.100.0-16) ...
Errors were encountered while processing:
 webmin

There will probably be an error. There is an easy fix.

router:~$ sudo apt-get upgrade -f
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Correcting dependencies... Done
Calculating upgrade... Done
The following NEW packages will be installed:
  apt-show-versions libapt-pkg-perl libauthen-pam-perl libio-pty-perl
  libnet-ssleay-perl
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
Need to get 425 kB of archives.
After this operation, 1,752 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/main libnet-ssleay-perl i386 1.58-1 [242 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ trusty/universe libauthen-pam-perl i386 0.16-2build3 [27.8 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu/ trusty/main libio-pty-perl i386 1:1.08-1build4 [36.7 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu/ trusty/main libapt-pkg-perl i386 0.1.29build1 [84.5 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu/ trusty/universe apt-show-versions all 0.22.3 [33.9 kB]
Fetched 425 kB in 1s (226 kB/s)        
Selecting previously unselected package libnet-ssleay-perl.
(Reading database ... 82053 files and directories currently installed.)
Preparing to unpack .../libnet-ssleay-perl_1.58-1_i386.deb ...
Unpacking libnet-ssleay-perl (1.58-1) ...
Selecting previously unselected package libauthen-pam-perl.
Preparing to unpack .../libauthen-pam-perl_0.16-2build3_i386.deb ...
Unpacking libauthen-pam-perl (0.16-2build3) ...
Selecting previously unselected package libio-pty-perl.
Preparing to unpack .../libio-pty-perl_1%3a1.08-1build4_i386.deb ...
Unpacking libio-pty-perl (1:1.08-1build4) ...
Selecting previously unselected package libapt-pkg-perl.
Preparing to unpack .../libapt-pkg-perl_0.1.29build1_i386.deb ...
Unpacking libapt-pkg-perl (0.1.29build1) ...
Selecting previously unselected package apt-show-versions.
Preparing to unpack .../apt-show-versions_0.22.3_all.deb ...
Unpacking apt-show-versions (0.22.3) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up libnet-ssleay-perl (1.58-1) ...
Setting up libauthen-pam-perl (0.16-2build3) ...
Setting up libio-pty-perl (1:1.08-1build4) ...
Setting up libapt-pkg-perl (0.1.29build1) ...
Setting up apt-show-versions (0.22.3) ...
** initializing cache. This may take a while **
Setting up webmin (1.750) ...
Webmin install complete. You can now login to https://router:10000/
as root with your root password, or as any user who can use sudo
to run commands as root.
Processing triggers for ureadahead (0.100.0-16) ...

Nix-ese?


L.C,


Thursday, May 28, 2015

Wednesday, May 27, 2015

Sunday, May 24, 2015

Cnc routing.



CNC is a big thing. You might think of it as the 2d version of a 3d machine. With a 2d machine you remove material and with a 3d machine you add material to get an object. Always have wanted to build one myself. Have the stepper motors, just have to make the rack.



Numerical control (NC) is the automation of machine tools that are operated by precisely programmed commands encoded on a storage medium, as opposed to controlled manually via hand wheels or levers, or mechanically automated via cams alone. Most NC today is computer (or computerized) numerical control (CNC) in which computers play an integral part of the control.

Nowadays you can get a computer and almost do all of it yourself.  First you have to have a drawing prepared. There are lots of existing artwork you can use or you can create your own .svg files with Inkscape. Scalable Vector Graphics (SVG) is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation. The SVG specification is an open standard developed by the World Wide Web Consortium (W3C) since 1999. SVG images and their behaviors are defined in XML text files. See also  https://www.google.com/?gws_rd=ssl#q=inkscape+tutorial


Once you have created your graphic or use and existing one, you can  use a specialized version of linux know as linuxcnc.


You can also use a program called Jscut that can run from a web server or a local machine. The advantage is that you can install it once and use it many times where ever you are at.  Most systems generate gcode.


G-code (also RS-274), which has many variants, is the common name for the most widely used numerical control (NC) programming language. It is used mainly in computer-aided manufacturing for controlling automated machine tools. G-code is sometimes called G programming language.

In fundamental terms, G-code is a language in which people tell computerized machine tools how to make something. The how is defined by instructions on where to move, how fast to move, and through what path to move. The most common situation is that, within a machine tool, a cutting tool is moved according to these instructions through a toolpath, cutting away excess material to leave only the finished workpiece. The same concept also extends to noncutting tools such as forming or burnishing tools, photoplotting, additive methods such as 3D printing, and measuring instruments.



Jscut is certainly easier than generating Gcode by hand then feeding a papertape  into a nc machine.  You do need to know what cnc is about to really use the Jscut. See also: https://www.youtube.com/watch?v=dVgf0Hf91vA

What will you make?




Thursday, May 21, 2015

oldie, but goodie.

Early dual cpu (aka dual core) system that was set up as a router. A collectors item.

eyeOS is forking into Oneos.

Just a bit of news again. According to the Linux Link Tech Show aka #tllts eyeOS  a gui desktop web based application  that can run from a gui-less server has been forked to Oneos.  Actually I have seen several similar sort of micro googledocs projects on Sourceforge. http://www.instructables.com/id/eyeOS/
 http://oneye-project.org/




Monday, May 18, 2015

Get your google data from the google cloud.

Get your google data such as pictures. email and etc. Just go to https://www.google.com/settings/takeout and then you can choose what to get. an archive will be created for you to download when the archive is finished, or you can wait and use the email they send you when the archive is done to go back and download the data.



Replacement script.

Look at astrology as an intellectual cartoon and an insight into human thinking. So I will peek at it once in a while. Also gave a chance to play with page scraping again.

$ ./horoscope.sh Virgo

Daily Horoscope for Tuesday 19th May 2015

Share :

Through friends of someone close, you could learn more about their
background. This extra information, particularly if it's related to how
they acquired their qualifications, and the friendships they made en
route, may not be something you wish to discuss with others, but might go
some way towards explaining why they are pulled towards certain
geographical locations. This might even impact on decisions being taken
now for travel in a couple of months time.

VIRGO

---------------------------------------------






Wrote a script to pull the daily horoscope for a particular sign. The site we are getting the data from has changed. So that led me to go to another site for the time being. Actually it seems a blessing in disguise because now we can get more than the daily listing. Here is the original script.


Original script
[code]
 #===================================
# Get today's horoscope
# get sign
hsign=""
read -p "Enter your horscope sign: " hsign
if [ -z $hsign  ]
then hsign="virgo"
fi
# hsign=$(zenity --entry \
#        --title="Daily Horoscope" \
#        --text="Enter your _sign:" \
#        --entry-text "$hsign")
#-------------------------------------------
# output data
# character width required for information box
cw=38
#create data file (datadir and file name can be changed to your needs.
datadir="/home/eddie/bin/signs"
filename="$datadir/th"
# make sure hsign is uppercase
hsign="`echo $hsign|tr '[a-z]' '[A-Z]'`"
cat $datadir/$hsign > $filename
echo -n "Today's date: " >> $filename
date +%D >> $filename
echo "Today's horoscope for:" >> $filename
lynx -width 1000 -dump "http://www.creators.com/lifestylefeatures/horoscopes/horoscopes-by-holiday.html" | grep $hsign | fold -sw $cw >> $filename
#output
# zenity --text-info --filename=$filename
cat $filename
#remove  unneeded file
rm $filename
[/code]

Was not sure how to do the script, but the I remembered the old CNN script we used. Minor changes and so we are back with the scopes again.  To invoke the script you would use   ./horoscope.sh Signname

$ ./horoscope.sh Virgo


First letter must be capitalized and the rest lower case.

New script
[code;]
 ####################################
# Horoscope  Grabber
#
#===============================
# Assignments
# --------------------------------
datafile="horoscope.txt"
let "flag = 0"
# end assignments
# 2b implemented
# foo="virgo"
# foo="$(tr '[:lower:]' '[:upper:]' <<< ${foo:0:1})${foo:1}"
# echo $foo
# Virgo
# or just 
# foo=virgo
# echo "${foo^}"
# echo $foo
# Virgo
#=================================
#
# Get data file
#---------------------------------
elinks -dump "http://www.horoscopes.co.uk/$1/Daily-Horoscope.php"  > $datafile
#=================================
#
# Extract and display data
#---------------------------------
while read line
do fdata[$a]=$line
    # where to start
    echo $line | grep -q "Daily Horoscope"
    if  [ $? -eq 0 ]; then
        # header
        clear
        let "flag = 1"
    fi
    if [ $flag -eq 1 ]; then
        # where to end
        echo $line | grep -q "$1"
            if [ $? -eq 0 ]; then
            let "flag = 0"
        else
            echo $line | grep -q "IMG"       
            if [ $? -eq 0 ]; then
                let "response = donothing"
            else
                echo $line | sed 's/\[.*\]//'
            fi
        fi
    fi
let "a += 1"
done < $datafile
# footer
echo ---------------------------------------------
echo
#===================================
# End.
####################################

[/code]


Saturday, May 16, 2015

Play scrabble with grep!

Doing a crossword and no one around to ask about a word, then this instructable is for you. Have not played scrabble since I was a kid. Also like to do crossword puzzles. If everyone uses the same tools, then it really is playing fair.There is an easy way to make the game more interesting and you increase your vocabulary as well as your typing skills at the same time.

Recommended that you have at least some time at the linux command line. These tricks can also be done on the Mac and BSD with minor modifications.

Note: a quick grep trick for finding a file wth certain text is:

 locate filename | grep search text

$ locate.pdf | grep arduino

Step 1: Whats required?

Hardware:
Linux based computer where you can access the command line.

Software:
A word file which should be already available in linux.
Access to the "grep" command.

Knowledge:
Assuming you know how to do a crossword puzzle and or know how to play scrabble.
You know how to use the linux command line.

Step 2: Getting the words.

What would one need? Maybe a list of words and a way to search the list of words. Let’s see there is a list of words on a linux system. That file could be easily copied to the home directory. You definitely want to copy it so if you edit it and there is an issue, there will be no problems with your system. $ cp /usr/share/dict/words .

Step 3: Using grep.

To get to know a command called grep a little better, tried to explore how it could be used to an advantage.  For now, how can I search through the file. The grep command will work perfectly. Well how do you use it. Lets say we want to add some letters where there is ed at the end. We use the “$” sign to show that we want something at the end of the word. $ grep “ed$” words

Alfred
Americanized
Anglicized
Appleseed
Brailled
Englished
Ethelred
Fed
Fred
Frenched

This generates a rather long list. so we might want to paginate the output.
$ grep “ed$” words | less
or we might want to save the list to a file we can edit and peruse for later use.
$ grep “ed$” words > wordfile
That is nice, now lets do letters at the beggining of the file. We use the “^” sign to show that we want something at the beggining of the word.
$ grep “^th” words

thank
thanked
thankful
thankfuller
thankfullest
thankfully
thankfulness
thankfulness’s

You can combine the two commands so that you can find a word that has the beginning and the ending we want. We want to use an “*” to say any letters can go in between. In this case thought we want one of the letters to be “m”. We use “[]” to show which possible letter or letters we want in between.
$ grep “^zo*[m]*ed$” words
zed
zoomed
There you can use the “[]” to say I want to have any words that start with these two letters.
$ grep “^[xz]“

xylem’s
xylophone
xylophone’s
xylophones
xylophonist
xylophonists
z
zanied
zanier
zanies
zaniest


grep "^m..t..s$" words
mantels
mantles
martyrs
masters
matters
mentors
misters
mittens
mortals
mortars
mottoes
mouthes
mutters
mystics

Blow by blow:

* ^ - The carat (shift-6) says "this is the beginning of the line". Without it, it would find all words like "fundamentals".
* $ - The dollar sign is the same thing, only for the end of the line. Without it, you'd also get words like "mattresses".
* . - The period means "any character here". One, and one only, character will match here.

But suppose you're dealing with a game besides a crossword puzzle, like Scrabble for instance, and you're limited by more constraints than in a crossword. You might want to 'hook' (Scrabble lingo for 'add letters to the beginning or end of a word to form more words'). So, let's see how many words end in "are".

ß grep "are$" words | wc -l
43

Well, those are good odds. But we hit the edge of the board with some of them (I peeked). So, we need words that are seven letters or less which end in "are". "^....are$" would get all of the seven letter words, but not the shorter ones. The solution is rather cryptic this time:

ß grep "^.\{1,4\}are$" words
airfare
aware
bare
beware
blare
care
compare
dare
declare
ensnare
fanfare
fare
flare
glare
hare
mare
pare
prepare
rare
scare
share
snare
spare
square
stare
unaware
ware
warfare
welfare

...but we've met the caret, dollar sign, and period before, so really the new part is the \{1,4\}. This says "match as few as one, and as many as four, repetitions of the previous character". The activator for the number range is the curly braces, which then have to be escaped with slashes (does anybody know why, class?). And since the previous character is a period, which matches any letter, we've found all the words shorter than eight letters which end in "are".

This is all well and good, but we only have so many letters to work with in Scrabble at one time. Say that our current rack has the letters "C F T W A B M". Can we limit it to only words which use those letters?

ß grep "^[cftwabm]\{1,4\}are$" words
aware
bare
care
fare
mare
ware

Ah, now we're getting somewhere! The [] square brackets give the set of acceptable characters. Another way to use them is to express a range (e. g. [0-9]), but that's hardly the usual case in word games.

The only limitation is the imagination. If you are playing scrabble, let your partners know what you doing to prevent any hurt feelings. I like to use this same idea for crossword puzzles also.

 Picture of Last thoughts. 

 

 

 

 

 

 

 

 

 

 

 

Step 4: Last thoughts.



If you want to combine two or more word lists to have a greater vocabulary to work from is fine also: (words.new must be an ascii or text file.)

$ mv words words.old
$ cat words.old words.new > words.temp
$ sort words.temp | uniq -u > words

List amount of words

$ wc -l words

You should now have a larger word file!

One other hint:
One zip file I downloaded of words had the words in files separated by the first letter. I needed them in one list. No problem.

$ cat {A..Z}.DIC > words.new

There are plenty of on-line web pages to do all this stuff, but for me it is nice not to have to depend on the internet all the time for one's needs. Have fun!

Step 5: Bonus: What words can you make.


Like anagrams or wonder what words you can make then try the "an" command.
$ sudo apt-get update
$ sudo apt-get install an
$ an "instructables"
inscrutable st
inscrutable t's
inscrutable ts
inscrutable t s
inscrutable t s
....
....
....
...
Want a limited list of only 10 options try:
$ an -n 10 "instructables"
inscrutable st
inscrutable t's
inscrutable ts
inscrutable t s
inscrutable t s
inscrutable t s
inscrutable t s
incurables st t
incurables t's t
incurables ts t

Step 6: Use grep to extract from a stream


Simple batch file to get weather information: (head command limits results).
#!/bin/bash
zip=$1
tmon=$(date +"%b")
tday=$(date +"%d")
echo "The weather for $zip on $tmon $tday:"
lynx -width 1000 -dump "http://m.wund.com/cgi-bin/findweather/getForecast?brand=mobile&query=$zip" > weather
cat weather | grep "Updated"
cat weather | grep "Observed"
cat weather | grep "Temperature"
cat weather | grep "Humidity"
cat weather | grep " Dew Point"
cat weather | grep "Wind" | head -1
cat weather | grep "Wind Gust" | head -1
cat weather | grep "Pressure"
cat weather | grep "Conditions" | head -1
cat weather | grep "Visibility"
cat weather | grep "UV"
cat weather | grep "Clouds"
cat weather | grep "Yesterday's Maximum"
cat weather | grep " Yesterday's Minimum"
cat weather | grep "Sunrise"
cat weather | grep "Sunset"
cat weather | grep "Moon Rise"
cat weather | grep "Moon Set"
cat weather | grep -A1 "Moon Phase"
cat weather | grep "Raw METAR"

Venn


You just get lucky sometimes.