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
403
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.
403
The following script will find and output the number of blocks occupied by the filename supplied as an argument;
if test –e “${1}”
then
ls -sd ${1} | sed 's/^ *//' | cut -d' ' -f1
else
echo 0
fi
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.
9 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
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
2. Shell Programming and Scripting
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
3. Shell Programming and Scripting
I want notes for learning Shell programming (2 Replies)
Discussion started by: Neha Agarwal
2 Replies
4. Shell Programming and Scripting
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
5. Homework & Coursework Questions
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
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
7. Shell Programming and Scripting
Hi every one,i am new to unix.Can any one tell me about shell programming.. (1 Reply)
Discussion started by: martina100011
1 Replies
8. UNIX for Dummies Questions & Answers
sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies
9. Shell Programming and Scripting
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
LEARN ABOUT DEBIAN
guards
GUARDS(1) User Contributed Perl Documentation GUARDS(1)
NAME
guards - select from a list of files guarded by conditions
SYNOPSIS
guards [--prefix=dir] [--path=dir2:dir2:...] [--default=0|1] [-v|--invert-match] [--list|--check] [--config=file] symbol ...
DESCRIPTION
The script reads a configuration file that may contain so-called guards, file names, and comments, and writes those file names that satisfy
all guards to standard output. The script takes a list of symbols as its arguments. Each line in the configuration file is processed
separately. Lines may start with a number of guards. The following guards are defined:
+xxx Include the file(s) on this line if the symbol xxx is defined.
-xxx Exclude the file(s) on this line if the symbol xxx is defined.
+!xxx Include the file(s) on this line if the symbol xxx is not defined.
-!xxx Exclude the file(s) on this line if the symbol xxx is not defined.
- Exclude this file. Used to avoid spurious --check messages.
The guards are processed left to right. The last guard that matches determines if the file is included. If no guard is specified, the
--default setting determines if the file is included.
If no configuration file is specified, the script reads from standard input.
The --check option is used to compare the specification file against the file system. If files are referenced in the specification that do
not exist, or if files are not enlisted in the specification file warnings are printed. The --path option can be used to specify which
directory or directories to scan. Multiple directories are separated by a colon (":") character. The --prefix option specifies the
location of the files.
AUTHOR
Andreas Gruenbacher <agruen@suse.de> (SuSE Linux AG)
perl v5.14.2 2012-03-04 GUARDS(1)