Naloga 4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Domača naloga – lupina bash 2.12.

2019

V nadaljevanju imate zapisane vzorce preprostih skript. Prepišite jih v bash lupino, uredite in
preizkusite delovanje.

Vsako skripto opremite s svojimi podatki, kot smo se dogovorili pri vajah

Rezultate objavite v e-učilnici

1) Napišite skripto, ki bo izračunala vsoto, povprečje in produkt štirih vnesenih števil

Echo “Enter four integers with space between”


Read a b c d
Sum =`expr $a + $b + $c + $d`
Avg =`expr $sum / 4`
Dec =`expr $sum % 4`
Dec =`expr \ ($dec \* 1000 \) / 4`
Product =`expr $a \* $b \* $c \* $d`
Echo Sum = $sum
Echo Average = $avg. $dec
Echo Product = $product

echo “Enter four integers with space between”

read a b c d

sum=`expr $a + $b + $c + $d`

avg=`expr $sum / 4`

dec=`expr $sum % 4`

dec=`expr \( $dec \* 1000 \) / 4`

product=`expr $a \* $b \* $c \* $d`

echo sum=$sum

echo average=$avg. $dec

echo product=$product

2) Napišite skripto, ki bo zamenjala vrednosti dveh spremenljivk

Echo “Enter value for a:”


Read a
Echo “Enter value for b:”
Read b
Clear
Echo “Values of variables before swapping”
Echo A = $a
Echo B = $b
Echo Values of variables after swapping
a = `expr $a + $b`
b = `expr $a – $b`
a = `expr $a – $b`
Echo A = $a
Echo B = $b

echo "Enter value for a:"

read a

echo "Enter value for b:"

read b

clear

echo "Values of variables before swapping"

echo A = $a

echo B = $b

echo Values of variables after swapping

a=`expr $a + $b`

b=`expr $a - $b`

a=`expr $a - $b`

echo A = $a

echo B = $b

3) V tekstu (poljubna datoteka) poiščite vrstice, ki vsebujejo števila

Echo “Enter filename”


Read filename
Grep [0-9] $filename

echo “Enter filename”

read filename
grep [0-9] $filename

4) Napišite skripto, ki bo zamenjala cifre v petmestnem številu

Echo “Enter a 5 digit number”


Read num
n = $num
rev=0
while [ $num -ne 0 ]
do
r = `expr $num % 10`
rev = `expr $rev \* 10 + $r`
num = `expr $num / 10`
done
Echo “Reverse of $n is $rev”

echo "Enter a 5 digit number"

read num

n=$num

rev=0

while [ $num -ne 0 ]

do

r=`expr $num % 10`

rev=`expr $rev \* 10 + $r`

num=`expr $num / 10`

done

echo “Reverse of $n is $rev”

5) Napišite skripto, ki bo združila dva niza in izpisala dolžino novega niza

Echo “Enter first string:”


Read s1
Echo “Enter second string:”
Read s2
s3 = $s1$s2
len = `Echo $s3 | wc -c`
len = `expr $len – 1`
Echo “Concatenated string is $s3 of length $len ”
echo "Enter first string:"

read s1

echo "Enter second string:"

read s2

s3=$s1$s2

len=`echo $s3 | wc -c`

len=`expr $len - 1`

echo "Concatenated string is $s3 of length $len "

6) Napišite skripto, ki bo poskala izbrani del poljubnega niza znakov

Echo “Enter main string:”


Read main
l1 = `Echo $main | wc -c`
l1 = `expr $l1 – 1`
Echo “Enter sub string:”
Read sub
l2 = `Echo $sub | wc -c`
l2 = `expr $l2 – 1`
n=1
m=1
pos = 0
while [ $n -le $l1 ]
do
a = `Echo $main | cut -c $n`
b = `Echo $sub | cut -c $m`
if [ $a = $b ]
then
n = `expr $n + 1`
m = `expr $m + 1`
pos = `expr $n – $l2`
r = `expr $m – 1`
if [ $r -eq $l2 ]
then
break
fi
else
pos = 0
m=1
n = `expr $n + 1`
fi
done
Echo “Position of sub string in main string is $pos”
echo "Enter main string:"

read main

l1=`echo $main | wc -c`

l1=`expr $l1 - 1`

echo "Enter sub string:"

read sub

l2=`echo $sub | wc -c`

l2=`expr $l2 - 1`

n=1

m=1

pos=0

while [ $n -le $l1 ]

do

a=`echo $main | cut -c $n`

b=`echo $sub | cut -c $m`

if [ $a = $b ]

then

n=`expr $n + 1`

m=`expr $m + 1`

pos=`expr $n - $l2`

r=`expr $m - 1`

if [ $r -eq $l2 ]

then

break

fi

else

pos=0
m=1

n=`expr $n + 1`

fi

done

echo "Position of sub string in main string is $pos"

