Welcome to QA's lab guide for Practicing Shell Scripting Fundamentals. Scripting can be the most frustrating area for new administrators, but automation is a foundational skill to possess in the career field. In this hands-on lab, QA walks you through some of the basics of writing shell scripts.
Because this is a foundational lab, we want to take some extra time to help walk beginners through some steps.
Step 1 - Accessing the Amazon Linux 2 Terminal
First, ensure that the lab environment has loaded 100% prior to clicking it.
Once the environment is fully loaded, click on Open development env, and it should open a new tab. Please log in using provided details from this step:https://app.qa.com/lab/practicing-shell-scripting-fundamentals/accessing-the-amazon-linux-2-terminal/.
The username is 'ca' and there is no password, so just click enter on your keyboard
Also from this step, enter the command 'tree' in the prompt to display the directories:
Once finished, please click "Next"
Step 2 - Modifying Your Lab Shell Script
In step 2, you'll find different ways of modifying files in the shell. The options mentioned are:
-
-
Vim: vim SCRIPT_NAME
-
Nano: nano SCRIPT_NAME
-
Emacs: emacs SCRIPT_NAME
-
In case you are not yet familiar with these commands, please refer to the below lectures. You may then click next once you are done reading up on these options.
Step 3 - Declaring and Using Shell Variables
For step 3, you are given the following prompt:
Inlab_script.sh
, declare a variableFILE
and set the value to the stringsample.txt
. Reference the variable within a string to echoFile to test: sample.txt
to the screen when thelab_script.sh
is run.
To do this, enter the below command onto the shell. Remember that you would need to edit lab_script.sh, as in the previous step, you would use either VIM, Nano, or emacs. I have used nano for this lab.
nano lab_script.sh
The file will open in the terminal. The first code you would need to add is...
FILE="sample.txt"
echo "File to test: ${FILE}"
It should look like this in the shell:
To save, click CTRL+S.
This should let you pass the check:
Click next.
Step 4 - Incorporating Tests with Conditional Statements
Another mission is stated. To pass, use the below code:
if [ -s $FILE ]
then
echo "File exists and contains text."
elif [ -e $FILE ]
then
echo "File exists but is empty."
else
echo "File does not exist."
fi
This should let you pass the check:
Make sure to paste it in the correct location, it should look like the screenshot below:
To save, click CTRL+S once again.
This should let you pass the check, and you may then click next for the last step.
Step 5 - Looping Through Command Line Arguments
To pass this final check please the code below:
for VALUE in $@
do
echo "Parameter: ${VALUE}"
done
Make sure this is pasted on the correct location as well, it should look like this:
Click CTRL+S once again to save.
This should let you pass the last check...
and finish the lab...
I hope this walkthrough helps and that you have been able to pass the hands-on lab. If you have any additional questions or concerns please reach out to support@cloudacademy.com.
Happy Training!
Comments
0 comments
Article is closed for comments.