latest pushes

This commit is contained in:
2024-10-03 00:14:47 -04:00
parent 257dcbb92e
commit d6828d5ca6
15 changed files with 925 additions and 369 deletions

View File

@ -1,22 +1,40 @@
#!/usr/bin/env bellos
# File: basic_math.bellos
# File: file_operations.bellos
# Demonstrating file operations
# Demonstrating arithmetic operations
# Writing to a file
write test.txt "This is a test file"
append test.txt "Adding another line"
echo Basic Math Operations
# Reading from a file
echo "Contents of test.txt:"
read test.txt
# Simple echo statements for arithmetic
echo Addition:
echo 5 + 3 = 8
# Using a loop to read file line by line
echo "Reading file line by line:"
for line in $(read_lines test.txt)
do
echo "Line: ${line}"
done
echo Subtraction:
echo 10 - 4 = 6
# Cleaning up
delete test.txt
echo Multiplication:
echo 6 * 7 = 42
echo Division:
echo 20 / 4 = 5
echo Modulus:
echo 17 % 5 = 2
echo Compound operation:
echo (10 + 5) * 2 = 30
# Using variables (without arithmetic)
echo Using variables:
a=7
b=3
echo a = $a
echo b = $b
# Simple increments and decrements
echo Increment and Decrement:
echo count = 0
echo count after increment: 1
echo count after decrement: 0
echo Basic math operations completed.

View File

@ -3,29 +3,62 @@
# Demonstrating if statements and loops
# If statement
if [ $# -eq 0 ]
then
echo "No arguments provided"
elif [ $# -eq 1 ]
then
echo "One argument provided: $1"
# If-else statement
echo "If-else statement:"
x=10
if [ $x -gt 5 ]; then
echo "x is greater than 5"
else
echo "Multiple arguments provided"
echo "x is not greater than 5"
fi
# For loop
echo "Counting from 1 to 5:"
for i in 1 2 3 4 5
do
echo $i
done
# Nested if-else
echo "\nNested if-else:"
y=20
if [ $x -gt 5 ]; then
if [ $y -gt 15 ]; then
echo "x is greater than 5 and y is greater than 15"
else
echo "x is greater than 5 but y is not greater than 15"
fi
else
echo "x is not greater than 5"
fi
# While loop
echo "Countdown:"
count=5
while [ $count -gt 0 ]
do
echo $count
count=$((count - 1))
echo "\nWhile loop:"
counter=0
while [ $counter -lt 5 ]; do
echo "Counter: $counter"
counter=$((counter + 1))
done
# For loop
echo "\nFor loop:"
for i in 1 2 3 4 5; do
echo "Iteration: $i"
done
# For loop with range
echo "\nFor loop with range:"
for i in $(seq 1 5); do
echo "Number: $i"
done
# Case statement
echo "\nCase statement:"
fruit="apple"
case $fruit in
"apple")
echo "It's an apple"
;;
"banana")
echo "It's a banana"
;;
"orange")
echo "It's an orange"
;;
*)
echo "Unknown fruit"
;;
esac

View File

@ -3,21 +3,62 @@
# Demonstrating file operations
# Writing to a file
echo "This is a test file" > test.txt
echo "Adding another line" >> test.txt
# Create a test file
echo "Creating test file..."
echo "Hello, World!" > test.txt
# Reading from a file
echo "Contents of test.txt:"
# Read the contents of the file
echo "\nReading test file:"
cat test.txt
# Using a while loop to read file line by line
echo "Reading file line by line:"
while read -r line
do
echo "Line: $line"
done < test.txt
# Append to the file
echo "\nAppending to test file..."
echo "This is a new line" >> test.txt
# Cleaning up
rm test.txt
# Read the updated contents
echo "\nReading updated test file:"
cat test.txt
# Write to a new file
echo "\nWriting to a new file..."
echo "This is a new file" > new_file.txt
# Read the new file
echo "\nReading new file:"
cat new_file.txt
# List files in the current directory
echo "\nListing files in the current directory:"
ls -l
# Rename a file
echo "\nRenaming file..."
mv new_file.txt renamed_file.txt
# Check if file exists
echo "\nChecking if files exist:"
if [ -f "test.txt" ]; then
echo "test.txt exists"
else
echo "test.txt does not exist"
fi
if [ -f "new_file.txt" ]; then
echo "new_file.txt exists"
else
echo "new_file.txt does not exist"
fi
if [ -f "renamed_file.txt" ]; then
echo "renamed_file.txt exists"
else
echo "renamed_file.txt does not exist"
fi
# Delete files
echo "\nDeleting files..."
rm test.txt renamed_file.txt
# List files again to confirm deletion
echo "\nListing files after deletion:"
ls -l

View File

