Thursday, July 23, 2015

C Programming


The situation is so much better for programmers today - a cheap used PC, a linux CD, and an internet account, and you have all the tools necessary to work your way to any level of programming skill you want to shoot for."


I've got a cheap computer with Linux on it and "A Book on C" by Al Kelley and Ira Pohl. It's time to shoot high ... 

1. Program Output

Program Output

Previous | Home | Next


Gather

Read Chapter 1, section 'Program Output (p. 6-10)

Reflect 

In file sea.c (omit line numbers, empty lines are only put for clarity of output)
1:  #include <stdio.h>  
2:    
3:  int main(void) {  
4:    
5:    printf("from sea to shining C\n");  
6:    
7:    return 0;  
8:  }  
download code here.

Linux compiling:
 $ gcc -o sea sea.c  

Running program:
 ./sea  

Line 1:
Directive for compiler pre-processor which will include the content of the file stdio.h which is found in the usual place (known to the compiler). This file will allow me to use function printf and others

Line 3:
Every program starts with function main(). Function will return an int (integer) to the OS that started program (Linux here).

Line 5:
Function printf(), which is loaded with line 1 directive, will display on the screen the string (sequence of characters enclosed between quotation marks. The string will end with 'new line' character (\n).

Line 7:
Program before finishing its job, is going to return number 0 (return 0) to the OS (which is code for 'all went well').

Lines 3-8:
Body of function main() is between curly braces {}. It is the code that will be run sequentialy (top-to-bottom).

Create

Introducing some errors in the code to see how the compiler is going to react.

Error 1
1:  #include <stdio.h>  
2:    
3:  int main(void)   
4:    
5:    printf("from sea to shining C\n");  
6:    
7:    return 0;  
8:  }  

