Sponsored Content
Full Discussion: shell programming assignment
Top Forums Shell Programming and Scripting shell programming assignment Post 302157650 by Jake777 on Friday 11th of January 2008 04:26:19 PM
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

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

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

2. 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

3. Shell Programming and Scripting

shell programming

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

4. 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

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

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

7. 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

8. 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

9. 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
exit(1)                                                            User Commands                                                           exit(1)

NAME
exit, return, goto - shell built-in functions to enable the execution of the shell to advance beyond its sequence of steps SYNOPSIS
sh exit [n] return [n] csh exit [ ( expr )] goto label ksh *exit [n] *return [n] DESCRIPTION
sh exit will cause the calling shell or shell script to exit with the exit status specified by n. If n is omitted the exit status is that of the last command executed (an EOF will also cause the shell to exit.) return causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command exe- cuted. csh exit will cause the calling shell or shell script to exit, either with the value of the status variable or with the value specified by the expression expr. The goto built-in uses a specified label as a search string amongst commands. The shell rewinds its input as much as possible and searches for a line of the form label: possibly preceded by space or tab characters. Execution continues after the indicated line. It is an error to jump to a label that occurs between a while or for built-in command and its corresponding end. ksh exit will cause the calling shell or shell script to exit with the exit status specified by n. The value will be the least significant 8 bits of the specified status. If n is omitted then the exit status is that of the last command executed. When exit occurs when executing a trap, the last command refers to the command that executed before the trap was invoked. An end-of-file will also cause the shell to exit except for a shell which has the ignoreeof option (See set below) turned on. return causes a shell function or '.' script to return to the invoking script with the return status specified by n. The value will be the least significant 8 bits of the specified status. If n is omitted then the return status is that of the last command executed. If return is invoked while not in a function or a '.' script, then it is the same as an exit. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
break(1), csh(1), ksh(1), sh(1), attributes(5) SunOS 5.10 15 Apr 1994 exit(1)
All times are GMT -4. The time now is 04:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy