Shell Scripting¶
A shell
script is a file containing a series of commands that the shell interprets and executes.
Shell
scripts can automate repetitive tasks, manage system operations, or run complex scripts involving multiple commands.Platform: Linux/Unix, macOS
Interpreter: Various (e.g.,
sh
,bash
,zsh
,ksh
)File Extension: Usually none, but can use
.sh
Syntax: Varies slightly depending on the shell
Comments¶
Comments begin with
#
and extend to the end of the line. They are ignored by theshell
.# This is a comment
Variable¶
Variables store data that can be referenced and manipulated.
NAME="John Doe" echo "Hello, $NAME"
Use
export
to make variables available to subprocesses.export NAME="John Doe"
Quoting¶
Quoting prevents the shell
from interpreting special characters.
Double Quotes (
"
): Preserve the literal value of all characters except$
,'
, and\
.echo "Hello, $NAME"
Single Quotes (
'
): Preserve the literal value of all characters within the quotes.echo 'Hello, $NAME'
Backslash (
\
): Escapes a single character.echo "Hello, \$NAME"
Control Structures¶
Conditional Statements¶
if-elif-else-then-else
if [ "$NAME" = "John" ]; then echo "Hello, John" elif [ "$NAME" = "Doe" ]; then echo "Hello, Doe" else echo "You are not John or Doe" fi
Loops¶
for loop
for i in 1 2 3 4 5; do echo "Iteration $i" done
while Loop
[ $COUNT -le 5 ]
uses the-le
(less than or equal to) operator to compareCOUNT
with 5.COUNT=1 while [ $COUNT -le 5 ]; do echo "Iteration $COUNT" COUNT=$((COUNT + 1)) done
Functions¶
Basic Definition
my_function() { echo "This is a function" } my_function
Functions can accept parameters.
greet() { echo "Hello, $1" } greet "John"
Backup¶
#!/bin/bash
# Backup script
SOURCE_DIR="/path/to/source"
DEST_DIR="/path/to/backup"
if [ -d "$SOURCE_DIR" ]; then
cp -r "$SOURCE_DIR" "$DEST_DIR"
echo "Backup completed successfully."
else
echo "Source directory does not exist."
fi
Same example used in Batch¶
$HOME
is used instead of%USERPROFILE%
for the user's home directory.[ ! -d "$SOURCE_DIR" ]
checks if the source directory does not exist.cp -r "$SOURCE_DIR" "$DEST_DIR"
copies the directory recursively.$?
checks the exit status of the last command.export PATH="$DEST_DIR:$DEST_DIR/Scripts:$PATH"
modifies thePATH
variable.#!/bin/bash # Set source and destination directories SOURCE_DIR="/c/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3" DEST_DIR="$HOME/.local/share/ESRI/conda/envs/arcgispro-py3-addins" # Check if the source directory exists if [ ! -d "$SOURCE_DIR" ]; then echo "Source directory does not exist: $SOURCE_DIR" exit 1 fi # Copy the source directory to the destination echo "Copying from $SOURCE_DIR to $DEST_DIR..." cp -r "$SOURCE_DIR" "$DEST_DIR" if [ $? -ne 0 ]; then echo "Failed to copy the directory." exit 1 fi echo "Directory copied successfully." # Check if the constructed path exists if [ -d "$DEST_DIR" ]; then # Set environment variables to mimic activation export CONDA_DEFAULT_ENV="arcgispro-py3-addins" export CONDA_PREFIX="$DEST_DIR" # Add the environment's Scripts directory to the PATH export PATH="$DEST_DIR:$DEST_DIR/Scripts:$PATH" # Installing required libraries from requirements.txt... pip install -r requirements.txt if [ $? -eq 0 ]; then echo "Libraries installed successfully." else echo "Failed to install libraries." exit 1 fi else echo "Failed to copy the directory." exit 1 fi # Pause the script (similar to 'pause' in batch files) read -p "Press any key to continue..."
Usage
- Save the script to a file, for example,
script.sh
. - Make the script: executable:
chmod +x script.sh
- The
chmod +x
command is used to change the permissions of a file to make it executable. The+x
option adds execute permissions to the file.
- The
- Run the script:
./script.sh
- Save the script to a file, for example,