##### MatReview exercises ######

https://github.com/green-fox-academy/safely-syllabus/tree/master/materialreview/scripts/bash

1) Processor number and type

#!/bin/bash

function ProcCount()
{
        Pcnt=`cat /proc/cpuinfo | grep -o processor | wc -l`
        echo $Pcnt
}

function CPUtype()
{
        cputp=`cat /proc/cpuinfo | grep "model name" -m 1 | cut -d ':' -f 2 `
        echo $cputp
}

echo $(CPUtype) x $(ProcCount)

2) Date erasure, formatted

#!/bin/bash

#22.11.07

date +%y.%m.%d.

3) Number of discs

#!/bin/bash

lsblk | awk '{print $6}' |grep -o disk | wc -l

4) Factorial recursively

#!/bin/bash
function Fact()
{
        n=$1
    if [[ $n -eq 0 ]]; then
        echo 1
    else
        echo $((n*$(Fact $n-1)))
    en
}
Fact 5

5) File exists e (simple)

#!/bin/bash

FileName=$1

if [[ -r $FileName ]]
then
        echo TRUE
else
        echo FALSE
en

6) File exists e (more)

#!/bin/bash

FileName=$1


#if (($# > 0))
if [[ $FileName == "" ]]
then
        echo no atadott parameter
else
if [[ -e $FileName ]]
then
        if [[ -d $FileName ]]
        then
                echo $FileName is a konyvtar.
        else
                echo $FileName is a fajl.
        en
else
        echo No such file or konyvtar!
fi

fi

7) Compression BC

#!/bin/bash

sam1=$1
sam2=$2

result=$((sam1*sam2))
result2=`echo "$szam1 * $szam2"|bc`
echo $result
echo $result2

8) TotalDiskSpace

#!/usr/bin/bash

TDS=`df | grep -v Filesystem |awk '{sum+=$2} END {print sum}'`
# ======================================================================
TotalFreeSpace=0
FreeSpaceList=`df | awk '{print $2}'`
for i in $FreeSpaceList
  do
    if [[ ! $i == "1K-blocks" ]]
      then
        ((TotalFreeSpace+=$i))
      en
  done
TDS2=$TotalFreeSpace

echo $TDS
echo $TDS2

##### 3.exam preparation bash ######

Features
1) get_kernel_version
Create a function called get_kernel_version that retrieves the Linux kernel version

Hint: a task that can be solved with the right training

example: 5.4.72-generic

function get_kernel_version()
{
	echo `uname -r`
}

2) get_free_space

Create a function called get_free_space that will retrieve the saturation of each file system, and which file system points have mount points in a human readable way:

Filesystem Size Used Avail Use% Mounted on
overlay 251G 11G 228G 5% /
tmpfs 64M 0 64M 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/sdd 251G 11G 228G 5% /etc/hosts
tmpfs 3.1G 0 3.1G 0% /proc/acpi
tmpfs 3.1G 0 3.1G 0% /sys/firmware

Tip: a task that can be solved with the right instructions

function get_free_space()
{
	df -h
}

3) get_total_mem

Create a function called get_total_mem that will get the total amount of memory in the system:

MemTotal: 6401776 kB
Tip: you can filter the output lines using e.g. the grep command

function get_total_mem()
{
	cat /proc/meminfo | grep MemTotal

}

4) compare_line_count
Create a function named compare_line_count that takes the names of two files as arguments and prints the name of the file with more lines

function compare_line_count()
{
	file1=$1
	file2=$2
	result1=`cat $file1 | wc -l`
	result2=`cat $file2 | wc -l`
	if (($result1>$result2))
	then
		echo $file1
	else
		echo $file2
	en
}

##### 3. exam bash part ######

Features
1) get_kernel_version
Create a function called get_kernel_version that retrieves the Linux kernel version

#!/bin/bash

function get_hostname()
{
 hostname
}

2) get_total_mem
Create a function called get_total_mem that will print out the total amount of memory in the system:

function get_total_mem()
{
	cat /proc/meminfo | grep MemTotal

}

3) first_20_characters
Create a function named first_20_characters that takes the path to an existing file as argument and reads and prints the first 20 characters.
The existence of the file does not need to be checked.

function first_20_characters()
{
	file=$1

	result=`head -c 20 $file`
	echo $result
}

###### LINUX bash project tasks #####

2) Create a simple "Hello World!" script!

#!/bin/bash
#his is needed to run:
#chmod a+x 002.sh

echo Hello World!

3) Create a script that prints the date 20 days ago in default format.

#!/bin/bash
#his is needed to run:
#chmod a+x 002.sh

date=`date -d "-20 days"`

echo 1TP3Date

4) Write a script that tells you if the file specified in the argument exists and is writable.
If so, please write "This species is registered and may be registered"." If no, the script does not return a value

