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

Volodymyr.savchenko #85

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
179 changes: 179 additions & 0 deletions 03-bash/task1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#!/bin/bash
# Version 1.0.0

# print help information on to a screen
help_usage() {
echo "Usage ./task1.sh [-h|--help] [-n num] [file] "
echo "-h, --help - number of resulting file"
echo "-n - number of files with hardware info"
echo "file - file name with obtained hardware info"
echo "For example, ./task1.sh -n 9 ./test/info/info.txt"
}

# check and create work directory
chk_work_dir() {
if ! [ -d "$1" ]; then
mkdir -p $1
if ! [ -d "$1" ]; then
echo >&2 "Error, working directory $1 was not created. Exit with error."
exit 1
fi
fi
if ! [ -f "$1/.value" ]; then
echo 0 >"$1/.value"
fi
}

# rename file
rename_file() {
# файл-yyyymmdd-nnnn
if [ -f "$1" ]; then
mv "$1" "$1_$(date +%Y%m%d)_$2"
fi
}

chk_is_number() {
local re="^[1-9][0-9]{0,}$"
if [ "$1" =~ $re ]; then
return 0
else
return 1
fi
}

parse_number() {
local m_number=$2
if chk_is_number $1 -eq 0; then
eval $m_number=$1
fi
}

parse_file_name() {
local m_filename=$2
local m_dirname=$3
if ! [ -z "$1" ]; then
eval $m_filename=$(basename $1)
eval $m_dirname=$(dirname $1)
fi
}

inc_value_in_file() {
local cnt=0
if [ -f "$1" ]; then
read cnt <$1
# echo "debug: in file stored: $COUNTER"
echo $((cnt + 1)) >$1
fi
}

get_current_value() {
if [ -f "$1" ]; then
read $2 <$1
inc_value_in_file $1
fi
# echo "debug: get_current_value:: COUNTER: ${!2}"
return 0
}

remove_files() {
# $1 - dir
# $2 - cnt
if [ -d "$1" -a "$2" -gt 0 ]; then
cd $1
local cnt=$(ls $1 | wc -l)
cnt=$( (cnt - $2) )
cnt=${cnt#-}
echo "cnt >> $cnt"
ls $1 | sort -r | tail -$cnt | xargs rm
fi
}

make_info_file() {
local info_file=$1
local m_date=$(date)
echo "Date: $m_date" >$info_file
echo "---- Hardware ----" >>$info_file
local m_proc=$(cat /proc/cpuinfo | grep 'model name' | tail -n 1 | cut -d':' -f2- | xargs)
echo "CPU: \"$m_proc\"" >>$info_file
# local m_proc=$(sudo dmidecode -t processor | grep Version | cut -d':' -f2- | xargs)
local m_mem=$(cat /proc/meminfo | grep 'MemTotal:' | cut -d':' -f2 | xargs | cut -d' ' -f1)
m_mem=$(( $m_mem / 1024 ))
echo "RAM: $m_mem MB" >>$info_file
local m_pr_name=$(sudo dmidecode -t baseboard | grep 'Product Name' | cut -d':' -f2- | xargs)
local m_pr_mb=$(sudo dmidecode -t baseboard | grep 'Manufacturer' | cut -d':' -f2- | xargs)
echo "Motherboard: $m_pr_mb, \"$m_pr_name\"" >>$info_file
local m_serial=$(sudo dmidecode -t2 | grep 'Serial Number' | cut -d':' -f2 | xargs)
echo "System Serial Number: $m_serial" >>$info_file
echo "---- System ----" >>$info_file
local m_release=$(lsb_release -d | cut -d':' -f2- | xargs)
echo "OS Distribution: \"$m_release\"" >>$info_file
local m_kernel=$(uname -v | awk '{print $4}')
echo "Kernel version: $m_kernel" >>$info_file
local m_install_date=$(/bin/ls -lGct /etc | tail -1 | awk '{print $5, $6, $7}')
echo "Installation date: $m_install_date" >>$info_file
local m_host=$(hostname)
echo "Hostname: $m_host" >>$info_file
local m_uptime=$(uptime | awk '{print $3}' | cut -d',' -f1)
echo "Uptime: $m_uptime" >>$info_file
local m_proc_cnt=$(ps aux | wc -l)
echo "Processes running: $m_proc_cnt" >>$info_file
local m_user_cnt=$(who -u | wc -l) # not work in debian 686?
echo "User logged in: $m_user_cnt" >>$info_file
echo "---- Network ----" >>$info_file
local m_ifaces=$(ip addr show | grep "inet\b" | awk '{print $(NF) ": " $2}')
echo "$m_ifaces" >>$info_file
echo '----"EOF"----' >>$info_file
}

# variables
RES_DIR="$HOME/bash"
RES_FILE="$RES_DIR/task1.out"

COUNTER=0
U_NUMBER=0
U_FILENAME=""
U_DIRNAME=""

while [ "$1" != "" ]; do
case $1 in
-h | --help)
help_usage
exit 0
;;
-n)
parse_number $2 U_NUMBER
shift
;;
-* | --*)
echo "Error, a parameter is wrong!"
help_usage
exit 0
;;
*)
parse_file_name $1 U_FILENAME U_DIRNAME
break
;;
esac
shift
done

if ! [ -z "$U_FILENAME" ]; then
RES_FILE=$U_DIRNAME/$U_FILENAME
RES_DIR=$U_DIRNAME
fi

COUNTER_FILE="$RES_DIR/.value"

chk_work_dir $RES_DIR

get_current_value "$RES_DIR/.value" COUNTER

