UNIX - SigmaNet

Download Report

Transcript UNIX - SigmaNet

nd
2 Assignment
 Improve Toyshell with some new feature, such as: help, cd, ctrl/D,
setenv and compile it. Create a user “lapsa” and make him to get
Toyshell upon login.
 Write 4 small shell scripts specified
 Explain some Unix shell script (e.g. some strat-up sequence script)
with total length >30 lines


What is the role of this script in the Unix system?
Comment the contents of the script line by line
 (Optional): replace Unix “init” program with some other program
(e.g. shell) and manually bring the system to usable state

Record your manual steps into a shell-script and use it to initialize the
system automatically after restart (without the classic “init”)
Submission
 Name your submission JanisBerzins.zip
 Name shell scripts 1.sh, 2.sh, 3.sh, 4.sh

To ease their automatic verification by teacher
Technical info
 Recommended is Bash; if you use a different shell language, make
sure that we will be able to run it
 Scripts can use external “helper” programs, but do not use any
exotics that the testing system might not have
 Scripts must not ask for any other input than specified or pause as
they may be tested automatically
 Of course, no abuse attempts
 Toyshell can be (optionally) improved with fragments from file
found under 4. lekcija – “ Noderīgi progr.fragmentu paraugi” or by
looking at some real shell source code
1.sh
 Create script, that automatically




writes “hello world” to standard output
deletes file “deleteme.txt”
creates directory “new_dir” in the working directory
writes the output of “date” command to file “date.txt”
inside the “new_dir” directory
2.sh
 File “a” will contain one number 0 <= A <= 100
 File “b” will contain one number 0 <= B <= 100
 Create Bash script, that will read inputs from files and
create file “c” containing number A*B (A multiplied by B)
 Example:


If file “a” contains “4”, file “b” contains “5”, and “./2.sh” is run,
file “c” should contain “20”.
3.sh
 Create script, that will take 2 arguments:

3.sh <directory> <destination>
 Search the files in <directory> for substring “moveme” in
the file content
 Move those files that contain the string to directory
<destination>
 On the standard output, output two lines:


On first line, output the total number of lines that matched
On second line, output the total number of files moved
4.sh
 Create a number guessing game, that “thinks” of a
number N in the range of 1..100 and makes the user
guess it
 Read user’s guess (internal command read)
 After successful attempt, show victory message and exit
 After unsuccessful attempt, show number of tries left, and
whether the number guessed is larger or smaller than N
 If 10 tries are made, display N and exit
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#define MAXLINE 200
#define MAXARG 20
extern char **environ;
void env(void){
int i;
for(i=0;environ[i]!=NULL;i++){
printf("%s\n",environ[i]);
}
}
void exitsh(int status){
_exit(status);
}
void execute(char *arg[]){
pid_t pid;
int status;
pid=fork();
if(pid>0){
wait(&status);
} else if (pid==0) {
execvp(arg[0],arg);
printf("Komanda nav atrasta\n");
exitsh(0);
} else {
printf("Kluda fork() sistemas
izsaukuma\n");
}
}
toyshell.c
int main (void){
char cmd[MAXLINE];
char *cmdp;
char *av[MAXARG];
int i;
while(1){
printf("$toyshell$> ");
fgets(cmd,sizeof(cmd),stdin);
if(strcmp(cmd,"env\n")==0){
env();
} else if(strcmp(cmd,"exit\n")==0){
exitsh(0);
} else {
cmdp=cmd;
for(i=0;i<MAXARG;i++){
av[i]=strtok(cmdp," \t\n");
cmdp=NULL;
}
execute(av);
}
}
return(0);
}
“toyshell” palaišana
# /usr/bin/gcc toyshell.c
# cc toyshell.c
# ./a.out
$toyshell$> env
USER=root
HOME=/root
TERM=vt100
PATH=/root/bin:/usr/local/bin:/bin:/usr/bin
SHELL=/bin/sh
$toyshell$> ps
PID TTY TIME CMD
126 co 0:00 -sh
95 c1 0:00 getty
435 p1 0:00 ./a.out
436 p1 0:00 ps
$toyshell$> exit
#