80 likes | 236 Views
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:
E N D
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: • $ 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`
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
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
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
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 )
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);\ • }'