Thursday, February 16, 2017

Grep and Regex

My Linux Learning Path

Do Linux to learn Linux. Use VIM and create a file named Id.txt with the following (CC, DNI, LIC; are Id denominations):

Id Name
Mauricio CC 71.989.867
Nury CC 98.001.023
Jhon LIC 123.456.874
Mike DNI 53.721.321-X
Alvaro DNI 75.342.123-S
Jose LIC 453.750.884

Now let´s play:
  1. Find Persons with: CC document Id.
  2. Find Persons with: DNI document Id.
  3. Find Persons with: LIC document Id.
  4. Find Persons with: CC, DNI document Id.
  5. Find Persons with: CC, LIC document Id.
  6. Find Persons with: LIC, DNI document Id.
  7. Define a regex pattern for DNI and use it with grep to find all DNI´s.
  8. Define a regex pattern for LIC and use it with grep to find all LIC´s.
  9. Define a regex pattern for CC and use it with grep to find all CC´s.
  10. Another way to identify DNI Id is to find lines that end with a letter. Use regex and grep for this purpose.
  11. Find names with 4 and 5 letters.
  12. Find if any Id repeat a number 2 times.

Arithmetic Expressions

My Linux Learning Path

Do Linux to learn Linux.

  1. Define a variable (var1) equal to 3.141516 and print var1.
  2. Define var2=10*var1. Print var2.
  3. Define var3=var1+var2. Print var3.
  4. Define a script and use parameter arguments (two float numbers only) and print:
    • Increase by 0.98 the first value.
    • Increase by one previous result.
    • Increase by 18.0123 the second value.
    • Decrease by one previous result.
    • Addition.
    • Subtraction.
    • Multiplication.
    • Division.
    • Modulo.
    • First value raised to the second value.
  5. Repeat previous exercise but this time assign the result to a fictitious variable. Use parameter expansion to create the variables.
  6. Double parenthesis (arithmetic expansion) deals only with integers. In terminal solve the following:
    • Define var3=12
    • Define var4=31
    • Print var3 + var4.
    • Print var3 - var4.
    • Increase by 1 and print var3
    • Decrease by 1 and print var4
  7. Create a script that ask user to insert tow integer numbers (only) and perform previous operations.
  8. Explain how the following works:
    • Want to get 5.
      (( var1=$1 ))
      (( var2=$2 ))
      
      (( var1 ++ ))
      (( var2 -- ))
      
      echo "Var1 + Var2 is: $(( var1 + var2 )) 
      
    • If var1=2 and var2=2 then result is:
      #!/bin/bash
      
      echo -e "Define var1: \c"
      read var1
      
      echo -e "Define var2: \c"
      read var2
      
      (( var1+=1 ))
      (( var2=var2+3 ))
      
      echo "Var1 + Var2 : $(( var1 + var2 ))"
      
    • This code calculate the present value of a zero coupon bond. Take this as an example and perform similar scripts to get use to it.
      #!/bin/bash
      
      ERROR_PARAM=3
      
      echo -e "Define Bond Principal: \c"
      read fv # Must be positive
      
      echo -e "Define intereset rate: \c"
      read int  # Must be positive and belongs to (0-1)
      
      echo -e "Define maturity: \c"
      read n  # Must be integer
      
      #Performing test
      if [[ fv -lt 0 ]]; then
              echo "Error: Negative Future Value."
              exit $ERROR_PARAM
      fi
      
      if [[ $(echo "$int < 0.0" | bc -l) -eq 1 ]] || [[ $(echo "$int > 1.0" | bc -l) -eq 1 ]]; then
              echo "Error: Interest range out of bounds."
              exit $ERROR_PARAM
      fi
      
      if [[ n -lt 0 ]]; then
              echo "Error: Negative maturity."
              exit $ERROR_PARAM
      fi
      
      rate=$(echo "(1.0+$int)" | bc -l)
      discount=$(echo "$rate^$n" | bc -l)
      pv=$(echo "scale=4; ( $fv / $discount )" | bc -l)
      
      echo "Bond price is: $pv"
      

