Shell Scripting Study Guide This guide distills essential concepts and practices for creating, running, and managing shell scripts, drawing from various sources including "FAQ ShellScripting includes variables", "Introduction to Shellscripting", and "TLDR CIS117 Week 8". -------------------------------------------------------------------------------- I. Introduction to Shell Scripting • What is a Script? ◦ A shell script is a small program. ◦ It's a text file with one command per line. ◦ They are often referred to as "shell scripting" or "bash scripting". • Why Use Shell Scripts? ◦ Automation: They are excellent for repetitive tasks and repeated activities. ◦ Problem Solving: They help you "figure out a puzzle and keep the solution for that puzzle" for future use. ◦ Consistency: Ensures you "don't forget anything" by documenting the necessary commands. ◦ Troubleshooting: Useful for creating baselines on servers and networks to compare data over time (e.g., comparing network packets). ◦ Complicated Tasks: Good for complex operations you don't perform often, so you don't have to re-figure them out. -------------------------------------------------------------------------------- II. Creating and Editing a Shell Script • File Naming: ◦ While not technically required by Linux, it's highly helpful to add a file extension like .sh (e.g., river.sh) to indicate it's a shell script and which language was used. • Text Editor: ◦ Scripts are created and edited in a text editor like vi. • The Shebang Line: ◦ This is the first line of your script and is crucial for telling the system how to execute it. ◦ It starts with a hashtag (#) and an exclamation point (!), followed by the path to the interpreter. ◦ Common examples: ▪ #!/bin/sh (for a general shell script) ▪ #!/bin/bash (for a Bash script) ◦ It's not strictly required for the script to run, but it makes the script's type clear and is considered good practice. • Comments: ◦ Use the hashtag symbol (#) at the beginning of a line to add comments. ◦ It's good practice to include a comment at the top of your script detailing its function, your name, and the date. -------------------------------------------------------------------------------- III. Basic Scripting Elements • echo Command: ◦ Used to print something out to the command line. • read Command: ◦ Used to accept input from the user. ◦ It pauses the script and waits for user input, then stores it in a specified variable. • Variables: ◦ Shell scripting uses untyped variables, meaning you don't need to declare their type (like int or character). ◦ You don't need to declare them at the top of the script. ◦ To use or expand a variable's value, precede its name with a dollar sign ($) (e.g., echo $answer). -------------------------------------------------------------------------------- IV. File Permissions and Execution • Default Permissions: ◦ When you create a new file in Linux, it is not executable by default. • Changing Permissions (chmod): ◦ You must change permissions to add execute permission for the script to run. ◦ This is often done using the chmod command. An FAQ video provides a demonstration of this process. • Running a Script: ◦ To run a script, you typically need to provide the path to the script (e.g., ./river.sh if it's in your current directory). ◦ A common mistake is not pointing to the script correctly. This is also demonstrated in the FAQ video. -------------------------------------------------------------------------------- V. Types of Text and Quotes The way you quote text with echo affects how variables are handled: • Plain (No Quotes): ◦ Example: echo hello ◦ Prints the text literally. • Semi-Opaque (Double Quotes): ◦ Example: echo "hello $name" ◦ Expands variables inside the quotes. • Opaque (Single Quotes): ◦ Example: echo 'hello $name' ◦ Shows exactly what's in the quotes, meaning variables will not be expanded. • Backticks (`): ◦ Used to execute a command within the script and use its output. ◦ The results of a command executed with backticks can be put into temporary variables (e.g., set \date``). ◦ Important: A backtick is not the same as a single quote. On an American keyboard, the backtick is typically above the Tab key, next to the tilde (~). -------------------------------------------------------------------------------- VI. Best Practices for Successful Scripting • Test Commands First: ◦ Always test your individual commands at the command prompt before putting them into a script. This practice becomes more natural with experience. • The Sequence for a Successful Script: 1. Test commands at the command prompt. 2. Create and edit the file in a text editor (like VIM). 3. Start the script with a shebang line (e.g., #!/bin/bash). 4. Add a comment at the top with the script's function, your name, and the date. 5. Save the file. 6. Change permissions to make it executable. 7. Test! 8. Rinse and repeat (for further modifications and debugging). • Thorough Testing is CRUCIAL: ◦ "PLEASE TEST YOUR CODE". ◦ Never turn in or use untested code. ◦ Be prepared to prove you've tested your code (e.g., with screenshots or by copy-pasting code) for assignments. Avoid explanations like "I think it works" or "it should have worked". -------------------------------------------------------------------------------- VII. Suggested Activities • Hands-on Practice: ◦ Follow along with videos like Adrianna Holden-Gouveia's "FAQ ShellScripting includes variables". ◦ Create a dummy file or folder [Conversation History]. ◦ Experiment with ls -l to view permissions [Conversation History]. ◦ Play around with chmod using octal numbers to change permissions [Conversation History]. ◦ Try the different types of echo quotes with a basic "hello world" and a variable to observe their behavior. • Terminal Exercises: ◦ Open your terminal and try pinging a website [Conversation History]. ◦ Run ip a (or ifconfig) and ss to see active network connections [Conversation History].