# SA Homework 1 You are asked to write a shell script for each following problem and submit all scripts in a tarball. Each script should be named exactly as specified in the problem description and have its executable permission on. You are allowed to use the standard (POSIX) shell or GNU Bash. If you use the standard shell, the first line of your script must be `#!/bin/sh`. If you use Bash, the first line of your script must be `#!/usr/bin/env bash`. Before submission, make sure you test each script with the `shellcheck` command, available on CSIE workstations. It catches common mistakes and gives useful suggestions. **Update**: We will not run your script against automatic checker; we will read your script one by one and grade it by your understanding of shell scripting. You don't have to treat it like a [Judgegirl](https://judgegirl.csie.org/) problem that disagrees even upon a single character. ## Uptime (25%) `uptime` is a useful command to get an bird's-eye view of the running system. Simulate the `uptime` command with shell script. #### Name ``` my_uptime.sh ``` #### Sample output: ```bash root@ubuntu:~# ./my_uptime.sh # uptime is just 7 hours, 13 minutes 08:12:22 up 7:13, 1 user, load average: 0.00, 0.00, 0.00 root@linux6:~# ./my_uptime.sh 08:14:07 up 114 days, 8:13, 150 users, load average: 9.02, 9.49, 9.51 root@laptop:~# ./my_uptime.sh 08:15:17 up 1 day, 10:22, 7 users, load average: 0.05, 0.10, 0.15 ``` Beware the plurality trivia, i.e. "day" or "days", "user" or "users" and don't forget to check out `man uptime`. **Update**: regarding the format of uptime, please follow these rules: - If less than one minute, print "0 min". - If less than one hour, print "xx min". - If less than a day, don't print "day"; just print "[hour]:[minute]". - The hour field, if less than 10, is padded with one space. - If more than one day, don't forget to add "s" to "day". - You don't have to consider the cases where uptime is more than a week. - For the curious, [here](https://gitlab.com/procps-ng/procps/blob/master/proc/whattime.c#L44-161) is the actual implementation of procps uptime. The following snippets are your building blocks: ``` # Get localtime zdump /etc/local # Get uptime cat /proc/uptime # Get logged-on users who # Get load average cat /proc/loadavg ``` ## Where are you from? (15%) We have many users on CSIE workstations, logging in from different countries, such as the Singapore, Japan, Macau, etc. Find out which country the workstation users are from. The user can be listed using `last -i`, the country name be queried with `geoiplookup` the output can be formatted by `uniq -c` and sorted by `sort -n`. #### Name ``` from.sh ``` #### Sample output: ```bash root@linux6:~# ./from.sh 1 JP, Japan 1 US, United States 2 MO, Macau 8 CN, China 11 HK, Hong Kong 75 SG, Singapore 558 TW, Taiwan ``` ## Phone number checker (10%) These are valid phone numbers: ``` 0912345678 0912-345678 0912-345-678 09xxxxxxxx # where x is a digit 09xx-xxxxxx # likewise 09xx-xxx-xxx # likewise ``` anything other than that is deemed invalid, for example: ``` 0912345-678 ``` If the phone number is valid, print it out. If not, do nothing. #### Name ``` phone_check.sh ``` #### Sample output: ``` ./phone_check.sh 0912-345-678 # Valid so print it out 0912-345-678 ./phone_check.sh 0912-345-6789 # Invalid so do nothing ``` ### (Bonus) NOL parser (10%) Write a script to parse the following [page](https://www.csie.ntu.edu.tw/~yunchih/stuff/nol.html.txt) (directly downloaded from nol.ntu.edu.tw and translated from *Big-5* to *UTF-8*), which is a list of courses offered by graduate institute of CSIE. You are asked to extract the **course name**, **instructor's name**, **course time** in each row, right-padded with spaces in a width of 50, 20, 10, respectively (See **Update** below). Since you are probably an undergraduate student uninterested in *Seminar*, *Special Project* and *Thesis*, if the script is supplied with the **-g** argument, exclude courses whose name **contains** those strings. Furthermore, if some field is missing (shown as `&nbsp` in HTML), you should substitute it with `N/A`. **Update**: you can directly use the following command to format the output: ```bash printf "%-50s\t%-20s\t%-10s\n" "$name" "$professor" "$time" ``` #### Name ``` nol_parser.sh ``` #### Sample output: ``` ./nol_parser.sh -f input_file.html Thesis (Ph.D.) N/A N/A Seminar CHANG RUEY-FENG N/A Special Project JA-LING WU N/A Special Project LI-CHEN FU N/A Special Project WEN-CHIN CHEN N/A Special Project GEN-HUEY CHEN N/A Special Project JIEH HSIANG N/A Special Project LAI, FEI-PEI N/A Special Project HSIN-HSI CHEN N/A Special Project MING OUH YOUNG N/A Special Project OYANG, YEN-JEN N/A Special Project YUNG-JEN HSU N/A Special Project CHIOU-SHANN FUH N/A Special Project YUH-DAUH LYUU N/A Special Project N/A N/A Special Project CHIH-JEN LIN N/A Special Project TEI-WEI KUO N/A Special Project PANGFENG LIU N/A Special Project PHONE LIN N/A Special Project CHIA-LIN YANG N/A Special Project YI-PING HUNG N/A Special Project CHAO, KUN-MAO N/A Special Project CHENG-FU CHOU N/A Special Project AI-CHUN PANG N/A Special Project CHI-SHENG SHIH N/A Special Project YUNG-YU CHUANG N/A Special Project SHIH-HAO HUNG N/A Special Project HSUEH-I LU N/A Special Project XUE ZHI-WEN N/A Special Project CHANG RUEY-FENG N/A Special Project TSENG Y. JANE N/A Special Project CHENG PU-JEN N/A Special Project LIN SHOU-DE N/A Special Project LI MING-SUI N/A Special Project SHU HONG-MING N/A Special Project STEVEN LIAO N/A Special Project BING-YU CHEN N/A Special Project HSUAN-TIEN LIN N/A Special Project HSIN-MU (MICHAEL) TSAI N/A Special Project MIKE Y. CHEN N/A Special Project JONATHAN LEE N/A Special Project JYH-SHING ROGER JANG N/A Special Project WEI-CHUNG HSU N/A Special Project HSU-CHUN HSIAO N/A Special Project TONY TAN N/A Special Project YUN-NUNG CHEN N/A Thesis (M.S.) N/A N/A Special Project LIN-SHAN LEE N/A Special Project JA-LING WU N/A Special Project LI-CHEN FU N/A Special Project WEN-CHIN CHEN N/A Special Project GEN-HUEY CHEN N/A Special Project JIEH HSIANG N/A Special Project LAI, FEI-PEI N/A Special Project HSIN-HSI CHEN N/A Special Project MING OUH YOUNG N/A Special Project OYANG, YEN-JEN N/A Special Project YUNG-JEN HSU N/A Special Project CHIOU-SHANN FUH N/A Special Project YUH-DAUH LYUU N/A Special Project N/A N/A Special Project CHIH-JEN LIN N/A Special Project TEI-WEI KUO N/A Special Project PANGFENG LIU N/A Special Project PHONE LIN N/A Special Project CHIA-LIN YANG N/A Special Project YI-PING HUNG N/A Special Project CHAO, KUN-MAO N/A Special Project CHENG-FU CHOU N/A Special Project AI-CHUN PANG N/A Special Project CHI-SHENG SHIH N/A Special Project YUNG-YU CHUANG N/A Special Project SHIH-HAO HUNG N/A Special Project HSUEH-I LU N/A Special Project XUE ZHI-WEN N/A Special Project CHANG RUEY-FENG N/A Special Project TSENG Y. JANE N/A Special Project CHENG PU-JEN N/A Special Project LIN SHOU-DE N/A Special Project LI MING-SUI N/A Special Project SHU HONG-MING N/A Special Project STEVEN LIAO N/A Special Project BING-YU CHEN N/A Special Project HSUAN-TIEN LIN N/A Special Project HSIN-MU (MICHAEL) TSAI N/A Special Project MIKE Y. CHEN N/A Special Project JONATHAN LEE N/A Special Project JYH-SHING ROGER JANG N/A Special Project WEI-CHUNG HSU N/A Special Project HSU-CHUN HSIAO N/A Special Project TONY TAN N/A Special Project YUN-NUNG CHEN N/A Seminar SHU HONG-MING N/A Seminar LI MING-SUI N/A Advanced Operating System CHI-SHENG SHIH Thu 7,8,9 Brain Theory CHENG-YUAN LIOU Wed 7,8,9 Performance Modeling CHENG-FU CHOU Wed 7,8,9 Performance Modeling TAY YOUNG CHIANG Wed 2,3,4 Principles of Financial Computing YUH-DAUH LYUU Wed 2,3,4 Numerical Methods CHIH-JEN LIN Mon 2,3,4 Natural Language Processing HSIN-HSI CHEN Thu 2,3,4 Software Engineering Design JONATHAN LEE Tue 6,7,8 Information Theory and Coding Techniques JA-LING WU Mon 7,8,9 Applied Algebra WEN-CHIN CHEN Tue 7,8,9 Advanced Compiler Design WEI-CHUNG HSU Mon A,B,C Advanced Computer Networks AI-CHUN PANG Mon 7,8,9 Advanced Computer Architecture CHIA-LIN YANG Tue 7,8,9 Parallel Programming PANGFENG LIU Tue 7,8,9 Multimedium Computing Environment CHANG RUEY-FENG Wed 3,4,5 Virtual Reality MING OUH YOUNG Mon 7,8,9 Medical Information System LAI, FEI-PEI Tue 6,7,8 Cryptography and Network Security HSU-CHUN HSIAO Wed 7,8,9 Digital Visual Effects YUNG-YU CHUANG Wed 7,8,9 An Introduction to Advanced Performance Modeling PHONE LIN Tue 7,8,9 Game Programming MIKE Y. CHEN Tue 7,8,9 Web Retrieval and Mining CHENG PU-JEN Fri 2,3,4 Technical Writing and Research Method LIN SHOU-DE Tue 7,8,9 Introduction to Digital Speech Processing LIN-SHAN LEE Wed 2,3,4 Embedded Operating System Implementation XUE ZHI-WEN Thu 7,8,9 Advanced Human Computer Interaction MIKE Y. CHEN Tue 2,3,4 Next-generation Wireless Networks AI-CHUN PANG Tue 2,3,4 Gpu Programming WEI-CHAO CHEN Mon A,B,C Advanced Computer Vision CHIOU-SHANN FUH Tue 7,8,9 Metabolomics TSENG Y. JANE Thu 6,7,8 Topics in Machine Learning CHIH-JEN LIN Tue 2,3,4 Advanced Topics in Systems Research SHIH-HAO HUNG Mon 7,8,9 Location Based Technologies,Applications and Services CHENG-HONG CHO Mon 7,8,9 Big Data Systems STEVEN LIAO Fri A,B,C Virtual Machines WEI-CHUNG HSU Thu 7,8,9 Intelligent Conversational Bot YUN-NUNG CHEN Tue 2,3,4 Machine Learning Techniques HSUAN-TIEN LIN Tue 5,6 Introduction to Communication Complexity TONY TAN Mon 2,3,4 Net Arts N/A Wed 7,8,9 Music Signal Analysis and Retrieval JYH-SHING ROGER JANG Mon 2,3,4 ``` ``` ./nol_parser.sh -f input.html -g Advanced Operating System CHI-SHENG SHIH Thu 7,8,9 Brain Theory CHENG-YUAN LIOU Wed 7,8,9 Performance Modeling CHENG-FU CHOU Wed 7,8,9 Performance Modeling TAY YOUNG CHIANG Wed 2,3,4 Principles of Financial Computing YUH-DAUH LYUU Wed 2,3,4 Numerical Methods CHIH-JEN LIN Mon 2,3,4 Natural Language Processing HSIN-HSI CHEN Thu 2,3,4 Software Engineering Design JONATHAN LEE Tue 6,7,8 Information Theory and Coding Techniques JA-LING WU Mon 7,8,9 Applied Algebra WEN-CHIN CHEN Tue 7,8,9 Advanced Compiler Design WEI-CHUNG HSU Mon A,B,C Advanced Computer Networks AI-CHUN PANG Mon 7,8,9 Advanced Computer Architecture CHIA-LIN YANG Tue 7,8,9 Parallel Programming PANGFENG LIU Tue 7,8,9 Multimedium Computing Environment CHANG RUEY-FENG Wed 3,4,5 Virtual Reality MING OUH YOUNG Mon 7,8,9 Medical Information System LAI, FEI-PEI Tue 6,7,8 Cryptography and Network Security HSU-CHUN HSIAO Wed 7,8,9 Digital Visual Effects YUNG-YU CHUANG Wed 7,8,9 An Introduction to Advanced Performance Modeling PHONE LIN Tue 7,8,9 Game Programming MIKE Y. CHEN Tue 7,8,9 Web Retrieval and Mining CHENG PU-JEN Fri 2,3,4 Technical Writing and Research Method LIN SHOU-DE Tue 7,8,9 Introduction to Digital Speech Processing LIN-SHAN LEE Wed 2,3,4 Embedded Operating System Implementation XUE ZHI-WEN Thu 7,8,9 Advanced Human Computer Interaction MIKE Y. CHEN Tue 2,3,4 Next-generation Wireless Networks AI-CHUN PANG Tue 2,3,4 Gpu Programming WEI-CHAO CHEN Mon A,B,C Advanced Computer Vision CHIOU-SHANN FUH Tue 7,8,9 Metabolomics TSENG Y. JANE Thu 6,7,8 Topics in Machine Learning CHIH-JEN LIN Tue 2,3,4 Advanced Topics in Systems Research SHIH-HAO HUNG Mon 7,8,9 Location Based Technologies,Applications and Services CHENG-HONG CHO Mon 7,8,9 Big Data Systems STEVEN LIAO Fri A,B,C Virtual Machines WEI-CHUNG HSU Thu 7,8,9 Intelligent Conversational Bot YUN-NUNG CHEN Tue 2,3,4 Machine Learning Techniques HSUAN-TIEN LIN Tue 5,6 Introduction to Communication Complexity TONY TAN Mon 2,3,4 Net Arts N/A Wed 7,8,9 Music Signal Analysis and Retrieval JYH-SHING ROGER JANG Mon 2,3,4 ``` ```bash ./nol_parser.sh -f /proc/meow.html Input file not found ./nol_parser.sh Missing input file ``` You don't need to consider other pages on NOL, just parse the one provided.