Solutions

Latest update 09. marts 2009 by  Torben Gregersen

 Exercise PC-1 1.

 The command:

 #man man

 shows that the man-command with option -k and argument "xxx" 
 displays the manuals that contains the text xxx.

 An example:

 man -k login name

 displays the names of manuals that descripe the term 
 "login name".

 This leads to this Linux-command: 
 
 $logname
 tg
 $

 (The apropos command can lead to the same result)

-----------------------------------------------------------------
 2.

 $date +"%r on %A %d %B %Y"
 09:40:52 AM on Friday 02 February 2007
 $

-----------------------------------------------------------------
 3.

 The script loggedin contains:

 
#!/bin/bash
 echo The following are logged in:
 who

 The script is executable for the user by use of this command:

 $chmod u+x loggedin

 The script is executed in this way:

 $./loggedin
 The following are logged in:
 tg   pts/0   Feb  1  09:47  
 tg   pts/1   Feb  1  09:47
 $

-----------------------------------------------------------------
 4.

 vi in colon-state:
 
 :1,$s/cision/vision/g

 Search and replace (s) from first line (1) to last line ($).
 Change cision to vision (this is by default only changed for
 the first occurence in a line.
 All occurences of cision is changed by the global-option (g)  

 A little difficult - but the vi-editor is the editor you can
 use in the LIAB-equipment (or other non-graphical, embedded
 Linux-platforms.

-----------------------------------------------------------------
 5.

 
#!/bin/bash
 ls -alt | head-4 | tail -3    (displays all files in details)
 
 or

 
#!/bin/bash
 ls -1at | head-3   (displays only the filnames)
 
 

 Exercise PC-2  1.  

 $ls -li | sort

 displays the content of a directory, sorted by i-nodes
 in increasing order.

-----------------------------------------------------------------
 2.

 This script prints the owner of the present directory:

 #!/bin/bash
 ls -al | head -2 | tail -1 | cut -f4 -d' '

 or
 
#!/bin/bash
 ls -ld . | cut -f4 -d'

-----------------------------------------------------------------
3.

 A script that prints files in /bin og /usr/bin with
 the same name, can be written as:

 #!/bin/bash
 ls -1 /bin > file
 ls -1 /usr/bin >> file
 sort file > sortedfile
 echo Duplicated files in /bin and /usr/bin :
 uniq -d sortedfile
 rm file sortedfile

 #!/bin/bash
 echo Duplicated files in /bin and /usr/bin :
 ls -1 /bin /usr/bin | sort | uniq -d

 or - again - without temporary files:

 
#!/bin/bash
 for i in /usr/bin; do
   ls /bin | grep $i 
 done                

-----------------------------------------------------------------
4.

 This script displays files in the present directory, sortet by
 size (the smallest files are displayed first).

 
#!/bin/bash
 ls -lSr
 
 or
 
 
#!/bin/bash
 ls -l | sort -n +4

-----------------------------------------------------------------
5.

 Displays, without duplication, the groups who owns the
 files in the present directory:

 $ls -lg | cut -f4 -d' ' | sort | uniq
 

 Exercise PC-3 1.

 The script can be written as:

 
#!/bin/bash
 echo "Your username is $LOGNAME"
 echo "Home directory is $HOME"
 echo "You are using terminal $TERM"

-----------------------------------------------------------------
2.

 The script can be written as:

 
#!/bin/bash
 echo "Enter directory: "
 read DIRECTORY
 echo "The content of the $DIRECTORY is: "
 ls -l $DIRECTORY

-----------------------------------------------------------------
3.

 It is necessary to use quotes ("") to descripe a space between
 two names:

 The content of the variable will be stored for later user by
 writing export:

 #export MY_NAME="Torben Gregersen"

-----------------------------------------------------------------
4.

 A script same_name, that displays files in two different
 directories with the same name. The directories are entered
 by the user as parameters:

 The script is for example used in this way:
 #./same_name /bin /usr/bin 

 A script-solution is:

 
#!/bin/bash
 echo Duplicated files in $1 and $2 :
 ls -1 $1 $2 | sort | uniq -d 

-----------------------------------------------------------------
5.

 This command displays all regular files in the home directory,
 where the size is smaller than 500 bytes:

 #find ~ -type f -size -500c -print

-----------------------------------------------------------------
6.

 This command find the longest word in a dictionary with many
 thousands words:

 #wc -L /usr/share/dict/words
 
-----------------------------------------------------------------
7.

 This command finds the name of all regular files in the
 current directory - including subdirectories - and prints
 the first line of the file:

 #find . -type f -print -exec head 1 {}\;

-----------------------------------------------------------------
8.

 This script counts all files with a user-entered name.
 The files are counted in the current directory, including
 all subdirectories:

 An example: A programmer want to see how many .c-files that
 are placed in the current directory, including all
 subdirectories:

 #./count_files / *.c
 1521

 A script solution is:

 
#!/bin/bash
 find $1 -name $2 | wc -l 
 
 Exercise PC-4  1.

  - - - - -
-----------------------------------------------------------------
 2.


                                                      
 //                                                   
 // file: search_table.cpp                            
 //                                                   

                                                      
 #include <iostream>                                  
 using namespace std;                                 
 const int SIZE = 100000;                             
                                                      
 int main()                                           
 {                                                    
    int array[SIZE];                                  
    int i;                                            
    int minValue;                                     
    int maxValue;                                     
                                                      
    cout << "Array-search (C++ application)" << endl; 
                                                      
    for (i = 0; i < SIZE; i++)                        
       array[i] = 0;                                  
    array[0] = 16;                                    
    array[SIZE-1] = -12;                              
    minValue = array[0];                              
    maxValue = array[0];                              
                                                      
    for (i = 1; i < SIZE; i++)                        
    {                                                 
       if (array[i] < minValue)                       
          minValue = array[i];                        
       if (array[i] > maxValue)                       
          maxValue = array[i];                        
    }                                                 
                                                      
   īcout << "Result of search:" << endl;              
    cout << "minValue = " << minValue << endl;        
    cout << "maxValue = " << maxValue << endl;        
    return 0;                                         
 }                                                    
                                                      


Compile-command for smallest possible memory usage:

 #g++ -Os search_table.c -o search_table

 This results in an executable file named search_table with
 size: 6740 bytes

C++ program:
 - optimized for smallest memory usage:    28 msec (filesize 6740)
 - optimized for fastest execution (-O2):  24 msec (filesize 6709)
 - optimized for fastest execution (-O3):  22 msec (filesize 6805)

 Try to optimize with -O2 and -O3 to get the fastest
 execution of the applications (unpredictable)


-----------------------------------------------------------------
 3.

                                                      
 //                                                   
 // file: search_table.cpp                            
 //                                                   

                                                      
 #include <iostream>                                  
 using namespace std;                                 
 const int SIZE = 100000;                             
                                                      
 int main()                                           
 {                                                    
    int array[SIZE];                                  
    int i;                                            
    int minValue;                                     
    int maxValue;                                     
                                                      
    cout << "Array-search (C++ application)" << endl; 
                                                      
    for (i = 0; i < SIZE; i++)                        
       array[i] = 0;                                  
    array[0] = 16;                                    
    array[SIZE-1] = -12;                              
    minValue = array[0];                              
    maxValue = array[0];                              
                                                      
    for (i = 1; i < SIZE; i++)                        
    {                                                 
       if (array[i] < minValue)                       
          minValue = array[i];                        
       if (array[i] > maxValue)                       
          maxValue = array[i];                        
    }                                                  
                                                      

 Compile-command for smallest possible memory usage:

 #g++ -Os search_table.c -o search_table

 This results in an executable file named search_table with
 size: 6740 bytes


-----------------------------------------------------------------
 4.

 Results of measurement of execution time


 Command for time measurement:
 #time ./search_table

 Command for file-size measurement:
 #ls -l search_table

 C program:
 - optimized for smallest memory usage:    28 msec (filesize 4956)
 - optimized for fastest execution (-O2):  26 msec (filesize 4968)
 - optimized for fastest execution (-O3):  17 msec (filesize 4968)

-----------------------------------------------------------------
 5.

You can find ghex at this link:
 http://mail.gnome.org/archives/gnome-announce-list/2004-May/msg00037.html

 when the downladed tarball is decompressed and dearchived the
 following commands are executed:

 #./configure
 #make
 #make install

 The new "Gnome Hex-Editor" is executed by this command:
 #ghex2 <any regular file as argument>

 It is now possible to view and modify the regular file by usin ghex2
-----------------------------------------------------------------
 6.

 
- - - - -
-----------------------------------------------------------------
 7.

  - - - - -
 

 Exercise LIAB-1  1.

Connect PC and LIAB via null-modem


 PC:    
#minicom -s      (setup, /dev/ttyS0, 115200 N81)

 
PC:     #minicom         (normal use)

 LIAB:   reset

-----------------------------------------------------------------
 2.

 PC:     #ifconfig eth0 192.168.1.101/24 up
         #ping 192.168.1.180


-----------------------------------------------------------------
 3.

 - - -
-----------------------------------------------------------------
 4.

 - - -
-----------------------------------------------------------------
 5.

 A solution is to write this in the file /root/.bash_profile:

   PATH=$PATH:$HOME/bin
   PATH=$PATH:/opt/crosstool/arm-softfloat-linux-gnu/gcc-3.3.2.klibc-2.3.2/bin

  To activate the new PATH it is necessary to logout and login again.

-----------------------------------------------------------------
 6.

  The file hello_arm.c is placed at the LIAB-ARM Distribution
  CD-ROM, in this directory:

 
software/crosscompiler/hello_arm/hello_arm.c

-----------------------------------------------------------------
 7.

 C program, results:
 - optimized for smallest memory usage:    38 msec (filesize 8874)
 - optimized for fastest execution (-O2):  38 msec (filesize 8874)
 - optimized for fastest execution (-O3):  42 msec (filesize 8866)

-----------------------------------------------------------------
 8.

 Find the regular file libstdc++.so.5 under /opt in the
 PC-platform.

 Transfer this file to the directory /lib in the LIAB.
 
 C++ program, results:
 - optimized for smallest memory usage:    72 msec (filesize 10868)
 - optimized for fastest execution (-O2):  72 msec (filesize 10868)
 - optimized for fastest execution (-O3):  76 msec (filesize 10868)

-----------------------------------------------------------------
 9.

  Place the file libstdc++.so.5 in directory /jffs2/root/lib

  Place a file named .rhosts with this content:
    192.168.1.101 root
    192.168.1.101 liab
 
in directory /jffs2/root/root

  The file libstdc++.so.5 is automatically copied to /lib at LIAB boot.
  The file .rhosts is automatically copied to /root at LIAB boot.
 
-----------------------------------------------------------------
 10.

The content of the file write_to_ttyS0.cpp is (for example):

                                                              
 //                                                           
 // file: write_to_ttyS0.cpp                                  
 //                                                           
                                                              

 #include <fcntl.h>                                           
 #include <iostream>                                          
 using namespace std;                                         
                                                              
 const int SIZE = 6;                                          
 const int MAX = 9999;                                        
                                                              
 int main()                                                   
 {                                                            
   char array[SIZE];                                          
   int i;                                                     
   int fd;                                                    
                                                              
   cout << "write_ttyS0 (C++ application)" << endl;           
                                                              
   fd = open ("/dev/ttyS0", O_RDWR); // open character device 
   if (fd < 0)                                                
   {                                                          
     cout << "error opening /dev/ttyS0" << endl;              
     exit(0);                                                 
   }                                                          
  for (i = 0; i <= MAX; i++)                                  
   {                                                          
     sprintf(array, "%04d\n", i);                             
     write(fd, array, 5); // use character device             
     sleep(1);                                                
   }                                                          
                                                              
   close(fd); // close character device                       
                                                              
   cout << "Finished" << endl;                                
   return 0;                                                  
 }                                                            

 

 Exercise LIAB-2
 
This exercise is YOUR work :-)
 
 Exercise LIAB-3
 This exercise is YOUR work :-)
 
 Exercise LIAB-4
 This exercise is YOUR work :-)
 
 Exercise LIAB-5
  This exercise is YOUR work :-)
 
  Exercise LIAB-6
  This exercise is YOUR work :-)
 
  Exercise LIAB-7
 This exercise is YOUR work :-)