Answers to Even-numbered Exercises



Similar documents
BASH Scripting. A bash script may consist of nothing but a series of command lines, e.g. The following helloworld.sh script simply does an echo.

Bash shell programming Part II Control statements

Thirty Useful Unix Commands

Lecture 4: Writing shell scripts

Unix Sampler. PEOPLE whoami id who

USEFUL UNIX COMMANDS

CS Unix Tools & Scripting Lecture 9 Shell Scripting

Using SVN to Manage Source RTL

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Systems Programming & Scripting

Introduction to Shell Scripting

Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Hadoop Hands-On Exercises

CS 2112 Lab: Version Control

An Introduction to the Linux Command Shell For Beginners

CS2720 Practical Software Development

SFTP SHELL SCRIPT USER GUIDE

CPSC2800: Linux Hands-on Lab #3 Explore Linux file system and file security. Project 3-1

Unix Scripts and Job Scheduling

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

HP-UX Essentials and Shell Programming Course Summary

An Introduction to Using the Command Line Interface (CLI) to Work with Files and Directories

LECTURE-7. Introduction to DOS. Introduction to UNIX/LINUX OS. Introduction to Windows. Topics:

Answers to Even- Numbered Exercises

Linux command line. An introduction to the Linux command line for genomics. Susan Fairley

Introduction to the UNIX Operating System and Open Windows Desktop Environment

Using SVN to Manage Source RTL

Hands-On UNIX Exercise:

Automating admin tasks using shell scripts and cron Vijay Kumar Adhikari.

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

CSIL MiniCourses. Introduction To Unix (I) John Lekberg Sean Hogan Cannon Matthews Graham Smith. Updated on:

Setting Up the Site Licenses

Kleine Linux-Tricks - schneller ohne Aufwand

Extreme computing lab exercises Session one

A Crash Course on UNIX

Unix Shell Scripting Tutorial Ashley J.S Mills

Using Parallel Computing to Run Multiple Jobs

Command Line - Part 1

Syntax: cd <Path> Or cd $<Custom/Standard Top Name>_TOP (In CAPS)

An Introduction to Using the Command Line Interface (CLI) to Work with Files and Directories

Hadoop Hands-On Exercises

Lecture 22 The Shell and Shell Scripting

Advanced Bash Scripting. Joshua Malone

CTIS486 Midterm I 20/11/ Akgül

Unix the Bare Minimum

List of FTP commands for the Microsoft command-line FTP client

Running your first Linux Program

Command Line Crash Course For Unix

Exercise 1: Python Language Basics

CPSC 2800 Linux Hands-on Lab #7 on Linux Utilities. Project 7-1

AN INTRODUCTION TO UNIX

Linux Shell Script To Monitor Ftp Server Connection

sqlcmd -S.\SQLEXPRESS -Q "select name from sys.databases"

Hadoop Shell Commands

Hadoop Shell Commands

ICS 351: Today's plan

Mass Deploying Bomgar Software to Macs

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

DOS Command Reference

How to build secure Apache Tomcat deployments with RPM.

RECOVER ( 8 ) Maintenance Procedures RECOVER ( 8 )

Unix Guide. Logo Reproduction. School of Computing & Information Systems. Colours red and black on white backgroun

Exercises: FreeBSD: Apache and SSL: pre SANOG VI Workshop

WES 9.2 DRIVE CONFIGURATION WORKSHEET

Extreme computing lab exercises Session one

Installing IBM Websphere Application Server 7 and 8 on OS4 Enterprise Linux

A UNIX/Linux in a nutshell

MAX_RMAN_08137_IGNORE=5 DISK_RETENTION_POLICY='RECOVERY WINDOW OF 7 DAYS'

WebPublish User s Manual

CMSC 216 UNIX tutorial Fall 2010

Birmingham Environment for Academic Research. Introduction to Linux Quick Reference Guide. Research Computing Team V1.0

HIGH AVAILABILITY SETUP USING VERITAS CLUSTER SERVER AND NETAPP SYNCHRONOUS SNAPMIRROR. Jorge Costa, NetApp June 2008

Outline. Unix shells Bourne-again Shell (bash) Interacting with bash Basic scripting References

INASP: Effective Network Management Workshops

Linux Syslog Messages in IBM Director

CS 103 Lab Linux and Virtual Machines

IBM Redistribute Big SQL v4.x Storage Paths IBM. Redistribute Big SQL v4.x Storage Paths

New Lab Intro to KDE Terminal Konsole

Hadoop Basics with InfoSphere BigInsights

Monitoring a Linux Mail Server

Quick Introduction to HPSS at NERSC

Miami University RedHawk Cluster Working with batch jobs on the Cluster

