Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In the previous class, you created your first bash script, edited $PATH variables to create custom commands and added aliases to your ~ / .
In the previous class, you created your first bash script, edited $PATH variables to create custom commands and added aliases to your ~bashrc file.
Now, we will add variables and conditional if statements to our scripts.
If statements improve our scripts by allowing us to:
Check if a file or directory exists before we create it
Check what user has run the script.
And more...
To complete this activity, you will create variables and if statements that satisfy given requirements and use them in your script.
Instructions
Consult this list of bash conditional operators while completing the activity:
: Checks if two items are equal.
: Checks if two items are not equal.
gt : Checks if an integer is greater than another.
lt : Checks if an integer less than another.
d pathtodirectory : Checks for existence of a directory.
&& : Both conditions have to be true to run.
: One condition has to be true to run.
Using Variables
Create a variable to hold the path of your output file.
Replace the output file path for each command with your variable.
Using If Statements
Create an if statement that checks for the existence of the ~research directory.
If the directory exists, do nothing. If the directory does not exist, create it
Remove the line in your script that creates this directory.
Create an if statement that checks for the existence of the file ~researchsysinfo.txt
If the file does not exist, do nothing.
If the file does exist, remove itThis will ensure that the script always creates a new file,
Bonus Variables
If you have already completed the exercises above, see if you can complete the following:
Create a variable to hold the output of the command: ip addr grep inet tail head
Replace this command in your script with your new variable.
Create a variable to hold the output of the command: find home type f perm
Replace this command in your script with your new variable.
Bonus If Statement
Protect the script from root! Create an if statement that checks if the script was run using sudo.
If it was run with sudo, exit the script with a message that tells the user not to run the script using sudo.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started