Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task2: Create some bash-scripts #73

Open
wants to merge 13 commits into
base: Dmytro.Serhieiev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions task1-simple-program/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Created by https://www.toptal.com/developers/gitignore/api/c
# Edit at https://www.toptal.com/developers/gitignore?templates=c

### C ###
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

# End of https://www.toptal.com/developers/gitignore/api/c

.vscode
37 changes: 37 additions & 0 deletions task1-simple-program/guess_a_number.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: GPL-2.0

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MIN_VALUE 0
#define MAX_VALUE 9

int get_rand_number(int min, int max)
{
srand((unsigned int)time(NULL));
return (rand() % (max - min + 1)) + min;
}

int main(void)
{
int user_number, random_number;

random_number = get_rand_number(MIN_VALUE, MAX_VALUE);

printf("Welcome to game \"guess a number\"!\n");
printf("Enter number from %d to %d: ", MIN_VALUE, MAX_VALUE);
scanf("%d", &user_number);
while (user_number < MIN_VALUE || user_number > MAX_VALUE) {
printf("Invalid value. Number must be from %d to %d. Try again: ", MIN_VALUE, MAX_VALUE);
scanf("%d", &user_number);
}

if (user_number == random_number) {
printf("You win\n");
return 0;
}

printf("You loose\n");
return 1;
}
34 changes: 34 additions & 0 deletions task2-bash/arch_project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

# Parameters:
# $1 is the path to the project directory
# $2 is the name of the target archive

DEFAULT_DIRECTORY="../task1-simple-program"
DEFAULT_ARCH_NAME="guesanumber"

if [ $# -lt 1 ]; then
echo "Warning: Arguments are not specified. Using default values."
set $DEFAULT_DIRECTORY $DEFAULT_ARCH_NAME
fi

if [ $# -lt 2 ]; then
echo "Warning: Archive name is not specified. Using default value."
set $1 $DEFAULT_ARCH_NAME
fi

if [ ! -d $1 ]; then
echo "Error: $1 is not a directory."
exit 1
fi

echo "Copying source files from $1 to /tmp/$2..."
[ -d /tmp/$2 ] || mkdir /tmp/$2
cp $1/*.c $1/*.h /tmp/$2/

echo "Making archive /tmp/$2.tgz..."
tar czf /tmp/$2.tgz -C /tmp $2

echo "Copying archive /tmp/$2.tgz to $1/release..."
[ -d $1/release ] || mkdir $1/release
cp /tmp/$2.tgz $1/release/
47 changes: 47 additions & 0 deletions task2-bash/run_game.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# Path to game "guess a number":
GAME="../task1-simple-program/guess_a_number.out"

# Colors:
red="\e[1;31m"
green="\e[1;32m"
yellow="\e[1;33m"
reset="\e[m"

# Variables:
attempt_cnt=0
win_cnt=0

# Command to enable extended pattern matching:
shopt -s extglob

while true; do

if [ $attempt_cnt -eq 0 ]; then
echo -e "${yellow}Enter Y to continue, N to exit, or the number of${reset}"
echo -e "${yellow}attempts you want to play without interruption:${reset}"
read reply
case $reply in
Y|y) ;;
N|n) break ;;
+([0-9])) let attempt_cnt=reply-1 ;;
*) echo -e "${red}Invalid choice!${reset}"
continue ;;
esac
else
let attempt_cnt--
fi

$GAME

if [ $? -eq 0 ]; then
let win_cnt++
echo -e "${green}Good job!${reset}"
else
echo -e "${red}Wish a good luck next time.${reset}"
fi

echo -e "${yellow}You have won $win_cnt time(s).${reset}"

done