Answers to Even-numbered Exercises

Similar documents
INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Basic Linux (Bash) Commands

COMP2100/2500 Lecture 17: Shell Programming II

COMP 4/6262: Programming UNIX

Shell programming. Introduction to Operating Systems

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

Lab #12: Shell Scripting

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

5/8/2012. Creating and Changing Directories Chapter 7

Useful Unix Commands Cheat Sheet

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Name: Tej. D. Shah Subject:CC-304 Linux Uni. Practical programme College :L.J. College Of Computer Application. Questions:

Shells & Shell Programming (Part B)

The Unix Shell & Shell Scripts

Essentials for Scientific Computing: Bash Shell Scripting Day 3

CS214 Advanced UNIX Lecture 4

LOG ON TO LINUX AND LOG OFF

find Command as Admin Security Tool

Shell Programming (bash)

Lecture 02 The Shell and Shell Scripting

Bourne Shell Reference

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Unix Introduction to UNIX

Bash scripting basics

Do not start the test until instructed to do so!

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2018

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

UNIX Programming Laboratory. Subject Code: 10MCA17

CS 2505 Computer Organization I Test 1

Systems Programming/ C and UNIX

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

Exploring UNIX: Session 3

Computer Systems and Architecture

Програмиранев UNIX среда

Shell Programming Overview

24 Writing Your First Script

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

UNIX shell scripting

Subcontractors. bc math help for the shell. interactive or programatic can accept its commands from stdin can accept an entire bc program s worth

Shell Programming 1/44

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Introduction: What is Unix?

Linux shell scripting Getting started *

Introduction to Linux for BlueBEAR. January

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Shell Control Structures

Linux shell programming for Raspberry Pi Users - 2

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

UNIX System Programming Lecture 3: BASH Programming

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Part 1: Basic Commands/U3li3es

$Id: asg4-shell-tree.mm,v :36: $

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Files and Directories

Introduction to Linux

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Unix background. COMP9021, Session 2, Using the Terminal application, open an x-term window. You type your commands in an x-term window.

LINUX Scripts de Bash Shell. Exercise 1 - Working with Files

CS 2505 Computer Organization I Test 1

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

9.2 Linux Essentials Exam Objectives

CSC 2500: Unix Lab Fall 2016

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

CSCI 2132 Software Development. Lecture 5: File Permissions

Assignment 3, Due October 4

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

15-122: Principles of Imperative Computation

Shell Start-up and Configuration Files

Computer Systems and Architecture

Shell Control Structures

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

This is Lab Worksheet 3 - not an Assignment

Essential Skills for Bioinformatics: Unix/Linux

Assignment 2. Summary. Some Important bash Instructions. CSci132 Practical UNIX and Programming Assignment 2, Fall Prof.

CSE 374 Midterm Exam Sample Solution 2/6/12

Shells and Shell Programming

Make sure the parameter expansion is quoted properly. It may not be necessary here, but it is good practice.

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

Shells and Shell Programming

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

Introduction to Supercomputing

Assignment clarifications

d. Permissions 600 on directory dir and 300 on file dir/foo. c. Permissions 700 on directory dir and 200 on file dir/foo.

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Linux Training. for New Users of Cluster. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Transcription:

11 Answers to Even-numbered Exercises 1. ewrite t propriate actions xists and the user does not have write permission to the le. Verify that the modied script works. 2. The special parameter "$@" is referenced twice in the out script (page 401). Explain what would be different if the parameter "$ *" were used in its place. If you replace "$@" with "$*" in the out script, cat or less would be given a single argument: a list of all les you specied on the command line enclosed within single quotation marks. This list works when you specify a single lename. When you specify more than one le, the shell reports No such le or directory because there is not a le named the string you specied on the command line (the SPACEs are not special characters when they are enclosed within single quotation marks). 3. a lter that takes a list of les as input and outputs the basename (page 31) of each le in the list. 4. Write a function that takes a single lename as an argument and adds execute permission to the le for the user. $ function perms () { chmod u+x $1 a. When might such a function be useful? When you are writing many shell scripts, it can be tedious to give many chmod commands. This function speeds up the process. b. Revise the script so that it takes one or more lenames as arguments and adds execute permission for the user for each le argument. $ function perms { chmod u+x $* c. What can you do to make the function available every time you log in? Put the function in ~/.bash_prole and/or ~/.bashrc to make it available each time you log in (using bash). 1

d. Suppose that, in addition to having the function available on subsequent login sessions, you want to make the function available now in your current shell. How would you do so? Use source to execute the le you put the function in, for example, $ source ~/.bash_prole 5. When might be necessary or advisable to write a script instead of a shell function? Give as many reasons as you can think of. 6. Write a shell script that displays the names of all directory les, but no other types of les, in the working directory. There are many ways to solve this problem. The listdirs script uses le to identify directory les and grep to pull them out of the list. Then sed removes everything from le s output, starting with the colon. $ cat listdirs le "$@" grep directory sed 's/:.*//' 7. Write a script nd) each time before you display the time. 8. Enter the following script named saveles, and give yourself execute permission to the le: $ cat saveles #! /bin/bash echo "Saving les in current directory in le savethem." exec savethem for i in * do echo "===================================================" echo "File: $i" echo "===================================================" cat "$i" done a. What error message do you get when you execute this script? Rewrite the script so that the error does not occur, making sure the output still goes to savethem. You get the following error message: cat: savethem: input le is output le Add the following lines after the line with do on it: if [ $i == "savethem" ] ; then continue

3 b. What might be a problem with running this script twice in the same directory? Discuss a solution to this problem. Each time you run saveles, it overwrites the savethem le with the current contents of the working directory. When you remove a le and run saveles again, that le will no longer be in savethem. If you want to keep an archive of les in the working directory, you need to save the les to a new le each time you run saveles. If you prex the lename savethem with $$, you will have a unique lename each time you run saveles. 9. Read ome experiments, and answer following questions: 10. Using the nd utility, perform the following tasks: a. List all les in the working directory and all subdirectories that have been modied within the last day. $ nd. -mtime -1 b. List all les that you have read access to on the system that are larger than 1 megabyte. $ nd / -size +1024k c. Remove all les named core from the directory structure rooted at your home directory. $ nd ~ -name core -exec rm {} \; d. List the inode numbers of all les in the working directory whose lenames end in.c. $ nd. -name "*.c" -ls e. List all les that you have read access to on the root lesystem that have been modied in the last 30 days. $ nd / -xdev -mtime -30 11. Write a shor ing ility.)