VDCF - Virtual Datacenter Control Framework for the Solaris TM Operating System

Tutorial Guide to the IS Unix Service

Automated Offsite Backup with rdiff-backup

TEL2821/IS2150: INTRODUCTION TO SECURITY Lab: Operating Systems and Access Control

Tor Exit Node Block Scripts

Backing Up TestTrack Native Project Databases

Tutorial 0A Programming on the command line

Welcome and thank you for considering enstratus as your cloud management platform.

SWsoft Plesk 8.2 for Linux/Unix Backup and Restore Utilities. Administrator's Guide

Introduction to Shell Programming

Command-Line Operations : The Shell. Don't fear the command line...

Linux Overview. Local facilities. Linux commands. The vi (gvim) editor

CA and SSL Certificates

Cisco Networking Academy Program Curriculum Scope & Sequence. Fundamentals of UNIX version 2.0 (July, 2002)

1 Basic commands. 2 Terminology. CS61B, Fall 2009 Simple UNIX Commands P. N. Hilfinger

University of Toronto

Sophos Anti-Virus for Linux configuration guide. Product version: 9

Cygwin command line windows. Get that Linux feeling - on Windows

Transcription:

11 Answers to Even-numbered Exercises 1. 2. The special parameter "$@" is referenced twice in the out script (page 442). 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. 4. a. 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 b. When might such a function be useful? When you are writing many shell scripts, it can get tedious to give many chmod commands. This function speeds up the process. c. 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 $* 1

2 Answers to Even-numbered Exercises d. What can you to make the function available every time you log in? Put the function in the ~/.bash_prole and/or ~/.bashrc le to make it available each time you log in (using bash). e. 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 so? Use source to execute the le you put the function in, for example, $ source ~/.bash_prole 5. 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. 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 * echo "===================================================" echo "File: $i" echo "===================================================" cat "$i" ne a. What error message you get when you execute this script? Rewrite the script so that the error es 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 on it: if [ $i == "savethem" ] ; continue

Answers to Even-numbered Exercises 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. 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. 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.

4 Answers to Even-numbered Exercises $ cat zerdel if [ "$1" == "-f" ] nd $2 -empty -print -exec rm -f {} \; else nd $1 -empty -ok rm -f {} \; 13. 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? ]] del=$(echo $1 cut -b3) shift else del=: IFS=$del set $* for i echo $i ne 15. 16. Rewrite bundle (page 469) 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 echo 'if echo $* grep -q' $i ' [ $# = 0 ]' echo echo "echo $i 1&2" echo "cat $i <<'End of $i'" cat $i echo "End of $i" echo ne

Answers to Even-numbered Exercises 5 17. 18. In principle, recursion is never necessary. It can always be replaced by an iterative construct, such as while or until. Rewrite makepath (page 511) as a nonrecursive function. Which version you prefer? Why? function makepath2() { wd=$(pwd) pathname=$1 while [[ $pathname = */* && ${#pathname} 0 ]] if [[! -d "${pathname%%/*}" ]] mkdir "${pathname%%/*}" cd "${pathname%%/*}" pathname="${pathname#*/}" ne if [[! -d $pathname && ${#pathname} 0 ]] mkdir $pathname cd $wd } The recursive version is simpler: There is no need to keep track of the working directory and you not have to handle making the nal directory separately. 19. 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 ] echo "Usage: maxfn dirname" return 1 max=0 for fn in $(/bin/ls $1) thisone=${#fn} if [ $thisone -gt $max ] max=$thisone ne echo "Longest lename is $max characters." 21.

6 Answers to Even-numbered Exercises 22. Write a function that lists the number of regular 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 reg=0 dir=0 blk=0 char=0 fo=0 symlnk=0 other=0 for fn in * 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)) *) ((reg=reg+1)) esac ne echo $reg regular echo $dir directory echo $blk block echo $char character echo $fo FIFO echo $symlnk symbolic link echo $other other

Answers to Even-numbered Exercises 7 b. Use the le type condition tests of the [[ expression ]] syntax to determine a le s type. $ function ft2 () { typeset -i reg=0 dir=0 blk=0 char=0 fo=0 symlnk=0 other=0 for fn in * if [[ -h $fn ]] ((symlnk=$symlnk+1)) elif [[ -f $fn ]] ((reg=reg+1)) elif [[ -d $fn ]] ((dir=$dir+1)) elif [[ -b $fn ]] ((blk=$blk+1)) elif [[ -c $fn ]] ((char=$char+1)) elif [[ -p $fn ]] ((fo=$fo+1)) else ((other=other+1)) ne echo $reg regular echo $dir directory echo $blk block echo $char character echo $fo FIFO echo $symlnk symbolic link echo $other other 23.