1 / 8

Part I:

Part I:. Q1: $ file * | grep “ascii text” | wc -l Q2: $ a.out | tee logfile Q3: $touch file.`date +’%H%M%S’`. Part I:. Q4: Solution 1: $ find -size +1024 -exec rm -i {} ; solution 2: $ rm -i `ls -l | awk ‘ /^-/ {if ($4 > 1024) printf(“%s”,$8);}’`. Part I:. Q5:

bing
Télécharger la présentation

Part I:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Part I: • Q1: • $ file * | grep “ascii text” | wc -l • Q2: • $ a.out | tee logfile • Q3: $touch file.`date +’%H%M%S’`

  2. Part I: • Q4: • Solution 1: • $ find -size +1024 -exec rm -i {} \;solution 2: • $ rm -i `ls -l | awk ‘ /^-/ {if ($4 > 1024) printf(“%s”,$8);}’`

  3. Part I: • Q5: • $ last | grep “`date | cut -d’ ‘ -f2,3” ` | • awk `$7>=“08:00” {print $1” ` • Q6: • $ grep mypals myfile > /dev/null && • mail `grep mypals myfile | cut -d: -f2`

  4. Part I: • Q8: Solution 1: • $ ls -t `grep -l main \`file * | grep ascii | cut -d: -f1 \` 2> /dev/null ` • Solution 2: • grep -l string `file * .* | grep text | cut -d: -f1` | xargs ls -t

  5. No one in the class work out Q7 correctly • Q7: • $ awk ‘/^$/ {print; turn=0; while (getline >0 && length($0) == 0){ turn = 1-turn; if (turn) print; } printf(“%s”,$0);} • /./ {printf(“%s”,$0); while (getline >0 && length($0) >0) printf(“%s”,$0); printf(“\n”); }’ date.txt

  6. Part II Command line parsing • #/bin/sh • showdir=0 • showfile=0 • while [ $# -gt 0 ] • do • case $1 in • -h) echo " lc [-f] [-d] [dir]" • echo " -f: list only files" • echo " -d: list only subdirectories" • exit 1 • ;; • -f) showfile=1 ;; • -d) showdir=1 ;; • -*) echo "Bad option :" $1 • exit 1;; • *) break • ;; • esac • shift • done

  7. Part II:Continue command line parsing • if [ ${showfile} -eq 0 -a ${showdir} -eq 0 ] • then • showfile=1 • showdir=1 • fi • curdir=${1-.} • test -x $curdir || ( echo "Can't chdir to" $curdir && exit 1 )

  8. Part II: main body • ls -l $curdir | awk -F' ' '$1 ~ /^[d-].*/ { printf("%s\t%s\n",$1,$8);}' |\ • nawk -v showfile=$showfile -v showdir=$showdir \ • ' • function output(title,arr,no){\ • printf("%s\n",title);\ • for(i=0;i<no;i++){\ • printf("%-10s ",substr(arr[i],0,10));\ • if(i%6==5)\ • printf("\n");\ • }\ • printf("\n");\ • }\ • BEGIN { countfile=0;countdir=0} \ • $1 ~ /^d.*/ { if( showdir ) dirs[countdir++]=$2; } \ • $1 ~ /^[^d].*/ { if( showfile ) files[countfile++]=$2; } \ • END { • if (showdir) \ • output("Directories:",dirs,countdir);\ • if(showfile)\ • output("\nFiles:",files,countfile);\ • }'

More Related