26.04.2015 Views

Bash Script Programming II - Christos Markides

Bash Script Programming II - Christos Markides

Bash Script Programming II - Christos Markides

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ACSC372 – Systems <strong>Programming</strong><br />

Lab: 4 Semester: Spring 2011<br />

Instructor: <strong>Christos</strong> <strong>Markides</strong><br />

Use the following spaces to write down the results for each task.<br />

1) Write a script that reads a file with each line containing 3 numbers. The script should then<br />

read the three numbers and produce their sum and their product. For each line the script<br />

should display: number1 + number2 + number3 = result. Use appropriate functions to<br />

calculate the sum and product in each case. At the last the line should print Goodbye!<br />

2) Write a script that reads each line of a file and then displays it double-spaced (if a newline<br />

character is found another newline is echoed). For example: double /etc/passwd:<br />

3) Write a shell script that will read a number of parameters and check whether the<br />

parameters submitted correspond to directories. If the parameters correspond to directories<br />

then the script should delete all files in that directory that are older than 40 days. The<br />

script should check for the number of parameters and print appropriate errors and help.<br />

Use the man pages to find out information about the command find. For example:<br />

./cleandir.sh foodir lopdir<br />

Page 1 of 3


4) Write a shell script that will prompt the user to enter the name of a file. The script should<br />

check whether the file exists. If the file exists then the script should read the file which<br />

contains three columns of numbers and print their sums in the form of col1 + col2 +<br />

col3 = sum using a suitable function sum(). The script should print at the beginning<br />

Printing Sums and at the end End of Sums. The script should check for the number of<br />

parameters and print appropriate errors and help. Use the file numlist.csv for testing.<br />

For example: ./sums.sh<br />

Page 2 of 3


5) Write a script that will read a file (.opensessions) that contains a number of sessions<br />

that are currently running on your system. The script should then isolate and store in an<br />

array the PID of each session. The script should then present the following menu.<br />

There are 3 sessions open<br />

a) List sessions<br />

b) Make current<br />

c) Kill session<br />

d) Exit<br />

Option: a<br />

Open sessions<br />

1. 4043<br />

2. 2028<br />

3. 3592<br />

There are 3 sessions open<br />

a) List sessions<br />

c) Make current<br />

b) Kill session<br />

d) Exit<br />

Option: d<br />

There are 3 sessions open<br />

a) List sessions<br />

b) Make current<br />

c) Kill session<br />

d) Exit<br />

Option: b<br />

Enter session (0-none -3): 3<br />

[Session is now in foreground use fg cmd]<br />

[Terminate session to return script]<br />

There are 2 sessions open<br />

a) List sessions<br />

b) Make current<br />

c) Kill session<br />

d) Exit<br />

Option:<br />

There are 3 sessions open<br />

a) List sessions<br />

c) Make current<br />

b) Kill session<br />

c) Exit<br />

Option: b<br />

Enter session (0-none -3): 3<br />

Enter session to kill: 2<br />

[kill session]<br />

There are 2 sessions open<br />

a) List sessions<br />

c) Make current<br />

b) Kill session<br />

c) Exit<br />

Option:<br />

The script should cater for cases that the number of sessions open are 0 or in the even that the<br />

user has selected to kill or to bring to foreground a session that does not exist (anymore) in the<br />

array:<br />

cat .opensessions<br />

4043.pts-0.hpc8 (Detached)<br />

2028.pts-0.hpc8 (Detached)<br />

3592.pts-0.hpc8 (Detached)<br />

[christos@hpc8 ~]$ ./countsessions<br />

There are 3 sessions open<br />

a) List sessions<br />

b) Make current<br />

c) Kill session<br />

d) Exit<br />

Option: a<br />

Page 3 of 3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!