Table of Contents
Syntax
If statements in gosh follow the standard POSIX shell syntax:
Basic If Statement
if condition
then
# commands
fi
If-Else Statement
if condition
then
# commands when true
else
# commands when false
fi
If-Elif-Else Statement
if condition1
then
# commands when condition1 is true
elif condition2
then
# commands when condition2 is true
else
# commands when all conditions are false
fi
Features
- Basic if-then-else statements with proper condition evaluation
- Multi-line syntax with separate
then
,else
, andfi
keywords - elif branches for multiple conditions
- Integration with test command for numeric, string, and file comparisons
- Function support - if statements work inside user-defined functions
- Variable expansion in conditions
- Return statement handling within if blocks
- Interactive and script support - works in both interactive mode and script files
Test Command Operators
The test
command is commonly used with if statements for condition evaluation:
Numeric Comparisons
-eq
- Equal to-ne
- Not equal to-lt
- Less than-le
- Less than or equal to-gt
- Greater than-ge
- Greater than or equal to
String Comparisons
=
- String equality!=
- String inequality-z
- String is empty-n
- String is not empty
File Tests
-f
- File exists and is a regular file-d
- File exists and is a directory-e
- File exists-r
- File is readable-w
- File is writable-x
- File is executable
Examples
Basic Conditional Logic
#!/usr/local/bin/gosh
# Simple numeric comparison
if test 5 -gt 3
then
echo "5 is greater than 3"
fi
# String comparison
local name="gosh" # Local variable
if test $name = "gosh"
then
echo "Welcome to gosh!"
else
echo "This is not gosh"
fi
Function Argument Checking
#!/usr/local/bin/gosh
check_args() {
if test $# -eq 0
then
echo "No arguments provided"
return 1
else
echo "Received $# arguments"
return 0
fi
}
check_args
check_args arg1 arg2
File Backup Script
#!/usr/local/bin/gosh
backup_file() {
if test $# -eq 0
then
echo "Usage: backup_file "
return 1
fi
if test -f $1
then
local backup_name="$1.backup.$(date +%Y%m%d)" # Local variable
cp $1 $backup_name
echo "Backup created: $backup_name"
else
echo "Error: File $1 does not exist"
return 1
fi
}
backup_file "important.txt"
System Information Script
#!/usr/local/bin/gosh
check_system() {
local current_user="$(whoami)" # Local variable
local current_dir="$(pwd)" # Local variable
if test $current_user = "root"
then
echo "Warning: Running as root user"
else
echo "Running as user: $current_user"
fi
if test -d $current_dir
then
echo "Current directory: $current_dir"
if test -w $current_dir
then
echo "Directory is writable"
else
echo "Directory is read-only"
fi
fi
}
check_system
Nested If Statements
#!/usr/local/bin/gosh
check_directory() {
if test -d $1
then
echo "Directory $1 exists"
if test -w $1
then
echo "Directory is writable"
if test -r $1
then
echo "Directory is readable"
else
echo "Directory is not readable"
fi
else
echo "Directory is read-only"
fi
else
echo "Directory $1 does not exist"
fi
}
check_directory $1
Important Notes
- Multi-line support: If statements are supported in both script files and interactive mode. In interactive mode, the shell automatically detects when you start an if statement and continues reading lines until you type
fi
- Multi-line format: Use separate lines for
if
,then
,else
, andfi
keywords - Nested if statements: gosh fully supports nested if statements. You can place if statements inside other if statements, and the parser will correctly match each
if
with its correspondingfi
- Condition evaluation: Conditions are evaluated using the exit status of commands (0 = true, non-zero = false)
- Variable expansion: Variables are expanded within conditions, so use proper quoting when necessary
- Return statements: Can be used within if blocks in functions to control function exit
- Test command: The
test
command is the most common way to create conditions