Sunteți pe pagina 1din 9

####1

# $# - numar parametri.
# $@ - toti parametri.
# $1 - primul parametru.
# $2 - al doilea parametru.
# $0 - nume script. in cazul acesta an_nou_1.sh.
# $? - cod retur al ultimei comenzi date.
####
#!/bin/bash

# ./script.sh [dir_sursa] extensie dir_dest

dir_sursa=$1
extensie=$2
dir_dest=$3

if test $# -eq 2
then
dir_sursa=.
extensie=$1
dir_dest=$2
elif test $# -lt 2
then
echo "Utilizare: $( basename "$0") [dir_sursa] extensie dir_dest" 1>&2
exit
elif test $# -gt 3
then
echo "Utilizare: ./$(basename "$0") [dir_sursa] extensie dir_dest" 1>&2
exit
fi

if ! test -d "$dir_sursa"
then
echo "Directoru sursa invalid." 1>&2
exit
fi

if ! test -d "$dir_dest"
then
mkdir -p "$dir_dest" 2>/dev/null
echo "Directorul dest nu exista, se creaza automat."
fi

contor=0
contor_copiat=0
fisiere_necopiate="\n"
fisiere_existente="\n"
for file in "$dir_sursa"/*."$extensie" ; do
if test -f "$file"
then
nume_fisier=$(basename "$file")
((contor++))
if test -f "$dir_dest"/"$nume_fisier"
then
fisiere_existente="$fisiere_existente \n"$nume_fisier""
continue
fi
cp "$file" "$dir_dest" 2>/dev/null
if test $? -eq 0
then
((contor_copiate++))
else
fisiere_necopiate="$fisiere_necopiate \n"$nume_fisier""
fi
fi
done

echo "S-au copiat $contor_copiate din $contor fisiere gasite."


if test -n "$fisiere_necopiate"
then
echo -e "Fisiere necopiate: $fisiere_necopiate"
fi
if test -n "$fisiere_existente"
then
echo -e "Fisiere deja existente:$fisiere_existente"
fi

####2
#!/bin/bash

# ./script.sh [dir_sursa] extensie dir_dest

dir_sursa=$1
extensie=$2
dir_dest=$3

if test $# -eq 2
then
dir_sursa=.
extensie=$1
dir_dest=$2
elif test $# -lt 2
then
echo "Utilizare: $( basename "$0") [dir_sursa] extensie dir_dest" 1>&2
exit
elif test $# -gt 3
then
echo "Utilizare: ./$(basename "$0") [dir_sursa] extensie dir_dest" 1>&2
exit
fi

if ! test -d "$dir_sursa"
then
echo "Directoru sursa invalid." 1>&2
exit
fi

if ! test -d "$dir_dest"
then
mkdir -p "$dir_dest" 2>/dev/null
echo "Directorul dest nu exista, se creaza automat."
fi

contor=0
contor_copiat=0
fisiere_necopiate=""
for file in "$dir_sursa"/*."$extensie" ; do
if test -f "$file"
then
((contor++))
cp "$file" "$dir_dest" 2>/dev/null
if test $? -eq 0
then
((contor_copiate++))
else
fisiere_necopiate="$fisiere_necopiate "$file""
fi
fi
done

echo "S-au copiat $contor_copiate din $contor fisiere gasite."


if test -n "$fisiere_necopiate"
then
echo "Fisiere necopiate: $fisiere_necopiate"
fi

####3
#!/bin/bash

# ./script.sh [dir_sursa] extensie dir_dest

dir_sursa=$1
extensie=$2
dir_dest=$3

if test $# -eq 2
then
dir_sursa=.
extensie=$1
dir_dest=$2
elif test $# -lt 2
then
echo "Utilizare: $( basename "$0") [dir_sursa] extensie dir_dest" 1>&2
exit
elif test $# -gt 3
then
echo "Utilizare: ./$(basename "$0") [dir_sursa] extensie dir_dest" 1>&2
exit
fi

if ! test -d "$dir_sursa"
then
echo "Directoru sursa invalid." 1>&2
exit
fi

if ! test -d "$dir_dest"
then
mkdir -p "$dir_dest" 2>/dev/null
echo "Directorul dest nu exista, se creaza automat."
fi

contor=0
contor_copiat=0
for file in "$dir_sursa"/*."$extensie" ; do
if test -f "$file"
then
((contor++))
cp "$file" "$dir_dest" 2>/dev/null
if test $? -eq 0
then
((contor_copiate++))
fi
fi
done

echo "S-au copiat $contor_copiate din $contor fisiere gasite."

####4
#!/bin/bash

for fisier in *; do
if test -f "$fisier"
then
ext=$(echo "$fisier" | cut -d "." -f 2)
name=$(echo "$fisier" | cut -d "." -f 1)

if ! test "$name" = "$ext"


then
ext=$(echo $ext | tr '[a-z]' '[A-Z]')
echo "$name.$ext"
else
echo "$name"
fi

fi
done

####5
#!/bin/bash

while true; do
echo "Introdu numele server-ului."
read server
if test -z "$server"
then
echo "Nu ai introdus niciun server."
continue
fi
server=$(echo $server | tr [:upper:] [:lower:])
if test "$server" = "exit"
then
echo "Inchidere program"
break
fi

/usr/bin/ping -w 5 $server 1>/dev/null 2>&1


return=$?
case $return in
0) echo "$server -- operational" ;;
1) echo "$server -- neoperational" ;;
*) echo "$server -- necunosut" ;;
esac
done

####6
#!/bin/bash

file=$1

if test $# -eq 0
then
echo "Nu ai introdus niciun parametru."
exit
fi

if ! test -f "$file"
then
echo "Parametrul introdus nu este fisier."
exit
fi

if ! test -r "$file"
then
echo "Fisierul nu poate fi citit."
exit
fi

if ! test -s "$file"
then
echo "Fisierul specificat ca parametru este vid."
exit
fi
num=0
while read line ; do
var=$(echo $line | grep -o [0-9] | wc -w)
if test $var -gt 0
then
num=$(($num+$var))
echo $var
done < "$file"

#echo "Numar cifre gasite: $num"

####7
#!/bin/bash

file=$1

if test $# -eq 0
then
echo "Nu ai introdus niciun parametru."
exit
fi

if ! test -f "$file"
then
echo "Parametrul introdus nu este fisier."
exit
fi

if ! test -r "$file"
then
echo "Fisierul nu poate fi citit."
exit
fi

if ! test -s "$file"
then
echo "Fisierul specificat ca parametru este vid."
exit
fi
num=0
while read line ; do
case $line in
[0-9]) ((num++)) ;;
*) : ;;
esac
done < "$file"

echo "Numar cifre gasite: $num"

####8
#!/bin/bash

dir=$1

if test $# -eq 0
then
echo "Nu ai introdus destui parametri."
exit
fi

if ! test -d "$dir"
then
dir=$PWD
fi

sum=0

for j in "$dir"/* ; do
if test -f "$j"
then
dim=$(stat -c %s "$j")

if test $dim -gt 50


then
echo "Fisier: $j ; Dimensiune: $dim"
sum=$(($sum+$dim))
fi
fi
done
echo "Dimensiune totala: $sum"

####9
#!/bin/bash

dir=$1

if test $# -eq 0
then
echo "Nu ai introdus destui parametri."
exit
fi
if ! test -d "$dir"
then
dir=$PWD
fi

sum=0

for j in "$dir"/* ; do
if test -f "$j"
then
dim=$(stat -c %s "$j")
echo "Fisier: $j ; Dimensiune: $dim"
sum=$(($sum+$dim))
fi
done
echo "Dimensiune totala: $sum"

####10
#!/bin/bash

dir=$1

if test $# -eq 0
then
echo "Nu ai introdus parametrii."
exit
fi

if ! test -d "$dir"
then
echo "Nu ai specificat un director valid."
exit
fi

for i in ./"$dir"/*.sh ; do
if test -f "$i"
then
echo "Fisier: $i"
fi

done

####11
#!/bin/bash

#$1 - nr 1
#$2 - nr 2
#$3 - nr 3
#$# - nr parametrii
#$@ - toti

echo "Ce faci?"


read rasp

case $rasp in
[Bb]ine)
echo "Ma bucur ca faci bine :D" ;;
[Rr]au)
echo "Imi pare rau :(" ;;
*)
echo "NU TE INTELEG." ;;
esac

####12
#!/bin/bash

#$1 - nr 1
#$2 - nr 2
#$3 - nr 3
#$# - nr parametrii
#$@ - toti

echo -n "Introdu string: "


read fis

if test -n "$fis"
then

if test -d $HOME/"$fis"
then
echo ""$fis" se afla in directorul meu casa!"
else
echo""$fis" nu se afla in directorul meu casa!"
fi

####13
#!/bin/bash

#$1 - nr 1
#$2 - nr 2
#$3 - nr 3
#$# - nr parametrii
#$@ - toti

if test $# -eq 3
then

var1=$1
var2=$2
var3=$3

stat=$(echo $@ | tr -d " " | grep -E ^[0-9]+$)

if test -z $stat
then
echo "Unul dintre numere nu este valid :("
exit
fi

min=$var1

if test $var2 -lt $min


then
min=$var2
fi
if test $var3 -lt $min
then
min=$var3
fi

echo "Cel mai mic dintre numerele "$var1,$var2,$var3" este: $min"

else
echo "Nu ai introdus exact 3 parametrii"
fi

S-ar putea să vă placă și