Linux Shell - Scripting
Introduction
Section titled “Introduction”What is a Script?
Section titled “What is a Script?”A shell script is simply a text file containing commands that you could type yourself. The power comes from chaining commands together and reusing them.
Think of it like recording a macro in Excel: instead of repeating steps by hand, you write them down once and run them whenever needed.
Structure of a Script
Section titled “Structure of a Script”-
Shebang line: tells Linux which interpreter to use.
#!/bin/bashWithout this, the system might use
/bin/sh(which may behave differently). -
Commands: everything you would type manually.
-
Comments: begin with
#. Comments are ignored by the shell but help humans understand.
Running a Script
Section titled “Running a Script”- Save it as
myscript.sh. - Make it executable:
Terminal window chmod +x myscript.sh - Run it:
The
Terminal window ./myscript.sh./ensures the shell looks in the current directory.
Alternatively, you can run it explicitly with an interpreter:
bash myscript.shVariables
Section titled “Variables”Variables let you store values.
- Create:
Terminal window name="Alice" - Use:
Terminal window echo "Hello $name"
By default, variables exist only inside the current shell. If you want them to be inherited by child processes, you need export:
export PATH=$PATH:/usr/local/binScript Arguments
Section titled “Script Arguments”Scripts can accept inputs. This is essential if you want them to be reusable.
$0→ script name$1,$2… → first, second argument$@→ all arguments
Example:
#!/bin/bashecho "This script is: $0"echo "First arg: $1"echo "All args: $@"Run it:
./myscript.sh apple bananaOutput:
This script is: ./myscript.shFirst arg: appleAll args: apple bananaThis is similar to function parameters in other languages.
Exit Codes
Section titled “Exit Codes”Every command returns a number ($?).
0means success.- Anything else means an error.
This is how you can make scripts react to success or failure.
Logic and Testing in Scripts
Section titled “Logic and Testing in Scripts”Scripts become powerful when they can make decisions. That’s where conditions come in.
If/Else Statements
Section titled “If/Else Statements”if [ condition ]; then echo "True"else echo "False"fiThe [ ] is actually a command called test.
File Tests
Section titled “File Tests”The test command can check properties of files:
-f file→ exists and is a regular file.-d dir→ exists and is a directory.-x file→ file exists and is executable.
Example:
if [ -f /etc/passwd ]; then echo "passwd file exists"fiThis is used all the time in installation or configuration scripts to check whether a file or directory already exists before overwriting it.
String and Number Tests
Section titled “String and Number Tests”[ "$a" = "$b" ]→ strings equal[ "$a" != "$b" ]→ strings not equal[ 5 -gt 3 ]→ greater than[ 2 -le 7 ]→ less or equal
Case Statements
Section titled “Case Statements”Instead of many if statements, you can use case to handle multiple possibilities:
case $1 in start) echo "Starting service" ;; stop) echo "Stopping service" ;; *) echo "Usage: $0 {start|stop}" ;;esacThis is often used for system scripts where the user might type ./script.sh start or ./script.sh stop. It improves readability compared to multiple if/else blocks.
Loops and Automation
Section titled “Loops and Automation”Loops let you repeat actions automatically.
For Loops
Section titled “For Loops”Best for iterating over lists.
for file in *.txt; do echo "Processing $file"doneThis is equivalent to PowerShell’s foreach ($f in Get-ChildItem).
While Loops
Section titled “While Loops”Run as long as a condition is true.
count=1while [ $count -le 5 ]; do echo "Count = $count" count=$((count + 1))doneThis is like PowerShell’s while ($true) { ... }, except you normally put a real condition instead of $true.
Until Loops
Section titled “Until Loops”The opposite of while: run until the condition becomes true.
until [ -f /tmp/stopfile ]; do echo "Waiting..." sleep 5doneThis is useful for waiting on a process to finish, or a file to appear, without writing complicated checks.