4 12. Write a script that takes the name of a directory as an argument and searches the le hierarchy rooted at that directory for zero-length les. Write the names of all zero-length les to standard output. If there is no option on the command line, have the script delete the le after displaying its name, asking the user for conrmation, and receiving positive conrmation. A f (force) option on the command line indicates that the script should display the lename but not ask for conrmation before deleting the le. The following script segment deletes only ordinary les, not directories. As always, you must specify a shell and check arguments. $ cat zerdel if [ "$1" == "-f" ] then nd $2 -empty -print -exec rm -f {} \; else nd $1 -empty -ok rm -f {} \; 13. Write a script that takes a colon-separated list of items and outputs items, one per line, to standard output (without the colons). 14. Generalize the script written in exercise 13 so that the character separating the list items is given as an argument to the function. If this argument is absent, the separator should default to a colon. This script segment takes an optional option in the form dx to specify the delimiter x: $ cat nodel if [[ $1 == -d? ]] then del=$(echo $1 cut -b3) shift else del=: IFS=$del set $* for i do echo $i done 15. Write a function name ble.

5 16. Rewrite bundle (page 428) so that the script it creates takes an optional list of lenames as arguments. If one or more lenames are given on the command line, only those les should be re-created; otherwise, all les in the shell archive should be re-created. For example, suppose that all les with the lename extension.c are bundled into an archive named srcshell, and you want to unbundle just the les test1.c and test2.c. The following command will unbundle just these two les: $ bash srcshell test1.c test2.c $ cat bundle2 #!/bin/bash # bundle: group les into distribution package echo "# To unbundle, bash this le" for i do echo 'if echo $* grep -q' $i ' [ $# = 0 ]' echo then echo "echo $i 1&2" echo "cat $i <<'End of $i'" cat $i echo "End of $i" echo done 17. What kind of links will nd? Why? 18. In principle, recursion is never necessary. It can always be replaced by an iterative construct, such as while or until. Rewrite makepath (page 76) as a nonrecursive function. Which version do you prefer? Why? function makepath2() { wd=$(pwd) pathname=$1 while [[ $pathname = */* && ${#pathname} 0 ]] do if [[! -d "${pathname%%/*}" ]] then mkdir "${pathname%%/*}" cd "${pathname%%/*}" pathname="${pathname#*/}" done if [[! -d $pathname && ${#pathname} 0 ]] then mkdir $pathname cd $wd } The recursive version is simpler: There is no need to keep track of the working directory and you do not have to handle making the nal directory separately.

6 19. Lists are coo the front of the list, as in 20. Write a function that takes a directory name as an argument and writes to standard output the maximum of the lengths of all lenames in that directory. If the function s argument is not a directory name, write an error message to standard output and exit with nonzero status. $ function maxfn () { typeset -i max thisone if [! -d "$1" -o $# = 0 ] then echo "Usage: maxfn dirname" return 1 max=0 for fn in $(/bin/ls $1) do thisone=${#fn} if [ $thisone -gt $max ] then max=$thisone done echo "Longest lename is $max characters." 21. Modifhy.

7 22. Write a function that lists the number of ordinary les, directories, block special les, character special les, FIFOs, and symbolic links in the working directory. Do this in two different ways: a. Use the rst letter of the output of ls l to determine a le s type. $ function ft () { typeset -i ord=0 dir=0 blk=0 char=0 fo=0 symlnk=0 other=0 for fn in * do case $(ls -ld "$fn" cut -b1) in d) ((dir=$dir+1)) b) ((blk=$blk+1)) c) ((char=$char+1)) p) ((fo=$fo+1)) l) ((symlnk=$symlnk+1)) a-z) ((other=other+1)) *) ((ord=ord+1)) esac done echo $ord ordinary echo $dir directory echo $blk block echo $char character echo $fo FIFO echo $symlnk symbolic link echo $other other

8 b. Use the le type condition tests of the [[ expression ]] syntax to determine a le s type. $ function ft2 () { typeset -i ord=0 dir=0 blk=0 char=0 fo=0 symlnk=0 other=0 for fn in * do if [[ -h $fn ]] then ((symlnk=$symlnk+1)) elif [[ -f $fn ]] then ((ord=ord+1)) elif [[ -d $fn ]] then ((dir=$dir+1)) elif [[ -b $fn ]] then ((blk=$blk+1)) elif [[ -c $fn ]] then ((char=$char+1)) elif [[ -p $fn ]] then ((fo=$fo+1)) else ((other=other+1)) done echo $ord ordinary echo $dir directory echo $blk block echo $char character echo $fo FIFO echo $symlnk symbolic link echo $other other 23. Modify a question are randomly arranged.