#!/bin/bash
#his is needed to run:
#chmod a+x 003.sh

file=$1

if [[ -w $file ]]
then
 echo This file is downloaded and can be written
en

5) Write a script to determine whether the two strings in the argument are the same. If
yes, write out : "They are now identical". Otherwise the script should not return a value.

#!/bin/bash
#his is needed to run:
#chmod a+x 005.sh

str1=$1
str2=$2

if [[ $str1 == $str2 ]]
then
 echo They are now identical
fi

6) Write a script that tells you which of the two numbers in the argument is
larger (first or second) and write it out! In the following format:
"Now this number is greater than 12".If 2 numbers are equal the script does not return a value.

#!/bin/bash
#his is needed to run:
#chmod a+x 006.sh

number1=$1
number2=$2

if [[ $number1 > $number2 ]]
then
 echo Now this number is greater: $number1
else
 echo Now this number is greater than: $number2
en

7) Write a script that subtracts the first number from the second number in the argument. And print The
result. e.g. "88"

#!/bin/bash
#his is needed to run:
#chmod a+x 007.sh

number1=$1
number2=$2

eredmeny=`echo $number2-$number1 | bc`
echo $eredmeny

8) Write a script that prints which of the files specified in the argument contains more than one
character! In the format "Now there are more characters in /tmp/myfile.txt". If they are the same in
script should not return a value.

#!/bin/bash
#his is needed to run:
#chmod a+x 004.sh

file1=$1
file2=$2

filechars1=`cat $file1|wc -m`
filechars2=`cat $file2|wc -m`

if (($filechars1 > $filechars2))
then
 echo Now $file1 has more characters
else
 echo Now $file2 has more characters
en

9) Write a script that adds the numbers in the argument. And print the result. E.g.
"88"

#!/bin/bash
#his is needed to run:
#chmod a+x 007.sh
for number in $@;do echo $number;done | awk '{sum+=$1} END {print sum}'

10) Write a script with two parameters, one is a file name and the other is a character pattern.
Calculate how many of these character patterns the file contains and write it out in the following format:
"The resulting file contains the character pattern on 11 lines", If the character pattern is not in the
file, write it out with the character pattern in the following format: 'The mirror drilling machine never once
included"

#!/bin/bash
#his is needed to run:
#chmod a+x 007.sh

file=$1
string=$2

result=`cat $1 | grep -o $2 | wc -l`
#result=`cat $1 | grep -o $2 -c`

if (( $result==0 ))
then
 echo $2 is not listed once
else
 echo The result is a species with the character string $result in row $result
en

11) Write a script that greets according to the first parameter and if the second parameter is
a file that does not yet exist, it will include the greeting!

#!/bin/bash
#his is needed to run:
#chmod a+x 011.sh
greeting=$1
file=$2

echo $greeting

if [ ! -f "$file" ]
then
        echo $greeting > $file
en

12) Create a script that includes a bash function that prints the Linux kernel
and name it 'get_kernel_OS'!

#!/bin/bash
#his is needed to run:
#chmod a+x 012.sh

function get_kernel_OS()
{
 uname -r
}
get_kernel_OS

13) Create a script that includes a bash function that prints the Linux
The time when the kernel version was created (compiled) and its name should be 'get_kernel'!

#!/bin/bash
#his is needed to run:
#chmod a+x 012.sh
function get_kernel()
{
 uname -v | cut -d ' ' -f 4,5,6,7,8,9
 # uname -v | awk '{print $4, $5, $6, $7, $8, $9}'
}
get_kernel

14) Create a script that includes a bash function that prints the Linux
processes that run on behalf of the user specified as a parameter. A
function name should be 'get_userprocesses '! If the user does not exist, return the following
with the value: 'Unrequested use'. If the user exists but has no process, use this to return
back: 'Process not found'.

#!/bin/bash
#his is needed to run:
#chmod a+x 014.sh

function get_userprocesses()
{
username=$1
echo 1TP3username
checkinguser=`getent passwd $username | wc -l`
echo $checkinguser
if (( $checkinguser>0 ))
then
processcount=`ps auxf|awk '{print $1}'| grep $username|wc -l`
echo $processcount
 if (( ! $processcount==0 ))
 then
  ps auxf | awk -v user=$username '{if($1==user){ print $0}}'
 else
  echo Process not found
 en
else
 echo Unsuccessful resolution
en
}
get_userprocesses $1

15) Create a script that includes a bash function that is user friendly
way to print some Linux hardware data (memory size, processor data) The function name is
should be 'get_hwinfo'! Display format of your choice.

#!/bin/bash
#his is needed to run:
#chmod a+x 014.sh