During compilation and linking I get:
 $ gcc -o sea sea.c   
 sea.c: In function ‘main’:  
 sea.c:5:5: error: expected declaration specifiers before ‘printf’  
    printf("from sea to shining C\n");  
    ^  
 sea.c:7:5: error: expected declaration specifiers before ‘return’  
    return 0;  
    ^  
 sea.c:8:1: error: expected declaration specifiers before ‘}’ token  
  }  
  ^  
 sea.c:8:1: error: expected ‘{’ at end of input  
   

What is the problem?

Error 2
1:  #include <stdio.h>  
2:    
3:  int main(void){   
4:    
5:    printf("from sea to shining C\n")  
6:    
7:    return 0;  
8:  }  

During compilation and linking I get:
 $ gcc -o sea sea.c   
 sea.c: In function ‘main’:  
 sea.c:7:5: error: expected ‘;’ before ‘return’  
    return 0;  
    ^  

What is the problem?

Error 3
1:  #include <stdio.h>  
2:    
3:  int main(void){   
4:    
5:    printf("from sea to shining C\n);  
6:    
7:    return 0;  
8:  }  

During compilation and linking I get:
 $ gcc -o sea sea.c   
 sea.c: In function ‘main’:  
 sea.c:5:12: warning: missing terminating " character [enabled by default]  
    printf("from sea to shining C\n);  
       ^  
 sea.c:5:5: error: missing terminating " character  
    printf("from sea to shining C\n);  
    ^  
 sea.c:7:5: error: expected expression before ‘return’  
    return 0;  
    ^  
 sea.c:8:1: error: expected ‘;’ before ‘}’ token  
  }  
  ^  

What is the problem?

Test

Write a program that displays the following:

first.c
first.c



Previous | Home | Next

Wednesday, July 22, 2015

CLI Explorations



The list of Raspberry Pi Explorations

Day 1: Installation
Day 2: Making Myself At Home
Day 3: VIM - Powerful Text Editor
Day 4: Linux As a Problem Solving Tool
Day 5: The Linux Command Line Ch1-3 Notes
Day 6: The Linux Command Line Ch4 Notes - Manipulating Files and Directories
Day 7: The Linux Command Line Ch5 Notes - Working With Commands
Day 8: The Linux Command Line Ch6 Notes - Redirection
Day 9: The Linux Command Line Ch7 Notes - Seeing The World As The Shell Sees It
Day 10: The Linux Command Line Ch8 Notes - Advanced Keyboard Tricks
Day 11: The Linux Command Line Ch9 Notes - Permissions
Day 12: The Linux Command Line Ch10 Notes - Processes
Day 13: The Linux Command Line Ch10 Notes -  Processes Continued
Day 14: The Linux Command Line Ch11 Notes - The Environment
Day 15: The Linux Command Line Ch12 Notes - Gentle Introduction To VIM
Day 16: The Linux Command Line Ch13 Notes - Customizing The Prompt
Day 17: The Linux Command Line Ch14 Notes - Package Management
Day 18: Installation of Raspbian Step By Step (instead of ch15)
Day 19: The Linux Command Line Ch16 Notes - (ping, traceroute etc.)
Day 20: The Linux Command Line Ch16 Notes - Continued (FTP, wget, ssh)
Day 21: The Linux Command Line Ch17 Notes - Finding Files and Directories
Day 22: The Linux Command Line Ch18 Notes - Archive and Backup
Day 23: The Linux Command Line Ch19 Notes - Regular Expressions Part 1

Day 23: The Linux Command Line Ch19 Notes

Previous | Home | Terminology | Next

Chapter 19 - Regular Expressions

What are regular expressions?

Let's quote the author of "The Linux Command Line" book William E. Shotts, Jr

"Simply put, regular expressions are symbolic notations used to identify patterns in text. In some ways, they resemble the shell’s wildcard method of matching file and pathnames but on a much grander scale. Regular expressions are supported by many command-line tools and by most programming languages to facilitate the solution of text manipulation problems. However,
to further confuse things, not all regular expressions are the same; they vary slightly from tool to tool and from programming language to language. For our discussion, we will limit ourselves to regular expressions as described in the POSIX standard (which will cover most of the command-line tools), as opposed to many programming languages (most notably Perl ), which use slightly larger and richer sets of notations."

List of Regular Expressions Metacharacters
(can be escaped and treated literally with backslash)


^ $ [ ] { } - ( ) | \

Note:

As we can see, many of the regular-expression metacharacters are also characters that have meaning to the shell when expansion is performed. When we pass regular expressions containing metacharacters on the command line, it is vital that they be enclosed in quotes to prevent the shell from attempting to expand them.

Little explanation:

. matches any single character.

* matches zero or more of characters (including a character specified by a regular expression) that immediately precedes it.

? matches zero or one occurrences of the preceding regular expression.

+ matches one or more occurrences of the preceding regular expression.

^ matches the first character of regular expression, matches the beginning of the line. 

$ matches the last character of regular expression, matches the end of the line.

[] matches any one of the class of characters enclosed between the brackets. A circumflex (^) as first character inside brackets reverses the match to all characters except newline and those listed in the class. A hyphen (-) is used to indicate a range of characters. The close bracket (]) as the first character in class is a member of the class. All other metacharacters lose their meaning when specified as members of a class.

[^bg]zip matches zip NOT preceded by bg.

{n} matches the preceding element if it occurs exactly n times.

{n,m} matches the preceding element if it occurs at least n times, but no more than m times.
{n,} matches the preceding element if it occurs n or more times.

{,m} matches the preceding element if it occurs no more than m times.

- is used to denote a range of characters (e.g. [a-z] a through z.

() matches an expression.

| acts as logical or (alternation).

\ treat following metacharacter as literal and NOT metacharacter.


The lab in next post!


Previous | Home | Terminology | Next

Tuesday, July 21, 2015

Day 22: The Linux Command Line Ch18 Notes


Chapter 18 - Archive and Backup

Commands:

Compressing Programs:

gzip - Compress or expand files.
bzip2 - A block sorting file compressor.

the archiving programs:

tar - Tape-archiving utility.
zip - Package and compress files.

and the file synchronization program:

rsync - Remote file and directory synchronization.


Lab 12

  1. List the /etc directory content and redirect output to foo.txt file. Check the size of the file.
  2. Compress foo.txt file and check its size after compression. Use gzip. Then decompress the file.
  3. Compress the file using bzip2 utility and check the file size.
  4. Decompress the file.
  5. Use foo.txt to create an archive that is going to be compressed at the same time (foo.tar.gz). Use: Verbose option.
  6. You are about to send a file to Window user. How would you compress/decompres it?
  7. There is an external disk mounted in /media/jaro directory. Its name is Seagate Expansion Drive. Crate a 'backup' directory on it and store a directory on it (my case 'Books').

Lab 12 Solutions

List the /etc directory content and redirect output to foo.txt file. Check the size of the file.

pi@raspberrypi ~ $ ls /etc > foo.txt; ls -l foo*
-rw-r--r-- 1 pi pi 1798 Jul 21 09:22 foo.txt
pi@raspberrypi ~ $ 

Compress foo.txt file and check its size after compression. Use gzip. Then decompress the file.

pi@raspberrypi ~ $ gzip foo.txt; ls -l foo*
-rw-r--r-- 1 pi pi 910 Jul 21 09:22 foo.txt.gz

pi@raspberrypi ~ $

pi@raspberrypi ~ $ gunzip foo.txt.gz

Compress the file using bzip2 utility and check the file size.

Decompress the file.

pi@raspberrypi ~ $ bzip2 foo.txt ; ls -l foo*
-rw-r--r-- 1 pi pi 1001 Jul 21 09:22 foo.txt.bz2

pi@raspberrypi ~ $

pi@raspberrypi ~ $ bunzip2 foo.txt.bz2 

pi@raspberrypi ~ $ 

Use foo.txt to create an archive that is going to be compressed at the same time (foo.tar.gz). Use: Verbose option.

pi@raspberrypi ~ $ tar -czvf foo.tar.gz foo.txt; ls -l foo*.gz
foo.txt
-rw-r--r-- 1 pi pi 1025 Jul 21 09:38 foo.tar.gz

pi@raspberrypi ~ $

You are about to send a file to Window user. How would you compress/decompress it?

$ zip file/unzip file
$ zip -r directory/unzip directory

There is an external disk mounted in /media/jaro directory. Its name is Seagate Expansion Drive. Crate a 'backup' directory on it and store a directory on it (my case 'Books').

Notice!
The name of a drive contains spaces. You can use quotes or backslashes to enter that disk.

cd /media/jaro/Seagate\ Expansion\ Drive/
rsync -av Books/ /media/jaro/Seagate\ Expansion\ Drive/Backup



Monday, July 20, 2015

Day 21: The Linux Command Line Ch17 Notes


Chapter 17 - Searching for Files

Commands:

locate  - Find files by name.
find - Search for files in a directory hierarchy.

xargs - Build and execute command lines from standard input.
touch - Create/Change file name.
stat - Display file/file system status

Locate utility finds files based on the database stored at:
/var/lib/mlocate/mlocate.db

This database is updated using: 'updatedb' utility.

Find searches a given directory and its subdirectories in order to find the file name. It also supports type of the file (-type f = file, -type -d = directory). For instance:

$ find ~ -type d | wc -l

will find all directories (-type d) in our home directory (~) and will list their number (| wc -l). Find is very powerful. Check for all options in the "The Linux Command Line" (p. 222-224).



Lab 11

  1. Update database locate uses and find all files with 'passwd' name.
  2. Using 'locate' display how many times 'passwd' was found.
  3. In your home directory find all files with ".avi" extenstion and that are 10MB or larger in size.
  4. Find files whose content or attributes were changed in the last 20 minutes.
  5. Find files whose content were changed in the last 2 or less minutes.
  6. Repeat the step 4 and 5 using only one search (hint: group both queries in two logical expressions with an -or)
  7. Which command can follow 'find' with user defined action?
  8. As a bonus follow the instructions as per 'A Return to the Playground' section in the book. It's awesome!
Update database locate uses and find all files with 'passwd' name.
(raspberry pi in my version did not have locate utility installed)

$ sudo apt-get install locate
$ sudo updatedb

Using 'locate' display how many times 'passwd' was found.

$ locate passwd

In your home directory find all files with ".avi" extenstion and that are 10MB or larger in size.

find ~ -type f -name '*.avi' -size +10M

Find files whose content or attributes were changed in the last 2 or less minutes.

$ find ~ -type f -cmin -2

Find files whose content were changed in the last 2 or less minutes.

find ~ -type f -mmin -2

Repeat the step 4 and 5 using only one search (hint: group both queries in two logical expressions with an -or).

find ~ \( -type f -cmin -2 \) -or \( -type f -mmin -2 \)

Which command can follow 'find' with user defined action?

-exec command '{}' ';'
or 
$  | xargs command

for instance:

$ find ~ -name '*.avi' | xargs --null ls -l
Notice!
--null option allows to interpret embedded spaces in file names to be treated as such rather than as space + another command/option

As a bonus follow the instructions as per 'A Return to the Playground' section in the book. It's awesome!


Create 100 directories as per example:

$ cd playground
$ mkdir dir-{00{1..9},0{10..99},100}

Create 100 files in each directory as per example:

dir-{00{1..9},0{10..99},100}/file-{A..Z}

Confirm that 100 file-A have been created (if you are in 'playground directory type the following):

find . -name 'file-A' | wc -l

Create file 'timestamp' for our reference:

$ cd ~
$ touch playground/timestamp
$ stat playground/timestamp

Change times on the file timestamp

$ touch playground/timestamp
$ stat playground/timestamp

Let's update some of the files:

find playground -type f -name 'file-B' -exec touch '{}' ';'

and find newer than timestamp:

find playground -type f -newer playground/timestamp

Finally, let’s go back to the bad permissions test we performed earlier and apply it to playground:

$ find playground \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \)


Friday, July 17, 2015

Day 20: The Linux Command Line Ch16 Notes Cont


Chapter 16 - Networking Continued

Commands:

ftp - Internet file transfer program
wget - Non-interactive network downloader
ssh - OpenSSH SSH client (remote login program)


Lab 8 - FTP
  1. Change directory (you can use your 'playground').
  2. Ensure that you are in the local directory 'playground'
  3. Using CLI, create a connection to any known public ftp server and download any file.
  4. Make sure that you see the download progress.
Lab 8 - Solution


pi@raspberrypi ~ $ ftp ftp.microsoft.com
-bash: ftp: command not found
pi@raspberrypi ~ $ 

Ooops! FTP client is not installed. Let's install it.

pi@raspberrypi ~ $ sudo apt-get install ftp
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  ftp
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 60.6 kB of archives.
After this operation, 140 kB of additional disk space will be used.
Get:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main ftp armhf 0.17-27 [60.6 kB]
Fetched 60.6 kB in 0s (77.1 kB/s)
Selecting previously unselected package ftp.
(Reading database ... 79596 files and directories currently installed.)
Unpacking ftp (from .../archives/ftp_0.17-27_armhf.deb) ...
Processing triggers for man-db ...
Setting up ftp (0.17-27) ...
update-alternatives: using /usr/bin/netkit-ftp to provide /usr/bin/ftp (ftp) in auto mode
pi@raspberrypi ~ $ 

Now, let's try again! I am going to use microsoft ftp server for this lab (user: ftp, password: anything).

pi@raspberrypi ~ $ ftp ftp.microsoft.com
Connected to ftp.microsoft.com.
220 Microsoft FTP Service
Name (ftp.microsoft.com:pi): ftp
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230-Welcome to FTP.MICROSOFT.COM. Also visit http://www.microsoft.com/downloads.
230 User logged in.
Remote system type is Windows_NT.

Now let's display the content of directory on the ftp server:

ftp> ls
200 PORT command successful.
125 Data connection already open; Transfer starting.
04-28-10  07:21PM       <DIR>          bussys
04-28-10  10:17PM       <DIR>          deskapps
04-28-10  11:14PM       <DIR>          developr
04-28-10  11:15PM       <DIR>          KBHelp
04-28-10  11:15PM       <DIR>          MISC
04-29-10  06:54AM       <DIR>          MISC1
04-29-10  08:47AM       <DIR>          peropsys
04-29-10  05:10PM       <DIR>          Products
04-29-10  05:13PM       <DIR>          PSS
04-29-10  05:22PM       <DIR>          ResKit
04-29-10  07:51PM       <DIR>          Services
04-30-10  08:37AM       <DIR>          Softlib
226 Transfer complete.
ftp> 

Let's snoop around in ... ResKit directory.

ftp> cd ResKit
250 CWD command successful.
ftp> ls
200 PORT command successful.
125 Data connection already open; Transfer starting.
04-29-10  05:21PM       <DIR>          bork
04-29-10  05:21PM       <DIR>          IIS4
04-29-10  05:21PM       <DIR>          mspress
04-29-10  05:21PM       <DIR>          nt4
04-29-10  05:22PM       <DIR>          win2000
04-29-10  05:22PM       <DIR>          win98
04-29-10  05:22PM       <DIR>          y2kfix
226 Transfer complete.
ftp> cd y2kfix
250 CWD command successful.
ftp> ls
200 PORT command successful.
150 Opening ASCII mode data connection.
04-29-10  05:22PM       <DIR>          alpha
02-03-00  06:24PM                 5115 readme.txt
04-29-10  05:22PM       <DIR>          x86


Check what's my local directory:

ftp> lcd
Local directory now /home/pi
ftp> 

I need to change my local directory to 'playground'

ftp> lcd playground
Local directory now /home/pi/playground
ftp> 

In order to display download progress I need to use keyword: hash

ftp> hash
Hash mark printing on (1024 bytes/hash mark).

And finally, let's download 'readme.txt' file'

ftp> get readme.txt
local: readme.txt remote: readme.txt
200 PORT command successful.
125 Data connection already open; Transfer starting.
#####
226 Transfer complete.
5115 bytes received in 0.19 secs (26.6 kB/s)
ftp> bye
221 Thank you for using Microsoft products.

The '#####' indicate download progress. With large files you will see a lot of them flying through the screen.



Lab 9 - WGET

  1. Connect to the same server and download the same file using wget.
  2. Use wget to download the file using the following url:

In case you were interested this link has all the chapters:



Lab 9 - Solution




Lab 10 - SSH

Create ssh connection to a computer using CLI.

Lab 10 - Solution