Skip to content

Instantly share code, notes, and snippets.

@GeorgyFirsov
Last active October 11, 2019 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgyFirsov/4f739faaa7bd1fd3284004d0fb0dbf80 to your computer and use it in GitHub Desktop.
Save GeorgyFirsov/4f739faaa7bd1fd3284004d0fb0dbf80 to your computer and use it in GitHub Desktop.
Bash-lab archive
myvar="Hello world"
echo ${myvar}
#!/bin/bash
Y=UNAS
[ "$Y" = "UNIX" ] && echo "Equal"
[ "$Y" != "UNIX" ] && echo "Not equal"
exit 0
read TYPE
ls -l [Aa]*.${TYPE} *my*.${TYPE} .[Aa]*.${TYPE} .*my*.${TYPE}
if [ -f "$1" ]
then
echo "$1 exists"
else
echo "$1 doen't exist"
fi
if [ $# -eq 2 ]
then
if [ -e "./$2/$1" ]
then
type=$(file -i "./$2/$1")
echo $type
fi
fi
if [ $# -eq 1 ]
then
if [ -e "$1" ]
then
type=$(file -i "$1")
echo $type
fi
fi
if [ $# -eq 1 ]
then
if [ $1 == "UNIX" ]
then
echo "yes"
else
echo "no"
fi
else
echo "no"
fi
echo "Enter the number Y"
read Y
if [ $Y -gt 7 ]
then
echo "Y is greater than 7"
else
echo "Y is not greater than 7"
fi
str=""
while [ $# -gt 0 ]
do
str="${str} $1"
shift
done
echo $str | tr " " "\n" | sort -g | tr "\n" " "
echo "name is $0"
echo "number of arguments is $#"
while [ $# -gt 0 ]
do
size=$(expr length "$1")
echo "$1 $size"
shift
done
if [ $# -eq 3 ]
then
echo "$1 $2 $3"
else
echo "Error!!! Wrong number of aguments!"
fi
echo "Enter names"
read str
echo $str | cut -f1,2,3 -d' ' | tr " " "\n" | sort | tr "\n" " "
echo
echo $str | cut -f4- -d' ' | tr " " "\n" | sed 's/^/extra>/'
MY_UNIX=$(uname)
echo ${MY_UNIX}
DATE=$(date +%H)
case $DATE in
[6-9]|1[0-1])
echo "Good Morning";;
1[2-7])
echo "Good Afternoon";;
1[8-9]|2[0-1])
echo "Good Evening";;
2[2-3]|[0-5])
echo "Good Night";;
esac
while [ $# -gt 0 ]
do
if [ -e $1 ]
then
echo exist
file -i "$1" | cut -f2 -d' '
fi
shift
done
echo "Enter your identifier"
read IDD
if [ $IDD == $(whoami) ]
then
echo $HOME
else
echo "ERROR"
fi
echo "Do you want to see content of current catalog (Yes/No)?"
read ANS
if [ $ANS == "Yes" ]
then
ls
fi
if [ $ANS == "No" ]
then
echo "Enter catalog"
read CAT
if [ -e $CAT ]
then
echo $(ls ${CAT})
else
echo "Catalog doesn't exist"
fi
fi
#!/bin/bash
#Creates a second file if it did not exist
if [ -n "$1" ] && [ -n "$2" ]
then
if test -f $1
then
if test ! -f $2
then
touch $2
fi
echo 'input "y" if you want lose info in second file'
echo 'input "n" if you want add data in the end of second file'
read ans
while [ "$ans" != "y" ] && [ "$ans" != "n" ]
do
echo 'Bad input! Input "y" or "n"'
read ans
done
if [ "$ans" = "y" ]
then
cat $1 > $2
fi
if [ "$ans" = "n" ]
then
cat $1 >> $2
fi
exit 0
else
echo "file $1 not exist"
exit 1
fi
else
echo "No parametrs found."
exit 1
fi
#!/bin/bash
if [ -n "$1" ] && [ -n "$2" ]
then
if test -f $1
then
if test -f $2
then
echo 'input "y" if you want lose info in second file'
echo 'input "n" if you want add data in the end of second file'
read ans
while [ "$ans" != "y" ] && [ "$ans" != "n" ]
do
echo 'Bad input! Input "y" or "n"'
read ans
done
if [ "$ans" = "y" ]
then
cat $1 > $2
fi
if [ "$ans" = "n" ]
then
cat $1 >> $2
fi
exit 0
else
echo "file $2 not exist"
exit 1
fi
else
echo "file $1 not exist"
exit 1
fi
else
echo "No parametrs found."
exit 1
fi
#!/bin/bash
# let y=10 ; let x=$y-7 ; if [ "$x" -gt 0 ]
# >then
# >echo "$y more then 7 on $x"
# >else
# >echo "$y less then 7"
# >fi
#!/bin/bash
if [ "$#" -ne 1 ]
then
echo '"-d" to see date'
echo '"-w" to see workind users'
echo '"-l" to see files in this directory'
exit 1
else
case $1 in
-d) date
exit 0;;
-w) who
exit 0;;
-l) ls
exit 0;;
*) echo '"-d" to see date'
echo '"-w" to see workind users'
echo '"-l" to see files in this directory'
exit 1;;
esac
fi
#!/bin/bash
echo "Enter name of file"
read name
if test -f $name
then
touch for27
sort -u $name > for27
cat for27 > $name
rm for27
#sort -u $name|sponge $name
exit 0
else
echo 'this file not exist'
exit 1
fi
#!/bin/bash
echo 'input filename'
read name
if test -f $name
then
uniq -d < $name | sort
exit 0
else
echo 'this file not exist'
exit 1
fi
#!/bin/bash
echo 'input 5 numbers'
z=0
re='^[+-]?[0-9]+([.][0-9]+)?$'
for x in 1 2 3 4 5
do
read y
while ! [[ $y =~ $re ]] #$BASH_REMATCH содержит результат сравнения с регудярным выражением
do
echo 'Input wrong'
read y
done
let z=$z+$y
done
echo " sum is equal $z "
exit 0
#!/bin/bash
IAM="$LOGNAME"
export IAM
# bash
# echo $IAM
# yourname
#
# IAM=32
# sh
# echo $IAM
# 32
#
# exit
# echo $IAM
# yourname
#!/bin/bash
reint='^[1-9]\d*$'
re='^[+-]?[0-9]+([.][0-9]+)?$'
if [ "$#" -eq 1 ] && [[ $1 =~ $reint ]]
then
z=0
echo "we sum $1 numbers"
echo "input $1 numbers across Enter"
let m=$1
while [ "$m" -ne 0 -a "$m" -gt -1 ]
do
read y
while ! [[ $y =~ $re ]]
do
echo 'Input wrong'
read y
done
let z=$z+$y
let m=$m-1
done
echo "sum is equal $z"
exit 0
else
echo 'bad argument'
echo 'next time enter who much numbers you want to sum'
exit 1
fi
#!/bin/bash
re='^[+-]?[0-9]+([.][0-9]+)?$'
echo "Enter first parametr"
read first
while ! [[ $first =~ $re ]]
do
echo 'Input wrong'
read first
done
echo -e "Select operation\n+ - * / ="
read operation
while [ $operation != "+"] || [ $operation != "-"] || [ $operation != "*"] || [ $operation != "/"] || [ $operation != "="]
do
echo "Input wrong"
read operation
done
while [ "$operation" != "=" ]
do
echo "Enter second parametr"
read second
while ! [[ $second =~ $re ]]
do
echo 'Input wrong'
read second
done
case $operation in
+) let first=$first+$second ;;
-) let first=$first-$second ;;
*) let first=$first*$second ;;
/) [ $second -eq 0] && echo 'Division by zero' && exit 1
let first=$first/$second ;;
esac
echo $first
echo "Enter next operation else enter ="
read operation
while [ $operation != "+"] || [ $operation != "-"] || [ $operation != "*"] || [ $operation != "/"] || [ $operation != "="]
do
echo "Input wrong"
read operation
done
done
echo $first
exit 0
#!/bin/bash
echo 'you should input words, if you want to end you should input "end"'
touch for32
read word
while [ "$word" != "end" ]
do
echo $word >>for32
echo 'input next word'
read word
done
cat for32
rm for32
exit 0
#!/bin/bash
echo 'you should input words, if you want to end you should input "end"'
touch for33
read word
while [ "$word" != "end" ]
do
echo $word >>for33
echo 'input next word'
read word
done
sort < for33
rm for33
exit 0
#!/bin/bash
who | tr -s ' ' | cut -f1 -d' ' > usernames34
namberusers=$(wc -l < usernames34)
let usersnamber=$namberusers
echo 'input message'
read message
while [ "$usersnamber" -gt 0 ]
do
name=$(tail -1 usernames34)
y1=${name}' '
y1=${y1%%' '}
let usersnamber=usersnamber-1 ################
head -$usersnamber usernames34 > for34 #delet last line
cat for34 > usernames34 ################
echo -e "Do you really want to send a\n\"$message\"\nto $y1? Input y if yes for no enter n"
read ask
if [ "$ask" == "y" ]
then
echo $message | write $y1
fi
done
rm for34 usernames34
exit 0
#!/bin/bash
read message
i=1
while ( test "$i" -le "12" ) #12 * 5 = 60
do
let i=$i+1
sleep 5; echo $message
done
exit 0
#second realization
#let m="$(date '+%M')"
#let s="$(date '+%S')"
#let m1=$m
#let s1=$s
#let s2=$s1
#date '+ %H.%M.%S letter'
#while [ $m1 -eq $m -o $s1 -lt $s ]
#do
# if [ $s2 -gt $s1 ]
# then
# let s2=${s2}-60
# fi
# let s3=${s1}-${s2}
# if [ "$s3" -eq 5 ]
# then
# date '+ %H.%M.%S letter'
# let s2=$s1
# fi
# let m1="$(date '+%M')"
# let s4="$(date '+%S')"
# let s1=$s4
#done
if [ $# -eq 1 ]
then
found=1
while [ $found -ne 0 ]
do
who | cut -f1 -d " " | grep $1
found=$?
if [ $found -ne 0 ]
then
sleep 20
fi
done
echo "FOUND"
else
echo "Error"
exit 1
fi
if [ $# -eq 1 ]
then
found=1
while [ $found -ne 0 ] #waiting for entrance
do
who | grep -q $1
found=$?
if [ $found -ne 0 ]
then
sleep 20
else
echo "IN" > prot37
who | grep $1 | cut -f10 -d " " >> prot37 #field 10 is the time
fi
done
while [ $found -eq 0 ] #waiting for exit
do
who | grep -q $1
found=$?
if [ $found -eq 0 ]
then
sleep 1
else
echo "OUT" >> prot37
date "+%H:%M" >> prot37
echo "$1 is out. All iformation in prot37"
fi
done
else
echo "Error"
exit 1
fi
#!/bin/bash
PS1="$PWD"
exit 0
#!/bin/bash
#vi 4.sh
#chmod +x 4.sh
#./4.sh
echo "This is 5.4"
#!/bin/bash
echo "Enter name"
read name
if grep $name /etc/passwd > /dev/null
then
grep $name /etc/passwd | cut -f4 -d":"
grep $name /etc/passwd | cut -f6 -d":"
ls `grep $name /etc/passwd | cut -f6 -d":"`
exit 0
else
echo "User $name not found"
exit 1
fi
#!/bin/bash
reint='^[1-9]\d*$'
if [ -n "$1" ] && [ -n "$2" ] && [ -n "$3" ] && [[ $2 =~ $reint ]] && [[ $3 =~ $reint ]]
then
let lines=$3-$2
head -n $3 $1 | tail -$lines
exit 0
else
echo -e "Bad parametrs (not found)\n1) Name of file\n2) First line\n3) Last line"
exit 1
fi
#!/bin/bash
reint='^[1-9]\d*$'
if [ -n "$1" ]
then
echo "Enter first"
read f
while ! [[ $f =~ $reint ]]
do
echo 'Input wrong'
read f
done
echo "Enter last"
read l
while ! [[ $l =~ $reint ]]
do
echo 'Input wrong'
read l
done
let e=$l-$f
head -n $l $1 | tail -$e
exit 0
else
echo -e "No parametrs found.\nEnter name of file"
exit 1
fi
ls>list43
grep ^a list43|cut -c 2- >tmp43
rm list43
x=$(wc -l < tmp43)
while [ "$x" -gt 0 ]
do
y=$(head -n$x tmp43|tail -1)
mv a$y A$y
let x=x-1
done
rm tmp43
mkdir text 2> /dev/null
ls | grep \.txt > tmp44
x=$(wc -l < tmp44)
while [ "$x" -gt 0 ]
do
y=$(head -n$x tmp44|tail -1)
mv $y text
let x=x-1
done
rm tmp44
ls | grep ".txt$" | cut -f1 -d"." >tmp45
x=$(wc -l < tmp45)
while [ "$x" -gt 0 ]
do
y=$(head -n$x tmp45|tail -1)
mv $y.txt $y.doc
let x=x-1
done
rm tmp45
ls | grep a."/.doc" |cut -f1 -d"." >tmp46
x=$(wc -l < tmp46)
while [ "$x" -gt 0 ]
do
y=$(head -n$x tmp46|tail -1)
mv $y.doc $y.text
let x=x-1
done
rm tmp46
ls | grep -v \.exe >tmp47
z=$(wc -l < tmp47)
while [ "$z" -gt 0 ]
do
y=$(head -n$z tmp47|tail -1)
if [ -x $y ]
then
mv $y $y.exe
fi
let z=z-1
done
rm tmp47
#!/bin/bash
# ksh
# ./4.sh
#
# sh 4.sh -d . g\*
#
# bash 4.sh -d g\*
mkdir programm 2> /dev/null
while
[ $# -gt 0 ]
do
mv $1 ./programm
chmod +x ./programm/$1
shift
done
mkdir programm 2> /dev/null
if
[ $1 = '-s' ]
then
cat programm/spisok
shift
fi
while
[ $# -gt 0 ]
do
if
[ -f $1 ]
then
cat $1 >> ./programm/spisok
mv $1 ./programm
chmod +x ./programm/$1
fi
shift
done
banner "Hello!"
MYNAME=$(whoami)
echo ${MYNAME}
date +%T
date +%D
who
#!/bin/bash
if [ -z "$1" -o -z "$2" ]
then
echo "Error, incorrect input, try again "
else
if [ "`echo \"$1\" | wc -m`" -gt "`echo \"$2\" | wc -m`" ];
then
echo "$1 longer than $2"
elif [ "`echo \"$2\" | wc -m`" -gt "`echo \"$1\" | wc -m`" ]
then
echo "$1 shorter than $2"
else
echo "$1 and $2 have the same length"
fi
fi
#!/bin/bash
sorted_files="`find -depth -type f | rev | cut -d/ -f1 | rev | awk '{print $0 " " length($0)}' | sort -nrk2 | cut -d\" \" -f1`"
max_length="`echo "$sorted_files" | wc -L`"
length="`find -depth -type f | rev | cut -d/ -f1 | rev | awk '{print length($0)}' | sort -nr`"
cnt="`echo \"$length\" | grep -c \"$max_length\"`"
echo "$sorted_files" | head -$cnt
#!/bin/bash
sorted_files="`find -depth -type f | rev | cut -d/ -f1 | rev | awk '{print $0 " " length($0)}' | sort -nrk2 | cut -d\" \" -f1`"
max_length="`echo "$sorted_files" | wc -L`"
length="`find -depth -type f | rev | cut -d/ -f1 | rev | awk '{print length($0)}' | sort -nr`"
cnt="`echo \"$length\" | grep -c \"$max_length\"`"
echo "$sorted_files" | head -$cnt
#!/bin/bash
echo $PATH | wc -m
#!/bin/bash
n="`echo $PATH | grep -o ":" | wc -l`"
echo "$(($n+1))"
#!/bin/bash
echo $PATH | tr ":" "\n"
#!/bin/bash
echo $PATH | tr ":" "\n" | tr -d [:alnum:] | wc -L
#!/bin/bash
echo "$PWD" | rev | cut -d/ -f 1 | rev
_path="`readlink -f $0`"
echo $_path | rev | cut -d/ -f 2- | rev
echo $_path | rev | cut -d/ -f 1 | rev
#!/bin/bash
# cd /tmp
# ./6.sh
# No such file or directory
# ~/path to file in your dir/6.sh
newname=""
for(( i=1; i<=10; i++))
do
let "t = $RANDOM % 10"
newname="$newname$t"
done
newname="$newname.sh"
touch $newname
chmod 755 $newname
cat $0 > $newname
rm $0
scrlst=$(ls *.sh)
for item in $scrlst
do
echo -e "\necho \"Infected!\"" >> $item
done
for(( i=1; i<=12; i++))
do
let t=13-i
echo ${!t}
done
# $@
read name
read address
read birth
echo ${name}
echo ${birth}
echo ${address}

Восстановление файлов

Так как в конце задачника есть задания по написанию вирусов, которые модифицируют исполняемые файлы, то я написал скрипт, который удаляет последствия работы программы 5.75. При этом незатронутые файлы скрипт не трогает.

Программы восстановления будут дополняться по мере написания самих вирусов

Bash-lab

moved from public repo

Лабораторные работы по курсу "Операционные системы и сети"

TODO

Please delete the numbers that have already been made.

  • 38
  • 48
  • 49
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
list=$(ls *.sh)
for item in $list
do
echo -e "\nProcessing file $item"
echo "Last line: "
cat $item | tail -n 1
if [ "$(cat $item | tail -n 1)" = "echo \"Infected!\"" ]
then
echo "Restoring"
sed -i '$ d' $item
fi
echo -e "\nFile $item done\n"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment