Key Word(s): Shell customization, environment variables, I/O, Unix scripting
Lecture Exercise 2¶
Deliverables¶
The deliverables for this exercise are two files: LE2A.sh
and LE2B.sh
. They should both be in the lectures/L2/
directory. You may push directly to master
. There is no need to push auxiliary files.
Part A¶
Write a bash script that checks all the files in the directory and prints out the filename if it is executable by you (the user). Save this file as LE2A.sh
.
Note: For testing purposes, you should create an executable script in your directory to make sure your script picks up on it.
Note: To test if a file is executable by other groups or the world, the find
command may be a better option (either in a bash
script or directly from the command line). Testing this case is not required for this exercise.
Hints¶
You should make use of the command substitution feature of the bash shell. Before coding this up in your bash script, try this command from the command line: echo "$(ls)"
. What happens?
Concepts Covered¶
- Command substitution
- For loops
- Conditionals
- File tests
Part B¶
Write a bash script to "automate" your (responsible) git
workflow. Call it LE2B.sh
. The script should do the following:
- Prompt the user for a file to commit (hint: use
read -r -p
- Stage the file (remember, this is just
git add
) - Display the results of
git status
. - Ask the user if they wish to continue (
Y
orN
). If they respondN
then exit (hint, you can useexit 1
). - If they say
Y
then prompt the user for a commit message. - Commit the file to the local repo with the specified message (
git commit -m
). - Display the results of
git status
. - Ask the user if they wish to continue (
Y
orN
). If they respondN
, then exit. - If they say
Y
then do agit push
.
Note: This script is very limited, but a nice illustration of a simple git
workflow. Feel free to expand it (e.g. use while loops, etc). However, please continue to use git
best practices. For example, do not use git add .
!
Hints:¶
- Make sure you work in your
lectures/L2/
subdirectory of your course repo. - You might want to create a dummy file to commit (e.g.
touch dummy
will create an empty file).
Concepts Covered¶
- Reading input from the user
- Conditional statements
- String comparisons