Readings

The readings for Monday, January 30 are:

  1. Shell Scripting Tutorial

    You will need this information to complete this reading assignment.

  2. The Linux Command Line:

    • Chapter 16 - Networking
    • Chapter 17 - Searching For Files
    • Chapter 18 - Archiving And Backup

    You will need this information to complete the next homework assignment, but not for this reading assignment.

TL;DR

The focus of this reading is to introduce shell scripting in bash and some basic networking.

Optional Resources:

Questions

Given the following output of ls -l:

total 8.0K
-rw-r--r-- 1 pbui pbui  23 Jan 18 15:39 README.md
-rw-r--r-- 1 pbui pbui 155 Jan 25 01:15 exists.sh

And the following script, exists.sh:

#!/bin/sh

if test -e "$1"; then
    echo "$1 exists!"
else
    echo "$1 does not exist!"
fi

In your reading02/README.md file, answer the following questions:

  1. How would you run the script even though it is not executable?

  2. How would you make this script executable?

  3. Once this script is executable, how would you run it directly?

  4. What is the purpose of the line #!/bin/sh?

  5. What is the output of the script if you run it with the arguments *?

  6. What is the $1 that appears in the script?

  7. What does test -e "$1" do?

  8. What does this script do?

Write a new version of exists.sh with the following modifications:

  1. Use [ instead of test for the conditional.

  2. Test every command line argument (one at a time).

  3. Return an error code if one of the tests fails.

  4. Display an error message and exit with an error if no arguments are given.

Testing

To verify the correctness of your exists.sh script, you should be able to reproduce the following:

$ ls -l                               # List files in reading02 directory
total 8.0K
-rw-r--r-- 1 pbui pbui  23 Jan 18 15:39 README.md
-rwxr-xr-x 1 pbui pbui 254 Jan 28 18:02 exists.sh

$ ./exists.sh * && echo Success       # Run script and check error code
exists.sh exists!
README.md exists!
Success

$ ./exists.sh * ASDF || echo Success  # Run script and check error code
exists.sh exists!
README.md exists!
ASDF does not exist!
Success

Feedback

If you have any questions, comments, or concerns regarding the course, please provide your feedback at the end of your response.

Submission

To submit your assignment, please commit your work to the reading02 folder in your assignments GitLab repository:

$ cd path/to/repository                   # Go to assignments repository
$ cd reading02                            # Go to Reading 02 directory
...
$ $EDITOR README.md                       # Edit README.md
$ git add README.md                       # Mark changes for commit
$ git commit -m "reading02: README.md"    # Record changes
...
$ $EDITOR exists.sh                       # Edit exists shell script
$ git add exists.sh                       # Mark changes for commit
$ git commit -m "reading02: exists.sh"    # Record changes
...
$ git push                                # Send changes to GitLab