Unix ja shell-ohjelmointi (tie 205) Unix ja shell-ohjelmointi (tie 205)

Demo 3 vastaukset
Teht 1

#!/bin/sh
# Demo3 tehtava 1

list="a b c d e f"
reverse_list=""
for i in $list
do
  reverse_list="$i $reverse_list"
done
echo $reverse_list

Teht 2

#!/bin/sh
# Demo 3 tehtava 2

files=\*.tex
tar_file=tex_files.tar
hak=/home/mjev/
find "$hak" -name "$files" -exec tar -rf $tar_file {} \;

Teht 3

#!/bin/ksh
# Demo 3 tehtava 3
# Tapa 1 (tehoton)
# 
trap 'echo "kerittiin $n kierrosta"' 2
n=0
lista="$*"
set -A X $lista
echo "Jarjestamaton lista:        $lista"
koko=${#X[*]}
i=0
while [ $i -lt $koko ]
do
   let j=$i+1
   y=${X[i]} 
   z=${X[j]}
   let n=$n+1
   if [ -n "$z" ] ; then
    if [[ $y > $z ]]; then
       X[i]=$z
       X[j]=$y
       i=0
     else 
      let i=$i+1
     fi
   else
    let i=$i+1
   fi     
done
echo "Jarjestetty lista (tapa 1):${X[*]}"
echo "Vertailuja tehtiin:$n"

n=0
#
# Tapa 2
#
lista="$*"

while [ 1 ] ;do
  for i in $lista ;do
    if test "$l" = "" ;then
      a=$i; b=$a; l=1 ; continue
    else
      b=$i
    fi
     let n=$n+1 
    if [[ $a > $b ]] ;then
      c=$a; a=$b; b=$c; muutos=1
    fi
    jono="$jono $a"
    a=$b; 
  done
  lista="$jono $b"
  jono=""; l=""
  if test "$muutos" = "" ;then
    break;
  fi
  muutos=""
done
echo "Jarjestetty lista (tapa 2):$lista"
echo "Vertailuja tehtiin:$n"

Teht 4

#!/bin/sh
# Demo 3 tehtava 4

if [ $UID != 0 ]; then
  echo "Must be root!"
  exit   
fi

pw_file=passwd

echo -n "Anna kayttajatunnus:"
read user
if grep "$user" "$pw_file"
then 
   echo "Antamasi kayttaja $user loytyy jo!"
   exit
fi

echo -n "Anna UID:"
read uid

echo -n "Anna GID:"
read gid

echo -n "Anna kayttajan nimi:"
read name

echo -n "Anna kotihakemisto:"
read home

echo -n "Anna shell:"
read shll

mkdir -p "$home"
cp /etc/skel/.* "$home"
chmod -R 700 "$home"
chown -R "$uid"."$gid" "$home"
echo "$user:x:$uid:$gid:$name:$home:$shll" >> $pw_file

Teht 5

#!/bin/sh
# Demo 3 tehtava 5
# 
# HT. lisaa tehtavaan kayttajan syotteen tarkistukset
#     ja muut mahdolliset virhetilanteiden kasittelyt
#
if [ $UID != 0 ]; then
  echo "Must be root!"
  exit   
fi

loytyi=0
pw_file=passwd
cc_passwd=/home/mjev/syksy99_unix/demo3/ru_passwd
exec 3< "$cc_passwd"

tark_uid () {
uid=$1
if grep "$uid" "$pw_file"
then
   return 1
fi
return 0
}

kysy_tiedot () {
user=$1
pal=1
while [ $pal = 1 ]
do
  echo -n "Anna UID:"
  read uid
  tark_uid $uid
  pal=$?
done 
echo -n "Anna GID:"
read gid
echo -n "Anna kayttajan nimi:"
read name
echo -n "Anna kotihakemisto:"
read home
echo -n "Anna shell:"
read shll
mkdir -p "$home"
cp /etc/skel/.* "$home"
chmod -R 700 "$home"
chown -R "$uid"."$gid" "$home"
echo "$user:x:$uid:$gid:$name:$home:$shll" >> "$pw_file"
exit
}

echo -n "Anna kayttajatunnus:"
read user
if grep "$user" "$pw_file"
then 
   echo "Antamasi kayttaja $user loytyy jo!"
   exit
fi
oldifs="$IFS"
IFS=":"
while read cc_user pw cc_uid cc_gid cc_name cc_home cc_shll
do
  if [ "$user" = "$cc_user" ] ; then
    loytyi=1
    break
  fi
done <&3

IFS="$oldifs"
tark_uid "$cc_uid"
pal=$?

if [ "$loytyi" -eq 1 -a "$pal" -eq 0 ] 
then
  echo "Antamasi kayttaja $user loytyi"
  echo "$user:x:$cc_uid:$cc_gid:$cc_name:$cc_home:$cc_shll"
  echo -n "Lisataanko [K/e]?"
  read vast
  if [ "X$vast" = "X" -o "$vast" = "K" -o "$vast" = "k" ] 
  then
    mkdir -p "$cc_home"
    cp /etc/skel/.* "$cc_home"
    chmod -R 700 "$cc_home"
    chown -R "$cc_uid"."$cc_gid" "cc_$home"
    echo "$user:x:$cc_uid:$cc_gid:$cc_name:$cc_home:$cc_shll" >> $pw_file
  else 
    echo "Anna tiedot kayttajasta $user"
    kysy_tiedot $user
  fi
elif [ "$loytyi" -eq 1 -a "$pal" -eq 1 ] ; then
    echo "Antamasi kayttaja loytyi, mutta UID ei ole yksikasitteinen"
    kysy_tiedot $user
fi


File translated from TEX by TTH, version 1.98.
On 14 Oct 1999, 09:15.