wget https://courses.cs.washington.edu/courses/cse391/23au/lectures/9/questions9.zip
unzip questions9.zip
  1. Suppose I have a bash script named script1.sh with the following contents

    #!/bin/bash
    
    book="Educated"
    echo "$book is a fantastic book"
    echo 'you should totally read $book'
    echo "hunter highly recommends $BOOK"
    

    What would be the result of running ./script1.sh (You may assume that it has execute permissions)?

    Solutions
    Educated is a fantastic book
    you should totally read $book
    hunter highly recommends
    
  2. Write a shell script named if_args.sh which takes two command line arguments and prints “these are sum lucky numbers!” if their sum is equal to 7, and prints a sad face otherwise.

    Solutions
    #!/bin/bash
    
        arg1=$1
        arg2=$2
        let sum="$arg1+$arg2"
    
        # Good to debug with echoing
        # echo "$arg1, $arg2, $sum"
    
        if [ $sum -eq 7 ]; then
            echo "these are sum lucky numbers!"
        else
            echo ":("
        fi
    
  3. Write a shell script named loop.sh that loops over all files in the current directory and prints out the contents of the file, with newlines between each output. (Note, we could probably do this without a loop, but for the sake of learning let’s use a loop here). For the sake of this problem you can assume there are no sub-directories in the current directory. If you finish early, try make this work when there are sub-directories.

    Solutions
    #!/bin/bash
    
        # First attempt, doesn't handle directories
        for file in $(ls); do
            cat $file
        done
    
        # Second attempt to ignore directories
        # One option: Change what you loop over
        #   Example: find . -type f -maxd
        #   Example: ls -p | grep -v "/"
        # Another option: Change loop logic
        for file in $(ls); do
            if [ -f $file ]; then
                cat $file
            fi
        done
    
  4. Write a shell script named filter.sh which takes any amount of numbers as arguments and prints out all the numbers which are even (Note that bash has a % operator just like Java). If no numbers are given as inputs, print out a help message describing how to use the program.

    Solutions
    #!/bin/bash
    
        # Check if the number of arguments is non-zero
        if [ $# -eq 0 ]; then
            echo "USAGE: ./filter.sh NUMBERS..."
            exit 1
        fi
    
        # Loop over all arugments, printing out if they are even
        for n in $@; do
            let i="$n % 2"
            if [ $i -eq 0 ]; then
                echo $n
            fi
        done
    
  5. Write a shell script named traveler.sh which goes into each directory (only one deep) from the current directory and prints out it’s contents.

    Solutions
    #!/bin/bash
    
        for dir in $(ls -p | grep "/"); do
            cd $dir
            ls
            cd ..
        done
    
        # Doesn't change the directory in the parent shell!
        cd dir1