actions_example/generate-primes.sh
kiper220 a5430fb5fb
All checks were successful
Caching Primes / build (push) Successful in 9s
/ ls (push) Successful in 4s
feat: add demo workflows
2025-02-07 22:48:39 +04:00

52 lines
No EOL
1,013 B
Bash
Executable file

#!/bin/bash
# Function to check if a number is prime
is_prime() {
local num=$1
if [ $num -lt 2 ]; then
return 1
fi
for (( i=2; i*i<=num; i++ )); do
if [ $((num % i)) -eq 0 ]; then
return 1
fi
done
return 0
}
# Handling arguments
generate_primes() {
local count=0
local num=2
while [ $count -lt 1000 ]; do
if is_prime $num; then
echo -n "$num "
count=$((count + 1))
fi
num=$((num + 1))
done
echo
}
# Обработка аргументов
while getopts "d:" opt; do
case $opt in
d)
file_name=$OPTARG
;;
*)
echo "Usage: $0 -d <file_name>"
exit 1
;;
esac
done
if [ -z "$file_name" ]; then
echo "No file name specified. Usage: $0 -d <file_name>"
exit 1
fi
# Generate prime numbers and write to file
generate_primes > "$file_name"
echo "Prime numbers successfully written to file $file_name"