Operating Systems Concepts & Design
getopts and Error HandlingMake sure to return to the AWS Learner Lab page (link in Pilot if you forgot to bookmark it) and hit “Start Lab” to turn on your sandbox / AWS instance.
Use ssh to connect to your AWS Ubuntu instance.
Go to the folder that contains your repository (likely named ceg2350-yourgithubusername).
Create a new directory, Lab06.
Create a file named README.md in the Lab06 folder. The Lab 06 Template can be copied from this link:
https://raw.githubusercontent.com/pattonsgirl/CEG2350/refs/heads/main/docs/Labs/Lab06/LabTemplate.mdYou may refer to additional resources outside of the recommended resources provided.
Any resource that you use that contributes to your understanding of exercises in this lab should be cited in the Citations section of your lab answers. To add citations, provide the site and a summary of what it assisted you with. If generative AI was used, include which generative AI system was used and what prompt(s) you fed it.
If you make mistakes with commands in the lab, note them! Writing down what went wrong and what the correction was will significantly help your learning journey. If you cannot find a correction, it will help the TAs or myself understand what point you reached and determine some potential solutions.
Craft an alias with a name of your choice that pipes a fortune to a cowsay cow of your choice (-f flag)
Craft an alias with a name of your choice to something “fun”. Checkout these as suggestions. You may need to install these programs first with apt.
curl wttr.incat use lolcat insteadasciiaquariumAdd the aliases to a file named .bash_aliases in your user’s home directory.
Read through the .bashrc file in your user’s home directory for a section that is relevant to “enabling” usage of the .bash_aliases file. Copy and paste the snippet into your lab template and describe what it does.
Confirm that your aliases continuously work as long as the conditions are met.
Copy your .bash_aliases file to your GitHub repository in your Lab06 folder.
You may delete the .bash_aliases file in your user’s home directory as needed through this lab.
Resources
Your tasking is to write a script that sets up your user account to use your .bash_aliases To accomplish this, you will create an “installation” script that accomplishes these taskings.
WARNING
You must have a minimum of 3 (THREE) COMMITS made for this script. Your commit messages must describe what’s currently working / tested. When (after which steps / testing) you make a commit is your choice. Your commits must demonstrate an incremental building process - a bulk dump and then making a spelling change or two will not get you out of the penalty in the rubric.
END WARNING
Create a script named dotinstall in your Lab06 folder.
Create a function called printHelp. printHelp should output the following:
Usage: dotinstall [-OPTION] [ARG]
-s setup - attempts to create a symbolic link .bash_aliases file to user's home directory
-d disconnect - removes symbolic link
-a append - adds a new alias to .bash_aliases file
-r remove - removes an alias from .bash_aliases file
Use getopts to read in options and save arguments that correlate with options. getopts should support the following options in the OPTSTRING
-h
printHelp function and exit script-s
.bash_aliases in the Lab06 folder to a .bash_aliases in the user’s home directory.bash_aliases already exists, prompt the user to confirm the most appropriate action before overwriting the existing file-d
-a
.bash_aliases-r
.bash_aliases\?
OPTSTRINGprintHelp and exit script# Sample runs of working script
$ bash dotinstall -h
Usage: dotinstall [-OPTION] [ARG]
-s setup - attempts to create a symbolic link .bash_aliases file to user's home directory
-d disconnect - removes symbolic link
-a append - adds a new alias to .bash_aliases file
-r remove - removes an alias from .bash_aliases file
$ bash dotinstall -s -a 'alias ls="ls -lah"'
No .bash_aliases file found in /home/ubuntu
Creating symbolic link in /home/ubuntu named .bash_alaises
Adding alias to .bash_alaises
User should the following to reload the shell:
source ~/.bashrc
$ bash dotinstall -s -a "alias ls=\"ls -lah\""
No .bash_aliases file found in /home/ubuntu
Creating symbolic link in /home/ubuntu named .bash_alaises
Adding alias to .bash_alaises
User should the following to reload the shell:
source ~/.bashrc
$ bash dotinstall -r ls
Found alias for ls in .bash_aliases
Alias for ls removed from .bash_aliases
$ bash dotinstall -d
Removing symbolic link in /home/ubuntu named .bash_alaises
User should the following to reload the shell:
source ~/.bashrc
$ bash dotinstall -z
Option not supported.
Usage: dotinstall [-OPTION] [ARG]
-s setup - attempts to create a symbolic link .bash_aliases file to user's home directory
-d disconnect - removes symbolic link
-a append - adds a new alias to .bash_aliases file
-r remove - removes an alias from .bash_aliases file
Resources
Before panicking, keep in mind that getopts is mostly a while loop iterating through arguments with a switch statement to define what to do on finding certain options. Play with the demos linked below before diving all in. Plan plan plan!
getopts tutorial - shellscriptgetopts works - assertnotmagicgetopts - ostechnixgetopts to a friend?Fill out the Usage Guide section in your lab template. It should contain a minimum of the following:
.bash_aliasesResources
Examples of bad README / Usage Guides:
Examples of good README / Usage Guides:
Add a feature to the dotinstall script. Ideas:
.bash_aliases. If its use doesn’t error, then add it. If it does, output the error to the user and exit so that they can fix it before they run the script again or prompt them to provide an alternativeAdd notes to your documentation about your added feature and show examples of it in action.
Verify that your GitHub repo has a Lab06 folder with at minimum:
README.mddotinstall.bash_aliasesIn the Pilot Dropbox, paste the URL to the Lab06 folder in your GitHub repo
Your files should be cleanly presented in your GitHub repository. Citations should be included as needed. Include which generative AI system was used and what prompts were used if generative AI was used.
getopts and error handlingI don’t not require any error handling outside of the sample program runs provided. However…
If you are reading this, you may have noted that getopts still doesn’t solve all user abuse cases by default.
For example, getopts can have a hard time “detecting” a missing argument after an option. bash dotinstall -s -d would not “count” as -r missing an argument, because OPTARG would read -d as the argument for -s. Fun right?
There are many ways to detangle this problem, some using getopts. You could play some quick games before running getopts, such as checking number of arguments passed… It isn’t failsafe, but it would help.
So let’s use what we have - stored values in variables. And we have an ability to check if those values contain anything… catch my drift? Check out the second answer in this post: https://unix.stackexchange.com/questions/50563/how-can-i-detect-that-no-options-were-passed-with-getopts
There are games you can play within the case statement to do this check, but it’s a little clunky. If you’re curious: https://stackoverflow.com/questions/43425556/getopts-behaves-not-as-expected-when-option-parameter-is-missing