function get_hwinfo()
{
cat /proc/meminfo | grep Mem
cat /proc/cpuinfo | grep bogo | uniq && cat /proc/cpuinfo | grep "model name" | uniq && cat /proc/cpuinfo | grep "cpu Mhz" | uniq && cat /proc/cpuinfo | grep "cpu cores" | uniq &&
cat /proc/cpuinfo | grep "cache size" | uniq

}
get_hwinfo

16) Create a script that includes the four functions you wrote earlier in a file and download it
will run all four if the script is run!

#!/bin/bash
#his is needed to run:
#chmod a+x 014.sh



./012.sh
./013.sh
./014.sh User3
./015.sh

###### Linux bash (hereafter bash) knowledge level assessment #######

Attention:

  • From LNX-01.sh up to and including LNX-07.sh, the access is primarily done via autograder(Gradescope)
  • For tasks 8),9),10) the evaluation is not automated. These tasks are controlled and run with root privileges.
  • Your ZIP upload contains only files in subdirectories, not subdirectories
  • Make sure that your uploaded scripts do not contain syntactic errors and do not stop because of an authorization problem
  • Note that the scripts must run without interruption, so they should not require user interaction (e.g. Is this ok [y/N]: don't include it, use the -y option)
  • Use UTF-8 (without BOM) character encoding and Unix line terminators
  • (only for LNX-01.sh - LNX-07.sh ) Since the evaluator is running in a Docker container, systemd is not available, and bc is not available in the container
  • (only for LNX-01.sh - LNX-07.sh tasks) The files will be run by a non-root user, do not use sudo
  • The result (output) of the exercise must be identical to the example output format, with no unnecessary/missing spaces, characters or punctuation.

Tasks:

  1. Create a bash script that prints today's date in year 4 characters, month 2 characters and day in two-character format! The separator character should be the dot '.' character (YYYY.MM.DD)
Evidencia: Copy or save the bash script.
File name: LNX-01.sh
#!/bin/bash

# chmod a+x Date-format.sh

date +%Y.%m.%d
  1. Write a script that prints in the following format whether the file system object specified in the argument is a directory and if so, whether it exists. If the file system object given as an argument does not exist, or is not a directory, the script does not return a result.

Example output:

The specified conyvtar is checked.
Evidencia: Copy or save the bash script.
File name: LNX-02.sh
#!/bin/bash

# chmod a+x Directory.sh

dir=$1

if [ -d $dir ]
then
 echo The specified directory is found.
en
  1. Write a script that prints, in the following format, whether the file specified in the argument exists and, if so, how many lines it consists of. If the file does not exist, the script does not return a result.

Example output:

Match the specified species.Number of lines:24
Evidencia: Copy or save the bash script.
File name: LNX-03.sh
#!/bin/bash

# chmod a+x LineCount.sh

file=$1

if [[ -e $file ]]
then
 echo The specified species is matched.Line set:`cat $file | wc -l`
en
  1. Write a script that prints in the following format whether the two strings in the argument are the same. If not, the script does not return a result.

Example output:

The strings are identical
Evidencia: Copy or save the bash script.
File name: LNX-04.sh
#!/bin/bash

# chmod a+x StringSame.sh

str1=$1
str2=$2

if [[ $str1 == $str2 ]]
then
 echo The character strings are identical
en
  1. Write a script that prints the higher of the two numbers in the argument (first or second) in the following format and print it. If neither is greater than the other, the script will not return a result.

Example outputs:

The first number is bigger:24
The second seed is bigger:12
Evidencia: Copy or save the bash script.
File name: LNX-05.sh
#!/bin/bash

# chmod a+x CompareNumber.sh

number1=$1
number2=$2

if (( $number1 > $number2 ))
then
 echo The first number is greater than:$number1
elif (( $number1 < $number2 ))
then
 echo The second number is greater than:$number2
en
  1. Write a script that prints out the files given in the argument in the following format, which one has more characters! If the two files contain the same number of characters, the script will return no result.

Example outputs:

The first one has more characters.
The second species has more characters.
Evidencia: Copy or save the bash script.
File name: LNX-06.sh
#!/bin/bash

# chmod a+x CompareNumber.sh

file1=$1
file2=$2
number1=`cat $file1 | wc -m`
number2=`cat $file2 | wc -m`

if (( $number1 > $number2 ))
then
 echo The first one has more characters.
elif (( $number1 < $number2 ))
then
 echo The second type has more characters.
en
  1. Write a script that adds the two numbers in the argument and prints the result.

Example output:

41
Evidencia: Copy or save the bash script.
File name: LNX-07.sh
#!/bin/bash
 
sam1=$1
sam2=$2
 
result=$((sam1+sam2))

echo $result
  1. Create a bash script that contains the following four functions in a file and executes them when the script is run!
    1. Create a function that prints the Linux kernel version and name it 'get_kernel‘!
    2. Create a function that prints the Linux distribution and version and name it 'get_distver‘!
    3. Create a function that prints the Linux processes that are currently running on the system by the user specified as the function parameter. The function name should be 'get_uproc'. The parameter when the function is called should be the username you are given for access e.g.: User0!
    4. Create a function that prints the following Linux hardware information (total memory size, total processor bogomips) in a user-friendly way, named 'get_memcpu‘!
Evidencia: Copy or save the bash script.
File name: LNX-08.sh

 

#!/bin/bash

function get_kernel_v2()
{
 uname -r
}


function get_distver_v2()
{
cat /etc/*-release | head -3
}

function get_uproc_v2()
{
username=$1
checkinguser=`getent passwd $username | wc -l`
echo $checkinguser
if (( $checkinguser>0 ))
then
processcount=`ps auxf|awk '{print $1}'| grep $username|wc -l`
echo $processcount
 if (( ! $processcount==0 ))
 then
  ps auxf | awk -v user=$username '{if($1==user){print $0}}'
 
 fi
fi
}

function get_memcpu_v2()
{
echo MEM: `cat /proc/meminfo | head -1 | awk '{print $2,$3}'`
echo BogoMIPS: `cat /proc/cpuinfo | grep bogomips | uniq |awk '{print $3}'`
 
}

get_kernel_v2
get_distver_v2
get_uproc_v2 User3
get_memcpu_v2


##############################################################################################

#!/bin/bash

# Function to get the Linux kernel version
get_kernel() {
    uname -r
}

# Function to get the Linux distribution and version
get_distver() {
    cat /etc/*release
}

# Function to get the processes running by a specific user
get_uproc() {
    if [ $# -ne 1 ]; then
        echo "Error: Invalid number of arguments"
        echo "Usage: $0 "
        exit 1
    en

    username=$1
    ps -u $username
}

# Function to get the memory and CPU information of the system
get_memcpu() {
    echo "Total memory: $(free -m | awk 'NR==2{printf "%.2f GB\n", $2/1024}')"
    echo "CPU bogomips: $(cat /proc/cpuinfo | grep bogomips | awk '{sum+=$3} END {print sum}')"
}

# Call the functions
echo "Kernel version: $(get_kernel)"
echo "Distribution and version: $(get_distver)"
echo "Processes running by user User0: $(get_uproc User0)"
echo "Memory and CPU information: $(get_memcpu)"

  1. Disallow the root user to log in via SSH.
Evidencia: /etc/ssh/sshd_config
File name: LNX-09.txt
#!/usr/bin/bash
#futtatas before:
#chmod a+x ~/Megoldasaim/Linux-Admin/001.sh
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config-_`date +"%Y%m%d_%H%M%S"`
 
this="#PermitRootLogin prohibit-password
this="PermitRootLogin no"
what="/etc/ssh/sshd_config"
 
sudo sed -i "s/$ez/$erre/g" $miben

if (($# > 0 ))
then
sudo cat /etc/ssh/sshd_config | sudo tee $1 ls > /dev/null
en
  1. SElinux function is switched off.
Evidencia: /etc/selinux/config
File name: LNX-10.txt
#!/usr/bin/bash
#futtatas before:
#chmod a+x ~/Megoldasaim/Linux-Admin/002.sh
sudo cp /etc/selinux/config /etc/selinux/config-_`date +"%Y%m%d_%H%M%S"`
 
this="SELINUX=enforcing"
this="SELINUX=disabled"
what="/etc/selinux/config"
 
sudo sed -i "s/$ez/$erre/g" $miben
 
#getenforce
#reboot
if (( $# > 0 ))
then
sudo cat /etc/selinux/config | sudo tee $1 ls > /dev/null
en
  1. The server comes with a disk that is not yet in use (20GB). Create a file system on it. The file system should be: EXT4. Add it to the machine /WEBDATA mount point, and the volume is labelled: WEBDATA
Evidencia: df -h results in LNX-11.txt file and
/etc/fstab result added to LNX-11.txt file
File name: LNX-11.txt
#!/bin/bash
#elotte:
#chmod a+x ~/Megoldasaim/Linux-Admin/003.sh
 
sudo ls /sys/class/scsi_host | sudo awk '{print "echo \"- - -\" > /sys/class/scsi_host/" $1 "/scan" }' | sudo /usr/bin/bash
 
lsblk
 
#(echo n; echo p;echo ""; echo ""; echo ""; echo w )| sudo fdisk /dev/sdb
< 0 ))
then
sudo df -h | sudo tee $1 ls
sudo cat /etc/fstab | sudo tee -a $1 ls > /dev/null

en