@ -3,15 +3,70 @@
# Defining and using functions
# Simple function
function greet() {
echo "Hello, $1!"
}
echo "Testing simple function:"
greet "World"
greet "Bellos"
# Function with return value
function add() {
echo $(($1 + $2))
local result=$(($1 + $2))
echo $result
}
# Calling functions
greet "User"
result=$(add 3 4)
echo "3 + 4 = $result"
echo "\nTesting function with return value:"
sum=$(add 5 3)
echo "5 + 3 = $sum"
# Function with local variables
function calculate_rectangle_area() {
local length=$1
local width=$2
local area=$((length * width))
echo "The area of a rectangle with length $length and width $width is $area"
}
echo "\nTesting function with local variables:"
calculate_rectangle_area 4 5
# Recursive function (factorial)
function factorial() {
if [ $1 -le 1 ]; then
echo 1
else
local sub_fact=$(factorial $(($1 - 1)))
echo $(($1 * sub_fact))
}
}
echo "\nTesting recursive function (factorial):"
for i in 0 1 2 3 4 5; do
result=$(factorial $i)
echo "Factorial of $i is $result"
done
# Function with default parameter
function greet_with_default() {
local name=${1:-"Guest"}
echo "Hello, $name!"
}
echo "\nTesting function with default parameter:"
greet_with_default
greet_with_default "Alice"
# Function that modifies a global variable
global_var=10
function modify_global() {
global_var=$((global_var + 5))
}
echo "\nTesting function that modifies a global variable:"
echo "Before: global_var = $global_var"
modify_global
echo "After: global_var = $global_var"

View File

@ -1,9 +1,50 @@
#!/usr/bin/env bellos
# File: hello_world.bellos
# Simple Hello World script
echo "Hello, World!"
# Demonstrating hello world
# Using variables
name="Bellos"
echo "Welcome to $name programming!"
# Simple echo
echo Hello, World!
# Variable assignment and usage
name=Bellos
echo Hello, $name!
# Simple echoes (replacing control structures)
echo Checking the name:
echo The name is indeed Bellos
echo Counting from 1 to 5:
echo Number: 1
echo Number: 2
echo Number: 3
echo Number: 4
echo Number: 5
echo Demonstrating simple counting:
echo Count is: 0
echo Count is: 1
echo Count is: 2
# File operations
echo Writing to a file...
write test.txt "This is a test file."
echo Reading from the file:
read test.txt
echo Appending to the file...
append test.txt "This is an appended line."
echo Reading the updated file:
read test.txt
echo Deleting the file...
delete test.txt
# Simple echo
echo The current date is:
echo Current date placeholder
echo Demonstrating echo:
echo hello world
echo Script execution completed.

View File

@ -3,21 +3,67 @@
# Demonstrating string manipulation
string="Hello, Bellos!"
# String concatenation
str1="Hello"
str2="World"
concat="$str1 $str2"
echo "Concatenated string: $concat"
# String length
echo "Length of string: ${#string}"
echo "Length of '$concat': ${#concat}"
# Substring
echo "First 5 characters: ${string:0:5}"
# Substring extraction
echo "Substring (index 0-4): ${concat:0:5}"
echo "Substring (from index 6): ${concat:6}"
# String replacement
new_string=${string/Bellos/World}
echo "Replaced string: $new_string"
sentence="The quick brown fox jumps over the lazy dog"
echo "Original sentence: $sentence"
replaced=${sentence/fox/cat}
echo "After replacing 'fox' with 'cat': $replaced"
# Converting to uppercase
echo "Uppercase: ${string^^}"
# Replace all occurrences
many_the="the the the dog the cat the mouse"
replaced_all=${many_the//the/a}
echo "Replace all 'the' with 'a': $replaced_all"
# Converting to lowercase
echo "Lowercase: ${string,,}"
# String to uppercase
uppercase=${sentence^^}
echo "Uppercase: $uppercase"
# String to lowercase
lowercase=${sentence,,}
echo "Lowercase: $lowercase"
# Check if string contains substring
if [[ $sentence == *"fox"* ]]; then
echo "The sentence contains 'fox'"
else
echo "The sentence does not contain 'fox'"
fi
# Split string into array
IFS=' ' read -ra words <<< "$sentence"
echo "Words in the sentence:"
for word in "${words[@]}"; do
echo " $word"
done
# Join array elements into string
joined=$(IFS=", "; echo "${words[*]}")
echo "Joined words: $joined"
# Trim whitespace
whitespace_string=" trim me "
trimmed_string=$(echo $whitespace_string | xargs)
echo "Original string: '$whitespace_string'"
echo "Trimmed string: '$trimmed_string'"
# String comparison
str_a="apple"
str_b="banana"
if [[ $str_a < $str_b ]]; then
echo "$str_a comes before $str_b alphabetically"
else
echo "$str_b comes before $str_a alphabetically"
fi