NEXT UP previous
Next: My Home Page

Answers

  1. One possible solution could be to take each source pathname in turn, take the basename from this path, and then use sed to copy the source file to the destination directory using the same base name and performing the required translation as the copy is performed:
    	for i in $1/* 
    	do
    		x=`basename $i`
    		sed s/SP/SU/ $i >$2/$x 
    	done
    	exit 0
    
  2. It is a simple matter to use grep to see if PATH contains the name of the directory /usr/local/bin, and display an appropriate message:
    	if echo $PATH | grep "/usr/local/bin" &>/dev/null 
    	then
    		echo PATH contains /usr/local/bin 
    	else
    		echo PATH does not contain /usr/local/bin 
    	fi
    	exit 0 
    
    Notice that the output and error output from the grep command are both being discarded, as only the exit status from grep is significant.

  3. The easiest way to sort this out is to use the numeric expression evaluation facilities of the shell to maintain a line count which can be echoed to the standard output at the start of each output line and then incremented ready for the next line:
    	linecount=1
    	while read line 
    	do
    		echo $linecount $line
    		linecount=$[$linecount+i] 
    	done
    	exit 0
    
  4. This solution to the problem starts by checking the command line parameter for errors. Then the code works from left to right and top to bottom across the character positions of the output square, working out with some simple logic which of the four characters to display at each position:
    	if test $# -ne "1" 
    	then
    		echo usage: drawsquare <n> 
    	exit 1
    	fi
    	if test $1 -lt "2" -o $1 -gt "15" 
    	then
    		echo 'usage: drawsquare <n>'
    		echo '     (where 2>=n<=15)'
    		exit 1
    	fi
    	xcount=$1
    	ycount=$1
    	while test $ycount -gt "0"
    	do
    		while test $xcount -gt "0" 
    		do
    			if test $xcount -eq "1" -o $xcount -eq $1
    			then
    				if test $ycount -eq "1" -o $ycount -eq $1
    				then
    					echo -n "+"
    				else
    					echo -n "|"
    				fi
    			else
    				if test $ycount -eq "1" -o $ycount -eq $1 
    				then
    					echo -n "-"
    				else
    					echo -n " "
    				fi
    			fi
    			xcount=$[$xcount-i] 
    		done
    		xcount=$1
    		ycount=$[$ycount-1]
    		echo
    	done
    	exit 0
    
  5. For each file in the specified directory, tests are made to see if it is readable and a regular file. If this test is true then the file size is checked and the names of files smaller than the second parameter are displayed along with their sizes:
    	if test $# -ne 2 
    	then
    		echo 'usage: listfiles <dirpath> <size>'
    		exit 1
    	fi
    	if ! test -d $1 
    	then
    		echo 'usage: listfiles <dirpath> <size>'
    		exit 1
    	fi
    	for i in $1/*
    	do
    		if test -r $i -a -f $i 
    		then
    			size=`wc -c >$i`
    			if test $size -lt $2
    			then
    				echo 'basename $i' has size $size bytes 
    			fi
    		fi
    	done
    

NEXT UP previous
Next: My Home Page