shell programming assignment

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell programming assignment
# 1  
Old 01-11-2008
shell programming assignment

I have a very tough shell program to do. Here is the assignment:

Write a non-interactive script that takes in any number of directory names as arguments and calculates and outputs the total number of blocks of disk space occupied by the ordinary files in all the directories. For example, the user would type:

assignment3 dir1 dir2 dir3 dir4


and the output is simply the number of blocks occupied by ordinary files in those directories. If, let’s say dir2 was not a directory and dir4 was inaccessible due to lack of permission say, then the program output (including errors) might be:

assignment3 dir1 dir2 dir3 dir4

assignment3: WARNING: dir2 is not a directory.

assignment3: WARNING: dir4 could not be accessed.


The following script will find and output the number of blocks occupied by the filename supplied as an argument;

if test –e “${1}”


ls -sd ${1} | sed 's/^ *//' | cut -d' ' -f1


echo 0


If the file given as an argument exists then ls will get the size in blocks and the filename and pass them to sed; sed will strip the leading spaces and pass it to cut; cut will extract and output the number of blocks. If the file given as an argument does not exist then 0 is output (a file that does not exist occupies no space).

This script will be placed in your home directory and named getblocks. You can use it in your script to get the number of blocks occupied by a file. Use it in your script in the following way (I am using a file called ‘file17’ as an example – in your program the filenames will be held in variables):

(( numblocks=$(getblocks file17) ))

This line will calculate the number of blocks occupied by a file called file17 and assign it to variable numblocks.

Your script should give sensible error messages and/or warnings where appropriate. For example, you should detect and report on the following possible errors or warnings:

1. no arguments given: ERROR – need to exit the program;
2. the argument being processed is not a directory: WARNING – no need to exit… just continue to the next argument;
3. the argument given names a directory that cannot be accessed (use ls command to determine this): WARNING - no need to exit… just continue to the next argument.

To tackle this assignment you will need to know:

1. how to capture the output of a command inside a script using ‘command substitution’; (see lab 7)
2. how to write a for loop that will process a list of files from a directory named in the argument list; (see lab 7)
3. how to write a while loop that will continue to process the arguments until they are all dealt with; (see lab 7)
4. how to create (and initialize) integer variables, and lowercase and uppercase string variables. (see lab 7)
5. how to do simple arithmetic using the (( )) command. (see lab 7)
6. how to check if a file is an ordinary file;
7. how to check if a file is a directory file.
8. how to write error messages, usage messages, and warning messages;

I just need some help in getting started on it.
Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Programming

Hi, I am using two files - one file contains list of service name , other file contains commands for each of these service name . I have to read each service name and check this string in 1.cfg file , if it exists , then i have to read another file (commands file ) and take the string and... (2 Replies)
Discussion started by: santhoshks
2 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

Shell programming

Hi every one,i am new to unix.Can any one tell me about shell programming.. (1 Reply)
Discussion started by: martina100011
1 Replies

4. Shell Programming and Scripting

Trouble with variable assignment and reading in shell scripting

Hi, I have an inputfile with some table names in it. For ex: t_arnge t_profl t_fac I need a script which reads the line one by one and need to assign to some dynamic variable. If to explain the above example: i need some a=table_name1 table_name1=t_arnge in first time and... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

5. Homework & Coursework Questions

Bash Shell Programming assignment.

Please take a look I am stuck on step 4 1. The problem statement, all variables and given/known data: #!/bin/bash ### ULI101 - ASSIGNMENT #2 (PART A) - DUE DATE Wed, Aug 3, 2011, before 12 midnight. ###==================================================================================== ###... (13 Replies)
Discussion started by: almirzaee
13 Replies

6. Shell Programming and Scripting

need help for shell programming

My purpose was to print out all of name of students in a list.First of all,I created a file name "List" in /home/tuan/Desktop/Shell_programming as below Tom Henry Ben Linda Marry And my script "script" is #!/bin/sh path=/home/tuan/Desktop/Shell_programming/List for student in $path... (3 Replies)
Discussion started by: thungmail
3 Replies

7. Shell Programming and Scripting

shell programming

I want notes for learning Shell programming (2 Replies)
Discussion started by: Neha Agarwal
2 Replies

8. Shell Programming and Scripting

variable assignment in new shell

Hi, I'm writing a KSH script, and at one point, I have to call a new shell and perform some variable assignments. I noticed that the assignment is not working. Please see two samples below: Command 1: #>ksh "i=2;echo I is $i" Output: #>I is Command 2: #>ksh <<EOF > i=2 > echo I... (7 Replies)
Discussion started by: maverick80
7 Replies

9. UNIX for Dummies Questions & Answers

Shell Programming Help

Hi, I am new to shell programming, and hve been given a request to write a shell script. Is there any good places to go to see examples of how to write shell programming scripts? Thanks (4 Replies)
Discussion started by: mec585858
4 Replies
Login or Register to Ask a Question