7) Napišite skripto, ki bo seštela in odštela dve števili, ki jih podate kot argument

add = `expr $1 + $2`


sub = `expr $1 – $2`
mul = `expr $1 \* $2`
Echo “Addition of $1 and $2 is $add”
Echo “Subtraction of $2 from $1 is $sub”
Echo “Multiplication of $1 and $2 is $mul”

add=`expr $1 + $2`

sub=`expr $1 - $2`

mul=`expr $1 \* $2`

echo "Addition of $1 and $2 is $add"

echo "Subtraction of $2 from $1 is $sub"

echo "Multiplication of $1 and $2 is $mul"

8) Napišite skripto, ki bo vsebino datoteke pretvorila v velike črke

Echo “Enter the filename”


Read filename
Echo “Contents of $filename before converting to uppercase”
Echo —————————————————-
cat $filename
Echo —————————————————-
Echo “Contents of $filename after converting to uppercase”
Echo —————————————————
tr ‘[a-z]’ ‘[A-Z]’ < $filename
Echo —————————————————

echo "Enter the filename"

read filename

echo "Contents of $filename before converting to uppercase"


echo -------------------

cat $filename

echo -------------------

echo "Contents of $filename after converting to uppercase"

echo —————————————————

tr ‘[a-z]’ ‘[A-Z]’ < $filename

echo -------------------

9) Napišite skripto, ki bo preštela znake, besede in vrstice v izbranem tekstu

Echo “Enter the filename”


Read file
w = `cat $file | wc -w`
c = `cat $file | wc -c`
l = `grep -c “.” $file`
Echo “Number of characters in $file is $c”
Echo “Number of words in $file is $w”
Echo “Number of lines in $file is $l”

echo "Enter the filename"

read file

w=`cat $file | wc -w`

c=`cat $file | wc -c`

l=`grep -c "." $file`

echo "Number of characters in $file is $c"

echo "Number of words in $file is $w"

echo "Number of lines in $file is $l"

10) Napišite skripto, ki bo združila vsebini dveh datotek

Echo “Enter first filename”


Read first
Echo “Enter second filename”
Read second
cat $first > third
cat $second >> third
Echo “After concatenation of contents of entered two files”
Echo —————————————————-
cat third | more
Echo —————————————————-

echo "Enter first filename"

read first

echo "Enter second filename"

read second

cat $first > third

cat $second >> third

echo "After concatenation of contents of entered two files"

echo ------------------

cat third | more

echo ------------------

11) Skripta naj obrne vhodni niz in ga izpiše

Echo -n “enter the string u want to reverse:-”


Read string
len = `Echo -n $string |wc -c`
Echo “no of character is:- $len”
while test $len -gt 0
do
rev = $rev`Echo $string |cut -c $len`
len = `expr $len – 1`
done
Echo “the reverse string is:-$rev ”

echo -n "enter the string u want to reverse:-"

read string

len=`echo -n $string | wc -c`

echo "no of character is:- $len"

while test $len -gt 0


do

rev=$rev`echo $string | cut -c $len`

len=`expr $len - 1`

done

echo "the reverse string is:-$rev "

12) Napišite skripto, ki bo izračunala povprečje vnesenih števil

total=0
count=$#
for i #or u can append ( in $*) to get same result.
do
total=`expr $total + $i`
done
avg1=`expr $total / $count`
avg2=`expr $total % $count`
avg2=`expr $avg2 \* 100 / $count`
echo “The Average is :- $avg1.$avg2”

total=0

count=$#

for i #or u can append ( in $*) to get same result.

do

total=`expr $total + $i`

done

avg1=`expr $total / $count`

avg2=`expr $total % $count`

avg2=`expr $avg2 \* 100 / $count`

echo "The Average is :- $avg1.$avg2"

13) Napišite skripto, ki bo preštela presledke in posebne znake v danem tekstu

Echo “Enter a text”


Read text
s=0
alpha = 0
j=``
n=1
while [ $n -le $c ]
do
ch = `Echo $text | cut -c $n`
if test $ch = $j
then
s = `expr $s + 1`
fi
case $ch in
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) alpha=`expr $alpha + 1`;;
esac
n = `expr $n + 1`
done
special = `expr $c – $s – $alpha`
Echo “Spaces = $s”
Echo “Special symbols = $special”

echo Enter a text

read text

w=`echo $text | wc -w`

w=`expr $w`

c=`echo $text | wc -c`

c=`expr $c - 1`

s=0

alpha=0

j=` `

n=1

while [ $n -le $c ]

do

ch=`echo $text | cut -c $n`

if test $ch=$j

then

s=`expr $s + 1`

fi

case $ch in

a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) alpha=`expr $alpha + 1`;;

esac
n=`expr $n + 1`

done

special=`expr $c - $s - $alpha`

echo Words=$w

echo Characters=$c

echo Spaces=$s

echo Special symbols=$special

You might also like