CS2718 Lab 6

Unlike previous labs, this lab should be completed by editing and submitting this HTML file and by submitting scripts and output files.

Submit via all materials by D2L by the end of the lab session.

Exercise 1: Built-in Using For

Given the following word list:

cd ls read for echo ps bind w then declare shift help printf grep

Write a shell script, builtin_for.sh, that outputs:

cd is builtin
ls is file
path for ls is /usr/bin/ls
read is builtin
for is keyword
echo is builtin
ps is file
path for ps is /usr/bin/ps
.
.
.

Your script should use type, which and a for loop. Use help type to see any relevant documentation.

Execute the script as follows to capture the output:

builtin_for.sh > ex1.txt

Submit both the script builtin_for.sh and ex1.txt.

Exercise 2: For loops and brace expansion

Consider the following script, mkfiles.sh

files=""
for first in x y z ; do
    for second in 1 2 3 ; do
        files="$files $first$second"
    done
done
echo $files
  1. What is the script's output?

Change this with your answer

  1. Write a one line command the uses braces expansion to produce the same output.

Change this with your answer

Exercise 3: Rewrite as a while loop

Consider the following for loop

for i in 0 3 6 9 12 ; do
    printf "%5x %6x\n" ${i} $(( i**2 ))
done
  1. Give the output of this script below:

Change this with your answer

  1. Explain why 'c' is being displayed on the last line.

Change this with your answer

  1. Explain the role of 5 and 6 in the output:

Change this with your answer

  1. Write a script that uses a while (instead of a for) loop and arithmetic expansion and produces the same output. Name the script while_instead.sh.

Submit as while_instead.sh.

Exercise 4: Rewrite script that uses seq

Consider the following script. Look at the man page for the seq command.

seq 0 10 101 | while read i; do echo "$i $(( i*2 + 1))"; done
  1. What is the script's output?

Change this with your answer

  1. Rewrite the script using only a while loop and arithmetic expansion (it should not call seq). The script must produce the same output.

Submit the script as seq_alt.sh