Download presentation
Presentation is loading. Please wait.
Published byGinger Hood Modified over 8 years ago
1
Part I: n Q1: n $ file * | grep “ascii text” | wc -l n Q2: n $ a.out | tee logfile n Q3: $touch file.`date +’%H%M%S’` n
2
Part I: n Q4: n Solution 1: n $ find -size +1024 -exec rm -i {} \; solution 2: n $ rm -i `ls -l | awk ‘ /^-/ {if ($4 > 1024) printf(“%s”,$8);}’`
3
Part I: n Q5: n $ last | grep “`date | cut -d’ ‘ -f2,3” ` | n awk `$7>=“08:00” {print $1” ` n Q6: n $ grep mypals myfile > /dev/null && n mail `grep mypals myfile | cut -d: -f2`
4
Part I: n Q8: Solution 1: n $ ls -t `grep -l main \`file * | grep ascii | cut -d: -f1 \` 2> /dev/null ` n Solution 2: n grep -l string `file *.* | grep text | cut -d: - f1` | xargs ls -t
5
No one in the class work out Q7 correctly n Q7: n $ awk ‘/^$/ {print; turn=0; while (getline >0 && length($0) == 0){ turn = 1-turn; if (turn) print; } printf(“%s”,$0);} n /./ {printf(“%s”,$0); while (getline >0 && length($0) >0) printf(“%s”,$0); printf(“\n”); }’ date.txt
6
Part II Command line parsing n #/bin/sh n showdir=0 n showfile=0 n while [ $# -gt 0 ] n do n case $1 in n -h)echo "lc [-f] [-d] [dir]" n echo "-f: list only files" n echo "-d: list only subdirectories" n exit 1 n ;; n -f)showfile=1 ;; n -d)showdir=1 ;; n -*)echo "Bad option :" $1 n exit 1;; n *)break n ;; n esac n shift n done
7
Part II: Continue command line parsing n if [ ${showfile} -eq 0 -a ${showdir} -eq 0 ] n then n showfile=1 n showdir=1 n fi n curdir=${1-.} n test -x $curdir || ( echo "Can't chdir to" $curdir && exit 1 )
8
Part II: main body n ls -l $curdir | awk -F' ' '$1 ~ /^[d-].*/ { printf("%s\t%s\n",$1,$8);}' |\ n nawk -v showfile=$showfile -v showdir=$showdir \ n ' n function output(title,arr,no){\ n printf("%s\n",title);\ n for(i=0;i<no;i++){\ n printf("%-10s ",substr(arr[i],0,10));\ n if(i%6==5)\ n printf("\n");\ n }\ n printf("\n");\ n }\ n BEGIN { countfile=0;countdir=0} \ n $1 ~ /^d.*/ { if( showdir ) dirs[countdir++]=$2; } \ n $1 ~ /^[^d].*/ { if( showfile ) files[countfile++]=$2; } \ n END { n if (showdir) \ n output("Directories:",dirs,countdir);\ n if(showfile)\ n output("\nFiles:",files,countfile);\ n }'
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.