Mathematical operations and logical testing utilities for performing calculations, generating sequences, and evaluating conditions within shell scripts and command pipelines.
Evaluate mathematical and logical expressions.
/**
* Evaluate expressions
* WebAssembly binary: dist/wasm/bin/expr
*
* Usage patterns:
* - expr expression
* - Supports arithmetic: +, -, *, /, %
* - Supports comparison: =, !=, <, <=, >, >=
* - Supports logical: &, |
* - String operations: length, substr, index, match
*/Factor integers into prime components.
/**
* Factor integers into primes
* WebAssembly binary: dist/wasm/bin/factor
*
* Usage patterns:
* - factor [number...]
* - Factors positive integers into prime components
* - Reads from stdin if no numbers provided
*/Generate arithmetic sequences of numbers.
/**
* Generate number sequences
* WebAssembly binary: dist/wasm/bin/seq
*
* Usage patterns:
* - seq [options] last
* - seq [options] first last
* - seq [options] first increment last
* - Supports -s (separator), -w (equal width), -f (format)
*/Evaluate conditional expressions and return appropriate exit codes.
/**
* Evaluate conditional expressions
* WebAssembly binary: dist/wasm/bin/test
*
* Usage patterns:
* - test expression
* - [ expression ]
* - File tests: -e (exists), -f (regular file), -d (directory)
* - String tests: -z (empty), -n (non-empty), = (equal)
* - Numeric tests: -eq, -ne, -lt, -le, -gt, -ge
*/Return fixed boolean values for logic operations.
/**
* Return false exit status
* WebAssembly binary: dist/wasm/bin/false
*
* Usage patterns:
* - false
* - Always returns exit code 1
* - Used in conditional logic and loops
*/Mathematical calculations:
const { path } = require("@cowasm/coreutils");
const { join } = require("path");
// Access math utility binaries
const binPath = join(path, "bin");
const exprBinary = join(binPath, "expr");
const factorBinary = join(binPath, "factor");
const seqBinary = join(binPath, "seq");
// These would be executed via your WebAssembly runtimeExpression evaluation:
# Arithmetic operations
expr 5 + 3 # Returns: 8
expr 10 - 4 # Returns: 6
expr 6 \* 7 # Returns: 42 (escape * in shell)
expr 15 / 3 # Returns: 5
expr 17 % 5 # Returns: 2
# Comparison operations
expr 5 \> 3 # Returns: 1 (true)
expr 2 \< 8 # Returns: 1 (true)
expr 4 = 4 # Returns: 1 (true)
expr 3 != 5 # Returns: 1 (true)
# String operations
expr length "hello" # Returns: 5
expr substr "hello" 2 3 # Returns: ell
expr index "hello" "e" # Returns: 2
expr match "hello" "h.*o" # Returns: 5Integer factorization:
# Factor single numbers
factor 60 # Returns: 60: 2 2 3 5
factor 17 # Returns: 17: 17 (prime)
factor 100 # Returns: 100: 2 2 5 5
# Factor multiple numbers
factor 12 15 20 # Factors each number
# Factor from stdin
echo "24 36 48" | factor # Factors each number from inputSequence generation:
# Simple sequences
seq 5 # Returns: 1 2 3 4 5
seq 3 7 # Returns: 3 4 5 6 7
seq 2 2 10 # Returns: 2 4 6 8 10
# Custom formatting
seq -s ", " 1 5 # Returns: 1, 2, 3, 4, 5
seq -w 8 12 # Returns: 08 09 10 11 12
seq -f "%.2f" 1 0.5 3 # Returns: 1.00 1.50 2.00 2.50 3.00
# Reverse sequences
seq 5 -1 1 # Returns: 5 4 3 2 1Conditional testing:
# File tests
test -f /path/to/file # True if file exists and is regular file
test -d /path/to/dir # True if directory exists
test -e /path/to/item # True if item exists (any type)
test -r /path/to/file # True if file is readable
test -w /path/to/file # True if file is writable
test -x /path/to/file # True if file is executable
# String tests
test -z "$empty_var" # True if string is empty
test -n "$non_empty_var" # True if string is non-empty
test "$str1" = "$str2" # True if strings are equal
test "$str1" != "$str2" # True if strings are not equal
# Numeric tests
test "$num1" -eq "$num2" # True if numbers are equal
test "$num1" -ne "$num2" # True if numbers are not equal
test "$num1" -lt "$num2" # True if num1 < num2
test "$num1" -le "$num2" # True if num1 <= num2
test "$num1" -gt "$num2" # True if num1 > num2
test "$num1" -ge "$num2" # True if num1 >= num2
# Logical operators
test -f file -a -r file # True if file exists AND is readable
test -f file -o -d file # True if item is file OR directory
test ! -f file # True if file does NOT exist# Complex arithmetic using expr
calculate_area() {
local length=$1
local width=$2
expr "$length" \* "$width"
}
# Percentage calculations
percentage() {
local part=$1
local total=$2
expr "$part" \* 100 / "$total"
}# Generate ranges for loops
for i in $(seq 1 10); do
echo "Processing item $i"
done
# Conditional loops with false
# Note: 'true' command not available, use alternative approaches for infinite loops
# Use false for never-executing loops
while false; do
echo "This never executes"
done# Validate numeric input
is_number() {
local input=$1
# Test if input is a valid number
expr "$input" + 0 >/dev/null 2>&1
}
# Validate file existence
validate_file() {
local filename=$1
if test -f "$filename"; then
echo "File $filename exists and is readable"
return 0
else
echo "File $filename not found or not readable"
return 1
fi
}# Generate specific number patterns
fibonacci_start() {
# Generate Fibonacci-like start (1, 1, 2, 3, 5...)
local n=$1
if test "$n" -le 2; then
echo 1
else
local prev1=$(fibonacci_start $((n-1)))
local prev2=$(fibonacci_start $((n-2)))
expr "$prev1" + "$prev2"
fi
}
# Powers of 2 using seq and expr
powers_of_2() {
local max_power=$1
for i in $(seq 0 "$max_power"); do
expr 2 \* 2 \* 2 # Would need to be calculated properly
done
}These utilities are essential for shell script logic:
#!/bin/bash
# Example script using math and logic utilities
# Input validation
if test $# -eq 0; then
echo "Usage: $0 <number>"
exit 1
fi
number=$1
# Validate input is numeric
if ! expr "$number" + 0 >/dev/null 2>&1; then
echo "Error: '$number' is not a valid number"
exit 1
fi
# Check if number is positive
if test "$number" -gt 0; then
echo "Factoring $number:"
factor "$number"
echo "Sequence from 1 to $number:"
seq 1 "$number"
else
echo "Please provide a positive number"
exit 1
fi