Hint: a sample replacement command for vi/sed to replace all occurrence
of substring "usa" by "United States" is
s/usa/USA/g
This exercise is about regular expressions, which are supported by many Unix commands, e.g. shell, perl, grep, awk, vi, sed, .... The regular expression allow one to specify patterns with special operators for repitition, choice, etc. It allows one to explore some useful features which not covered in the textbook.
ln hardlink1 myfile.txt |
ln hardlink2 hardlink1 |
ln -s symbolicLink1 myfile.txt |
ln -s symbolicLink2 symbolicLink1 |
/* Invoke with a positive integer command line argument */ /* HTML web browser may not print include file properly. */ /* Include stdio.h, unistd.h, stdlib.h enclosing each in angular brackets */ #include#include #include #define NL "\n" void foo(int i, int p) { if ( div(i,p).rem > 0) printf("%d %s", i, NL); } int main(int argc, char *argv[]) { int p, max, n, cpid, status, fd[2]; if (argc >= 2) { max = atoi(argv[1]); } else { printf("Usage: %s positiveInteger %s", argv[0], NL); exit(0); } if (argv[2] == NULL) { p = 2; } else { scanf("%d", &p) ; } if (p <= max) { printf("%d %s", p, NL); pipe(fd); cpid = fork(); if (cpid) { dup2(fd[1], STDOUT_FILENO); close(fd[1]); } else{ dup2(fd[0], STDIN_FILENO); close(fd[0]); execl(argv[0], argv[0], argv[1], "child", NULL); } if (p == 2) { n = 2; while(n < max) foo(n++,p) ; } else { do { scanf("%d", &n); foo(n,p); } while (n <= max ); } printf("%d %s", max+1, NL); /* termination flag */ while ( wait(&status) != cpid ); } exit(0); }
/* HTML web browser may not print include file properly. */ /* Include stdio.h enclosed in angular brackets */ #includevoid main (int argc, char* argv[]) { int flag; char NL[] = "\n" ; if (argc != 2) { printf ("Usage %s case(0/1/2)", argv[0]); exit(0); } else { flag = atoi(argv[1]); } switch (flag) { case 0: { printf("Hello %s", NL) ; fork(); printf(" Bye "); break;} case 1: { printf("Hello"); fork(); printf("Bye %s", NL); break;} case 2: { printf("Hello %s", NL); fork (); printf(" Bye %s", NL); break;} } }