Files Mangement

My Linux Learning Path.

Do Linux to learn Linux (via exercises).

  1. In Desktop create a directory with the name: "Test".
  2. Set root the working directory. From here create two additional directories: "Data", "Shell" in "Test" directory created previously.
  3. Set root the working directory. Create 3 txt file (file1.txt, file2.txt, file3.txt) in "Data" directory. Use expansion expression to create the files.
  4. Set "~/Desktop/Test/Shell/" the working directory. Create file "set_file1.sh" and define a script inside that insert numbers from 1 to 10 in "file1.txt"
  5. Execute previous script. If problems use ls -li to identify permissions and make the necessary corrections.
  6. Set "~/Desktop/Test/" the working directory.
    • Use ls -li command to list tree of items found. Redirect the result to file2.txt using standard output channels.
    • Use ls -li command to list tree of items found. Redirect the result to file3.txt using tee command.
    • Use ls -li command to list tree of items found. Append the result to file4.txt.
  7. Set ~/Desktop/ the working directory. Use ls and grep command (with pipe) to identify files with extension: txt.
  8. Repeat previous exercise but this time sort the output by file name in reverse order.
  9. Repeat previous exercise but this time use tee command to copy the output to files: file_list1.txt, file_list2.txt.
  10. Use cat, less and pipe to identify the first 25 commands that appears in $HISTFILE.
  11. Identify the last 15 command that appears in $HISTFILE.Print them in the screen and save it in a txt file.
  12. ls -lc sort the result by filename. Use ls -li and sort command to achieve the same result.
  13. ls -lt sort the result by time modification (according to man ls). Use ls -li and sort command and order the file by month, date, hour.
  14. Create a file Students.doc (use vim) with the following information:
    Javier; Mauricio; 34
    Alvaro; Raga; 54
    Elizabeth; Alvarez; 35
    Sandra; Alvarez; 35
    Luz; Osorio; 62
    Sandra; Lopez; 21
    Next, identify:
    • Sort data by first column.
    • Sort Data by first and second column
    • Create a document that contains columns 1 and 3 only.
    • Create a document that contains columns 2 and 3 only, and change separator to ;.

Archiving and Compressing Files

My Linux Learning Path

Most persons have difficulties with tar because ignores options order. File option f must be the last. Do Linux to learn Linux. Remember that if you don´t remember an option, use man tar in terminal.
Set ~/Desktop/ the current directory. Next, create two folders: Part1 (files: File1, File2, File3) and Part2 (files: File4, File5, File6), considering arbitrary content in each file.

  1. Create a tar file Program.tar with no content. Any Error?
  2. Create a tar file Program1.tar with folder Part1 only.
  3. Create a tar file Program2.tar with folders Part1 and Part2.
  4. Append folder Part2 to Program1.tar.
  5. Modify File1 and identify how to update Program1.tar file.
  6. Identify the content of Program2.tar on the screen.
  7. Untar Program1.tar to folder ~/Desktop/MyPrograms/
  8. Compress Program1.tar. using gzip.
  9. Create a compress tar file Program3.tar.gz that contains folders Part1 and Part2.
  10. Repeat previous exercise but this time use bzip2.
  11. Compress Program2.tar using gzip.
  12. Compress Program2.tar using bzip2.
  13. Decompress previous two files to different folders.

Access Control

MyLinux Learning Path

