Ondřej Plátek Blog
PhD candidate@UFAL, Prague. LLM & TTS evaluation. Engineer. Researcher. Speaker. Father.

Week 10, Intro to Unix lab 2016

Content

  • awk
    • BEGIN, END, ‘re {}
    • built-in variables IFS, OFS, NR, RS, FS, FILENAME
  • nicely formatted lines with awk: seq 10 | awk '/.*/ { printf "%4d\n", $1 ; }'
  • print histogram: seq 10 | awk '/.*/ { for(i=0; i< $1; i++) { printf "=" ; } printf "\n" }
  • correctly matching brackets - todo rewrite example
  • computing variance if we have each number on one row
  • basename, dirname

Homework

  • (0 point) - Recap - test command
  • (1 point) Implement in shell basename without options
    • submit it in format basename_YOURNICKNAME.sh
    • Example usage. In this case basename output and $USER are identical
      • cd ~ ; echo $(basename pwd/) $USER
      • Note that the original basename discards trailing / for directories
  • (2 point) Use awk to distribute space evenly in a file.
    • Distribute spaces evenly for each line
    • Do not change words on lines
    • Example input
input file
longer line with more words
      input      file      
longer line with more words

Bonus

  • (2 point) - Write a script ancestor_YOURNICKNAME.sh PID1 PID2 [PID] ...
    • which takes at least two arguments PID1 and PID2
      • which must be numbers and must be running process
      • If argument is not number write an error message and exit with 1
      • If process with PID of an argument is not running write error message and exit with 2
    • and prints a numerically sorted list of pairs of PIDs together with closest common ancestor process pid
      • format per row:
        • PID_A PID_B PID_COMMON_A_B
      • Example output where processes has very distant ancestor init process
        • 1233 1234 1