CS288 Intensive Programming in Linux - Course Updates and Homework Review

Slide Note
Embed
Share

In CS288 Intensive Programming in Linux, Professor C.F. Yurkoski shared announcements regarding TA contact info, a sneak peek at upcoming quizzes, and file editing tips. The course also covered evaluation quiz results and practical exercises using tools like vi and tr. Homework assignments focused on script writing, including a Bash script to analyze commands in /bin directory. Alternative approaches were discussed for the homework task, encouraging student exploration. The course emphasized practical skills in Linux programming.


Uploaded on Oct 08, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Cs288 Intensive Programming in Linux Instructor: C F Yurkoski Christopher.f.yurkoski@njit.edu Section web site: https://web.njit.edu/~yurkowsk/cs288.html 6-9-15 1

  2. 6-9-15 2

  3. announcements 1. You also send mail to the TA if you have questions: KSHITIJ RANGANATH kr347@njit.edu 2. I ll save some time at the end of class today for question. 3. Bring your laptop next week, we ll have a surprise programming quiz. 6-9-15 3

  4. Evaluation quiz results 8.9 out of 15 (59%) (two students with same name on quiz). 6-9-15 4

  5. editing files for use on Linux use vi on Linux Or use tool like notepad++ if you must use Windows. use tr to strip \r indent your code properly! 6-9-15 5

  6. more tr Usage: tr [OPTION]... SET1 [SET2] Translate, squeeze, and/or delete characters from standard input, writing to standard output. -c, -C, --complement use the complement of SET1 -d, --delete delete characters in SET1, do not translate -s, --squeeze-repeats replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character -t, --truncate-set1 first truncate SET1 to length of SET2 6-9-15 6

  7. cat filecontaintsreturn | tr -d "\r" > filecontaintsnoretrun 6-9-15 7

  8. Homework 1 revu Write a Bash script, count.sh, which builds a table of counters for the commands under /bin in alphabetical order. a 9 ... ... ... z 11 Use loop and array to design and implement this script. ls /bin > /tmp/x letters=(a b c d e f g h i j k l m n o p q r s t u v w x y z) for i in ${letters[*]} do x=0 echo -n "$i " grep "^${i}" /tmp/x | while read file do let x=x+1 echo $x > count done cat count done 6-9-15 8

  9. w/o using array letters=(a b c d e f g h i j k l m n o p q r s t u v w x y z) for i in ${letters[*]} for i in {a..z} 6-9-15 9

  10. Homework revu using wc &back quotes ls /bin > /tmp/x letters=(a b c d e f g h i j k l m n o p q r s t u v w x y z) for i in ${letters[*]} do echo $i `grep "^${i}" /tmp/x | wc -l` done 6-9-15 10

  11. Homework class 2, problem 2. Write a Bash script, reverse.sh, which reverses the contents of a directory passed as a parameter. Assuming /my/dir contains "cache cron games lib log run tmp," your program "reverse.sh /my/dir" will return "tmp run log lib games cron cache." Use an array and a function called reverse(). Do not use the built-in command sort -r. function reverse { let x=$1 while [ ${x} -gt 0 ] do let x=x-1 echo ${names[$x]} done } names=(`ls $1`) reverse ${#names[@]} 6-9-15 11

  12. Homework problem 3 Write a Bash script, filter.sh, which prints those files/directories that have the size greater than the average file size of the directory. Suppose for example that the directory /my/dir has five files/directories with size in parentheses " a (100) b (10) c (100) d (100) e (20)", your program "filter.sh /my/dir" will list "a c d" since the size of each of the three files/directories "a c d" is greater than the average file size of 66. Use three functions: main, average, and filter, where main calls average and filter, and average computes the average file size of a directory and filter filters out those that have less than the average. 6-9-15 12

  13. function average { x=0 count=0 ls -s $1 | grep -v "^total" | ( while read size name do let x=x+1 let count=count+size done let avg=count/x echo $avg ) } function filter { if [ $1 -gt $2 ] then echo $3 fi } avg=$(average $1) ls -s $1 | grep -v "^total" | while read size name do filter $size $avg $name done 6-9-15 13

  14. w/o using functions x=0 count=0 ls -s $1 | grep -v "^total" | while read size name do let x=x+1 let count=count+size echo $count > /tmp/count$$ echo $x > /tmp/x$$ done x=`cat /tmp/x$$` count=`cat /tmp/count$$` let avg=count/x ls -s $1 | grep -v "^total" | while read size name do if [ $size -gt $avg ] then echo $name fi done 6-9-15 14

  15. more array operations Delete elements from an array afsconnect1-71 one >: cat delete.sh myarray=($*) echo ${myarray[*]} del=${myarray[1]} myarray=( "${myarray[@]/$del}" ) echo ${myarray[*]} myarray=(${myarray[*]} $del ) echo ${myarray[*]} afsconnect1-72 one >: bash delete.sh a b c a b c a b c a b c a b c a b c a c a c a c a c a c a c b 6-9-15 15

  16. Problem 4 Write a Bash script, insert-sort.sh, which sorts a list of command line parameters in ascending order. For example, your command will look something like: $ insert-sort.sh 7 2 3 9 -1 and type enter. Your program will return: -1 2 3 7 9 Use only basic commands and array. Do not use any built-in commands that sort array. 6-9-15 16

  17. incorrect solution for j in ${myarray[*]} do min=1000000 for i in ${myarray[*]} do if [ $i -lt $min ] then min=$i fi done echo -n "${min} " myarray=( "${myarray[@]/$min}" ) done echo afsconnect1-42 one >: bash insert-sort.sh 7 4 9 -1 5 -1 4 5 7 9 afsconnect1-43 one >: bash insert-sort.sh 7 7 4 9 -1 7 5 -1 4 5 7 9 1000000 1000000 6-9-15 17

  18. # correct solution myarray=($*) for j in ${myarray[*]} do #echo myarray ${myarray[*]} min=1000000 for i in ${myarray[*]} do if [ $i -le $min ] then min=$i fi done echo -n "${min} " newarray=() x=0 while [ ${myarray[$x]} -ne $min ] do newarray=(${newarray[*]} ${myarray[$x]} ) let x++ done let x++ while [ $x -lt ${#myarray[@]} ] do newarray=(${newarray[*]} ${myarray[$x]} ) let x++ done myarray=(${newarray[*]}) done echo 6-9-15 18

  19. Other commands and misc continue wc 6-9-15 19

  20. special files /dev/tty /dev/console /dev/null 6-9-15 20

  21. Grep examples grep "^am" /etc/passwd grep "^[abc]" /etc/passwd | more grep "^[a-c]" /etc/passwd | more grep "nologin$" /etc/passwd grep "^a.*nologin$" /etc/passwd grep "^a.a.*nologin$" /etc/passwd grep -v "^a.a.*nologin$" /etc/passwd 6-9-15 21

  22. Cut examples 28 cut -c 10-20 /etc/passwd 29 cut -c -10 /etc/passwd 31 cut -c 30- /etc/passwd 37 cut -d ":" -f 5 /etc/passwd 38 cut -d ":" -f 2,5 /etc/passwd 39 cut -d ":" -f 3-5 /etc/passwd 6-9-15 22

  23. Repetition A regular expression may be followed by one of several repetition operators among them: * The preceding item will be matched zero or more times. {n} The preceding item is matched exactly n times. 6-9-15 23

  24. wget gets a URL, stores file in current directory e.g.: wget https://web.njit.edu/~yurkowsk/cs288.html -q Turn off Wget's output. -O file The documents will not be written to the appropriate files, but all will be concatenated together and written to file. If - is used as file, documents will be printed to standard output, disabling link conversion. 6-9-15 24

  25. other Linux tools and built ins shift sort uniq awk Aho, Weinstein & Kernighan. sed stream editor. 6-9-15 25

  26. Usage: sort [OPTION]... [FILE]... -b, --ignore-leading-blanks ignore leading blanks -d, --dictionary-order consider only blanks and alphanumeric characters -f, --ignore-case fold lower case to upper case characters -g, --general-numeric-sort compare according to general numerical value -i, --ignore-nonprinting consider only printable characters -M, --month-sort compare (unknown) < `JAN' < ... < `DEC' -h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G) -n, --numeric-sort compare according to string numerical value -R, --random-sort sort by random hash of keys --random-source=FILE get random bytes from FILE -r, --reverse reverse the result of 6-9-15 comparisons 26

  27. Other reminders to redirect stdout to stderr: 1>&2 to redirect stderr to stdout: 2>&1 to test for null strings: z to test for non-null strings: -n 6-9-15 27

  28. sed 6-9-15 28

  29. sed examples sed -e "/pool-17/d" hosts sed -e "s/pool-1[1-9]/poolA/" hosts sed -f script hosts cat script s/pool-1[1-9]/poolA/ s/pool-[1-9]/poolB/ 6-9-15 29

  30. small problem, 4 line solution Write a bash script call cshuser.sh which finds the names of all the users who use csh as their login shell on afsconnect1.njit.edu. Output their name and user number and login name in that order in three tab separated columns sorted by name. Make sure you do not include the users who use tcsh. Make sure you do not include the colons that are in between the columns in the password file. The tail of your output should look like this: 6-9-15 30

  31. Sanchoy K Das 11728 das Sotirios G Ziavras 11722 ziavras syswork account 10000 syswork tamara gund fac/staff 24297 gund Trevor Tyson fac/staff 21954 tyson Walid Hubbi---ECE-----x3518 11705 hubbi warren c pencak cis stnt 22056 wcp2915 wayne s chaneski cms fac/staff 24858 chaneski william skawinski fac/staff 24614 skawinsk william tereshkovich ie stnt 20795 wxt1955 yun-qing shi fac/staff 24260 shi zhiming ji 32190 ji afsconnect1-187 two >: bash cshuser.sh | tail syswork account 10000 syswork tamara gund fac/staff 24297 gund Trevor Tyson fac/staff 21954 tyson Walid Hubbi---ECE-----x3518 11705 hubbi warren c pencak cis stnt 22056 wcp2915 wayne s chaneski cms fac/staff 24858 chaneski william skawinski fac/staff 24614 skawinsk william tereshkovich ie stnt 20795 wxt1955 yun-qing shi fac/staff 24260 shi zhiming ji 32190 ji 6-9-15 31

  32. passwd file format chess:x:32159:30:murray lieb math fac/staff:/afs/cad/u/c/h/chess:/usr/local/etc/klub/clubshell k2mff:x:32162:30:NJIT Amateur Radio Club:/afs/cad/u/k/2/k2mff:/usr/local/etc/klub/clubshell senate:x:32164:30:The NJIT Student Senate:/afs/cad/u/s/e/senate:/usr/local/etc/klub/clubshell ge:x:32184:30:Hongya Ge:/afs/cad/u/g/e/ge:/bin/tcsh rosato:x:32185:30:anthony d rosato:/afs/cad/u/r/o/rosato:/bin/csh spasovic:x:32186:30:lazar spasovic fac/staff:/afs/cad/u/s/p/spasovic:/bin/csh meyer:x:32187:30:Andrew U. Meyer:/afs/cad/u/m/e/meyer:/bin/csh ji:x:32190:30:zhiming ji:/afs/cad/u/j/i/ji:/bin/csh sengupta:x:32191:30:Arijitkumar Sengupta:/afs/cad/u/s/e/sengupta:/bin/csh gap8357:x:32252:30:glenn a pozo cis stnt:/afs/cad/u/g/a/gap8357:/bin/tcsh jerry:x:32420:30:jerry l fjermestad imag fac/staff:/afs/cad/u/j/e/jerry:/bin/tcsh bukiet:x:32510:30:bruce g bukiet math fac/staff:/afs/cad/u/b/u/bukiet:/bin/tcsh misieg:x:32511:30:michael s siegel math fac/staff:/afs/cad/u/m/i/misieg:/bin/tcsh sohna:x:32525:30:Andrew Sohn:/afs/cad/u/s/o/sohna:/bin/csh lipuma:x:32532:30:james m lipuma hss fac/staff:/afs/cad/u/l/i/lipuma:/bin/tcsh jacksonn:x:32535:30:nancy l jackson hss fac/staff:/afs/cad/u/j/a/jacksonn:/bin/tcsh 6-9-15 32

  33. solution cat /etc/passwd | grep "csh$" | grep -v "tcsh$" | cut -d":" - f1,3,5 | tr : " " | while read id num name do echo $name $num $id done | sort 6-9-15 33

  34. Hw 2 Problem 1 Write script called change_login_shell.sh which prompts for a user ids until an EOF or q is entered; if the id supplied is not in the /etc/passwd file, output a message which says the user is not found, if the user is in the passwd file output that id s dwdwpasswd file entry and ask if this is the entry that should be changed. If the response is not y, Y or yes continue and ask for another id. If the answer is in the affirmative ask what the new login shell should be. Create a file named passwd.1 which substitutes the user old login shell with the new one. /afs/cad/u/y/u/yurkowsk/homework/two/new.sh 6-9-15 34

  35. Class 3 hw, problem Given an example URL: https://web.njit.edu/~yurkowsk/hw2prob2 which points to a file which contains 3 tab separated columns consisting of a name and 2 scores, write a script which takes as input a URL pointing to a similarly formatted file and produces as output this header: Student Test1 Test2 -------- -------- ------- followed by a series of lines which contain the name from the first column of the input, the scores from next two columns and the average of the two. Like this: Lou Cifer 66 6 G R Emlin 100 0 Finally output a line which is the class average for each test and the average for the class for both tests. Like this: Class Avg 83 3 Note that i may test your script with an input URL which has different data but will have the same format. Average -------- 36 50 43 6-9-15 35

Related