Do Linux to learn Linux. Set current directory to: ~/Desktop/. File and group owner is javierma36.

  1. Create a txt file (Code1.txt) using touch command. Identify and print separately (user, group, other) the default access control assigned.
  2. Create a directory (Delta_Proyect) using mkdir command. Identify and print separately (user, group, other) the default access control assigned.
  3. What does it means: xrw access control.
  4. Change user access control permissions of Code1.txt to: x-- and try to open the file.
  5. What does it means: $ chmod u-x Code1.txt ?
  6. What does it means: $ chmod ug+w Code1.txt ?
  7. What does it means: $ chmod a+x Code1.txt ?
  8. What does it means: $ chmod 444 Code1.txt ?
  9. What does it means: $ chmod 111 Code1.txt ?
  10. What does it means: $ chmod 222 Code1.txt ?
  11. What does it means: $ chmod 666 Code1.txt ?
  12. What does it means: $ chmod 777 Code1.txt ?
  13. What does it means: $ chmod 600 Code1.txt ?
  14. Is this possible: chmod 777 Code1.txt Delta_Proyect/ ?
  15. Imagine that user "alex" exists. Is this possible: $ chown alex Delta_Proyect/ ? Who is the group owner ?
  16. Imagine that user "alex" exists. Is this possible: $ sudo chown alex Delta_Proyect/ ? Who is the group owner ?
  17. Imagine that user "alex" exists. Is this possible: # chown alex Delta_Proyect/ ? Who is the group owner ?
  18. Imagine that user "alex" exists. Is this possible: $ chown alex:alex Code1.txt/ ? Who is the user and group owner now.?
  19. Imagine that user "alex" exists. Is this possible: $ sudo chown alex:alex Code1.txt/ ? Who is the user and group owner now.?
  20. Imagine that user "alex" exists. Is this possible: # chown alex:alex Code1.txt/ ? Who is the user and group owner now.?
  21. Imagine that user "alex" exists. Is this possible: # chgrp alex Code1.txt/ ? Who is the user and group owner now.?
  22. Imagine that user "alex" exists. Is this possible: $ chgrp alex Code1.txt/ ? Who is the user and group owner now.?
  23. Imagine that user "alex" exists. Is this possible: $ sudo chgrp alex Code1.txt/ ? Who is the user and group owner now.?

If..then Exercises

My Linux Learning Path

Do Linux to learn Linux. Create a directory in Desktop for this purpose.

  1. Use Bash exit parameter to assert the following condition test:
    • 3 >= 6
    • 2 = 7
    • 56 <= 12
    • (10^3) = 1000
  2. Create a script that accept two parameters (only) and use if .. then to assert previous relations (one script for relation). Script output must be 0 and 1 (true or false).
  3. In the previous exercises when script file is created, permission does not allow execution. Before changing permission use if .. then control flow to assert if file is: readable, executable and writable.
  4. Write a script that request two numbers: num1, num2. Next identify if:
    • num1 = num2
    • num1 > num2
    • num1 < num2
  5. write a script that request a number: num1. Next identify if:
    • num is positive and greater than 10.
    • num is positive and greater than 20.
    • num is negative and greater than -10.
    • num is negative and greater than -20.
    • num belongs to range [-10,7].
    • num belongs to range [-10,7).
    • num belongs to range (-10,7].
    • num belongs to range (-10,7).
  6. Identify the meaning of the following file test operators. Write an example of each in the console. Next Write a script that accept one (only) input parameter and identify how to use each operator:
    • -e ________________________.
    • -f ________________________.
    • -d ________________________.
    • -s ________________________.
    • -w ________________________.
    • -r ________________________.
    • -p ________________________.
  7. Identify how to mix previous file operators. For example test if xxxx.sh is executable and empty.
  8. Match Integer comparison operator
    • -eq           !=
    • -ne           <
    • -gt           >=
    • -lt           <=
    • -ge           >
    • -le           =
  9. Provide 3 examples of Nested if .. then condition test.
  10. Create a nested if then condition test considering. For this purpose create a script that request a number and test:
    • Number is positive or negative.
    • Number belongs to range [1,10] or [-1,-10].
    • Number * 3 belongs to range [10,20] or [-15,-40].