rename_file $RES_FILE $(printf "%04d" $COUNTER)

make_info_file $RES_FILE

remove_files $RES_DIR $U_NUMBER

echo "The file \"$RES_FILE\" was created. Done."

exit 0
35 changes: 35 additions & 0 deletions 03-bash/task2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

help_usage() {
echo "Usage ./task2.sh [-h|--help] [file] "
echo "-h, --help - number of resulting file"
echo "file - file name with obtained hardware info"
echo "For example, ./task2.sh ./test/info/task1.sh"
}

chk_work_dir() {
if ! [ -d "$1" ]; then
mkdir -p $1
if ! [ -d "$1" ]; then
yekovalyov marked this conversation as resolved.
Show resolved Hide resolved
echo >&2 "Error, working directory $1 was not created. Exit with error."
exit 1
fi
fi
}

INSTALL_DIR="/usr/local/bin"
INSTALL_FILE="task1.sh"

if [ -z "$1" ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be better to check the existing file using [ -f ... ]

INSTALL_FILE=$1
fi

chk_work_dir $INSTALL_DIR

cp $INSTALL_FILE $INSTALL_DIR

chmod u+s $INSTALL_DIR/$INSTALL_FILE

chmod 755 $INSTALL_DIR/$INSTALL_FILE

exit 0
88 changes: 88 additions & 0 deletions 03-make/make_soup/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/make -f

# Author: Volodymyr Savchenko [email protected]

KITCHEN = Kitchen
TABLE = $(KITCHEN)/Table
STAVE = $(KITCHEN)/Stave
CUPBOARD = $(KITCHEN)/Cupboard
POTS = $(CUPBOARD)/Pots
REFRIGERATOR = $(CUPBOARD)/Refrigerator
SCRIPTS = Scripts
CHK_DIR_SCRIPT = $(SCRIPTS)/chk_dir.sh
POT_SIZE_DEF = medium
POT_SIZE = $(POT_SIZE_DEF) # set pot size
POTS_PATTERN = small medium big
VEGS_PATTERN = potato carrot onion

#.ONESHELL: pots

all: soup

soup: cooking clean_table # water meat potato pasta carrot onion salt spice
@mv $(STAVE)/$(strip $(POT_SIZE))_pot.txt $(TABLE)/soup.txt
$(info Soup in the $(TABLE)/[email protected].)

clean_table:
@if [ -d $(TABLE) ] ; then rm -rf $(TABLE)/* ; fi

cooking: receipe.txt get_water put_pot_on_stave add_meat add_vegies add_salt_and_spices
$(info Lets cook a soup! Receipe in $(TABLE)/$<.)


receipe.txt: kitchen
@echo "Put the pot on the stave." >> $(TABLE)/$@
@echo "Turn on the stave." >> $(TABLE)/$@
@echo "Cook for 45-60 minutes." >> $(TABLE)/$@
@echo "Put vegies. Cook for 20-25 minutes" >> $(TABLE)/$@
@echo "Add salt and spices." >> $(TABLE)/$@
@echo "Cook for 5 minutes." >> $(TABLE)/$@
@echo "Turn off the stave." >> $(TABLE)/$@

add_salt_and_spices: kitchen pots get_pot
@echo "Salt and spices." >> $(TABLE)/$(strip $(POT_SIZE))_pot.txt

put_pot_on_stave: kitchen pots get_pot
@mv $(TABLE)/$(strip $(POT_SIZE))_pot.txt $(STAVE)

add_vegies: kitchen pots get_meat get_vegies
@echo "Potato, carrot and onion " >> $(STAVE)/$(strip $(POT_SIZE))_pot.txt

add_meat: kitchen pots get_meat
@echo "Meat, 1/2 kg" >> $(STAVE)/$(strip $(POT_SIZE))_pot.txt

get_water: kitchen pots
@echo "Cold water, 3/4 of the pot volume" >> $(TABLE)/$(strip $(POT_SIZE))_pot.txt

get_vegies: kitchen
@$(foreach v, $(VEGS_PATTERN), $(shell touch $(TABLE)/$(v).txt ) )

get_pot: kitchen pots
@mv $(POTS)/$(strip $(POT_SIZE))_pot.txt $(TABLE)

get_meat: meat.txt
@if [ -f $(REFRIGERATOR)/meat.txt ] ; then mv $(REFRIGERATOR)/meat.txt $(TABLE) ; fi

pots: kitchen
@if ! [ -f $(POTS)/$(strip $(POT_SIZE))_pot.txt ] ; then touch $(POTS)/$(strip $(POT_SIZE))_pot.txt ; fi
@$(foreach p, $(POTS_PATTERN), $(shell touch $(POTS)/$(p)_pot.txt) )

meat.txt: kitchen
@touch $(REFRIGERATOR)/$@

kitchen:
@$(CHK_DIR_SCRIPT) $(KITCHEN)
@$(CHK_DIR_SCRIPT) $(CUPBOARD)
@$(CHK_DIR_SCRIPT) $(TABLE)
@$(CHK_DIR_SCRIPT) $(POTS)
@$(CHK_DIR_SCRIPT) $(STAVE)
@$(CHK_DIR_SCRIPT) $(REFRIGERATOR)


.PHONY: clean

clean:
rm -rf $(KITCHEN)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing empty lines in the end of file.


5 changes: 5 additions & 0 deletions 03-make/make_soup/Scripts/chk_dir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
# echo ">>>>>>>>>> dir >>>>>>>>>>> $1"
if ! [ -d $1 ]; then
mkdir -p $1
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line is absent in the end of file.