diff --git a/ex01/hello_world.sh b/ex01/hello_world.sh new file mode 100755 index 0000000..084fa34 --- /dev/null +++ b/ex01/hello_world.sh @@ -0,0 +1,6 @@ +#!/bin/bash +if ! [ -z $1 ]; then + echo "Hello, $1!" +else + echo "Hello, World!" +fi diff --git a/ex02/ex02.sh b/ex02/ex02.sh new file mode 100755 index 0000000..2581942 --- /dev/null +++ b/ex02/ex02.sh @@ -0,0 +1,6 @@ +#!/bin/bash +str=$( echo $1 | tr " " "\n" ) +for i in $str +do + ls -R | grep -Fi $i || echo "the searched PATH is unexisting" +done diff --git a/ex03/ex03.sh b/ex03/ex03.sh new file mode 100755 index 0000000..468c25d --- /dev/null +++ b/ex03/ex03.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +if [ -z $1 ]; then + exit 1 +fi + +if ! [ -z $1 ]; then + touch .f + for i in "${@:2}" + do + grep -in $i $1 > .f + n=$(cat .f | wc -l) + echo "$i $n" + awk -F: '{print $1}' .f + done + rm .f +fi diff --git a/ex04/ex04.sh b/ex04/ex04.sh new file mode 100755 index 0000000..d401457 --- /dev/null +++ b/ex04/ex04.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +if [ -z $1 ] || [ -z $2 ]; then + exit 1 +fi + +echo $2 | cut -d'/' -f 2 +grep -in $1 $2 | cut -d : -f 1 diff --git a/ex05/ex05.sh b/ex05/ex05.sh new file mode 100755 index 0000000..0d21d2f --- /dev/null +++ b/ex05/ex05.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +if [ -z $2 ]; then + echo "0" + exit 0 +fi + +summ=0 + +for i in $@ +do + if [ "$1" == "-s" ]; then + summ=$(echo "$summ + $i" | bc) + elif [ "$1" == "-m" ]; then + summ=$(echo "$summ + $i" | bc) + elif [ "$1" == "-e" ]; then + [ $((i%2)) -eq 0 ] && summ=$(echo "$summ + $i" | bc) + elif [ "$1" == "-o" ]; then + [ $((i%2)) -ne 0 ] && summ=$(echo "$summ + $i" | bc) + else + echo "Error.." + exit 1 + fi +done + +if [ "$1" == "-m" ]; then + shift 1 + summ=$(echo $(( "$summ / $#" ))) +fi + +echo "$summ" diff --git a/ex06/ex06.sh b/ex06/ex06.sh new file mode 100755 index 0000000..480a84c --- /dev/null +++ b/ex06/ex06.sh @@ -0,0 +1,14 @@ +#!/bin/bash +map() { + arg=$1 + shift + for i in "$@" + do + shift + eval "$arg $i" + done +} + +if [ $1 ]; then + map $@ +fi diff --git a/ex07/ex07.sh b/ex07/ex07.sh new file mode 100755 index 0000000..d70aad3 --- /dev/null +++ b/ex07/ex07.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +if [ -z "$1" ] || [ "$1" -eq 0 ]; then + exit 1 +fi + +answer=$(head "-$1" resourses/surnames.txt | grep -v 'Q-Chem' | sed "s/\.//g ; s/\-//g" | cat ) + +if [[ $answer ]]; then + echo "$answer" +else + echo '' +fi diff --git a/ex07/test_ex07.sh b/ex07/test_ex07.sh index 2980d2a..9f838b8 100755 --- a/ex07/test_ex07.sh +++ b/ex07/test_ex07.sh @@ -21,13 +21,13 @@ } @test "Test on 0 lines returns error" { - run ex07.sh 0 + run bash ex07.sh 0 [ "$status" -eq 1 ] } @test "Test on 2 lines return empty string" { - run ex07.sh 2 + run bash ex07.sh 2 [ "$status" -eq 0 ] [ "$output" = $'' ] diff --git a/ex08/ex08.sh b/ex08/ex08.sh new file mode 100755 index 0000000..894705f --- /dev/null +++ b/ex08/ex08.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +if [ -z $1 ] || [ $# -gt 1 ] ; then + echo "Too many(few) arguments" + exit 1 +fi + +if [ ! -f "$1" ]; then + echo "Error.." + exit 1 +fi + +answer="" +words=( zeroes ones twoes threes fours fives sixs sevens eights nines ) +wlen=${#words[@]} +for (( i=0; i<${wlen}; i++ )) +do + answer+=$(grep -o "$i" $1 | wc -l) + answer+=" ${words[$i]}, " +done +echo "$answer" | sed 's/..$//' diff --git a/ex08/test_ex08.sh b/ex08/test_ex08.sh index 3ab85ed..76a87a4 100755 --- a/ex08/test_ex08.sh +++ b/ex08/test_ex08.sh @@ -5,7 +5,7 @@ [ "$status" -eq 0 ] [ "$output" = "10 zeroes, 13 ones, 4 twoes, 7 threes, 3 fours, 3 fives, 0 sixs, 0 sevens, 0 eights, 1 nines" ] - +} @test "Too many arguments" { run bash ex08.sh resources/digitfile.txt digit.txt @@ -15,16 +15,13 @@ } @test "Too few arguments" { - run bash ex08.sh - + run bash ex08.sh [ "$status" -eq 1 ] [ "$output" = "Too many(few) arguments" ] } @test "File doesn't exist" { run bash ex08.sh resources/digitfile - [ "$status" -eq 1 ] [ "$output" = "Error.." ] } - diff --git a/ex09/ex09.sh b/ex09/ex09.sh new file mode 100755 index 0000000..0a4fb56 --- /dev/null +++ b/ex09/ex09.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +if [ -z $1 ] || [ -z $2 ]; then + exit 1 +fi + +if [ "$1" = "-u" ] || [ "$1" = "--url" ]; then + grep -Po '(^|\t)https:[A-Za-z0-9\/\.]+' $2 +elif [ "$1" = "-e" ] || [ "$1" = "--email" ]; then + grep -Po '(^|\t)[A-Za-z0-9\.]+@[A-Za-z0-9\.]+\.[A-Za-z0-9\.]+' $2 +else + exit 1 +fi diff --git a/ex09/test_ex09.sh b/ex09/test_ex09.sh index 7dad67c..4019883 100755 --- a/ex09/test_ex09.sh +++ b/ex09/test_ex09.sh @@ -2,28 +2,28 @@ @test "Find emails in file without emails" { run bash ex09.sh -e resources/email1.txt - + [ "$status" -eq 1 ] [ "$output" = "" ] } @test "Find emails in file with emails" { run bash ex09.sh -e resources/email2.txt - + [ "$status" -eq 0 ] [ "$output" = "craig@bbhost.us" ] } @test "Find urls in file without urls" { run bash ex09.sh -u resources/url1.txt - + [ "$status" -eq 1 ] [ "$output" = "" ] } @test "Find urls in file with urls" { run bash ex09.sh -u resources/url2.txt - + [ "$status" -eq 0 ] [ "$output" = "https://en.emailfake.com/usjima1129.ga/craig https://mail.google.com/mail/u/0/" ] @@ -31,14 +31,14 @@ https://mail.google.com/mail/u/0/" ] @test "Find emails in file with emails and urls" { run bash ex09.sh -e resources/emailurl.txt - + [ "$status" -eq 0 ] [ "$output" = "craig@bbhost.us" ] } @test "Find urls in file with emails and urls" { run bash ex09.sh -u resources/emailurl.txt - + [ "$status" -eq 0 ] [ "$output" = "https://en.emailfake.com/usjima1129.ga/craig https://mail.google.com/mail/u/0/" ] @@ -46,7 +46,7 @@ https://mail.google.com/mail/u/0/" ] @test "Test long flag for email" { run bash ex09.sh --email resources/emailurl.txt - + [ "$status" -eq 0 ] [ "$output" = "craig@bbhost.us" ] } @@ -54,7 +54,7 @@ https://mail.google.com/mail/u/0/" ] @test "Test long flag for url" { run bash ex09.sh --url resources/emailurl.txt - + [ "$status" -eq 0 ] [ "$output" = "https://en.emailfake.com/usjima1129.ga/craig https://mail.google.com/mail/u/0/" ] diff --git a/ex10/ex10.sh b/ex10/ex10.sh new file mode 100755 index 0000000..f3fd7bb --- /dev/null +++ b/ex10/ex10.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +if [ ! -f "$1" ];then + exit 1 +fi + +for i in $@ +do + echo "$(awk 'END{print NR}' $i) $i" +done