Читаем UNIX полностью

cd . # place holder for /usr/news

for i in `ls -t * $HOME/.news_time`

do

 case $i in

 */.news_time) break ;;

 *) echo news: $i

 esac

done

touch $HOME/.news_time

<p>3.8.31 <code>news2</code></p>

# news: print news files, version 2

HOME=. # debugging only

cd . # place holder for /usr/news

IFS='

' # just a newline

for i in `ls -t * $HOME/.news_time 2>&1`

do

case $i in

*' not found') ;;

*/.news_time) break ;;

*) echo news: $i ;;

esac

done

touch $HOME/.news_time

<p>3.8.32 <code>news3</code></p>

# news: print news files, final version

PATH=/bin:/usr/bin

IFS='

' # just a newline

cd /usr/news

for i in `ls -t * $HOME/.news_time 2>&1`

do

 IFS=' '

 case $i in

 *' not found') ;;

 */.news_time) break ;;

 *) set X`ls -l $i`

  echo "

$i: ($3) $5 $6 $7

"

  cat $i

 esac

done

touch $HOME/.news_time

<p>3.8.33 <code>nohup</code></p>

trap "" 1 15

if test -t 2>&1

then

 echo "Sending output to 'nohup.out'"

 exec nice -5 $* >>nohup.out 2>&1

else

 exec nice -5 $* 2>&1

fi

<p>3.8.34 <code>older</code></p>

# older f: list files older than f

ls -tr | sed '/^'$!'$/q'

<p>3.8.35 <code>overwrite1</code></p>

# overwrite: copy standard input to output after EOF

# version 1. BUG here

PATH=/bin:/usr/bin

case $# in

1) ;;

*) echo 'Usage: overwrite file' 1>&2; exit 2

esac

new=/tmp/overwr.$$

trap 'rm -f $new; exit 1' 1 2 15

cat >$new # collect the input

cp $new $1 # overwrite the input file

rm -f $new

<p>3.8.36 <code>overwrite2</code></p>

# overwrite: copy standard input to output after EOF

# version 2. BUG here too

PATH=/bin:/usr/bin

case $# in

1) ;;

*) echo 'Usage: overwrite file' 1>&2; exit 2

esac

new=/tmp/overwr1.$$

old=/tmp/overwr2.$$

trap 'rm -f $new $old; exit 1' 1 2 15

cat >$new # collect the input

cp $1 $old # save original file

trap '' 1 2 15 # we are committed; ignore signals

cp $new $1 # overwrite the input file

rm -f $new $old

<p>3.8.37 <code>overwrite3</code></p>

# overwrite: copy standard input to output after EOF

# final version

opath=$PATH

PATH=/bin:/usr/bin

case $# in

0|1) echo 'Usage: overwrite file cmd [args]' 1>&2; exit 2

esac

file=$1; shift

new=/tmp/overwr1.$$; old=/tmp/overwr2.$$

trap 'rm -f $new $old; exit 1' 1 2 15 # clean up files

if PATH=$opath >$new # collect input

then

 cp $file $old # save original file

 trap '' 1 2 15 # we are committed; ignore signals

 cp $new $file

else

 echo "overwrite: $1 failed, $file unchanged" 1>&2

 exit 1

fi

rm -f $new $old

<p>3.8.38 <code>p1.c</code></p>

/* p: print input in chunks (version 1) */

#include

#define PAGESIZE 22

char *progname; /* program name for error message */

main(argc, argv)

 int argc;

 char *argv[];

{

 int i;

 FILE *fp, *efopen();

 progname = argv[0];

 if (argc == 1)

  print(stdin, PAGESIZE);

 else

  for (i = 1; i < argc; i++) {

   fp = efopen(argv[i], "r");

   print(fp, PAGESIZE);

   fclose(fp);

  }

  exit(0);

}

Перейти на страницу:

Похожие книги