rilpoint_mw113

Linux:Shell Scripts

(Változatok közti eltérés)
(Error handling)
225. sor: 225. sor:
   echo "Error creating directory: $ERR"
   echo "Error creating directory: $ERR"
fi
fi
 +
</code>
 +
</megoldas>
 +
 +
=== Command Line Parameters ===
 +
=Paraméterek=
 +
<kivonat>$0, $#, $@, exit, shift</kivonat>
 +
* Készíts programot parmstest néven, mely kiírja saját nevét, paramétereinek számát, a paraméterek listáját, és a paramétereket egyenként! (Példa a kipróbálásra: ''./parmstest alma körte dió'') <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
echo "Az elindított program neve: $0"
 +
echo "Paraméterek száma: $#"
 +
echo "Paraméterlista: $@"
 +
echo "Paraméterek egyenként: "
 +
for I in $@ ; do
 +
  echo $I
 +
done
 +
</code>
 +
</megoldas>
 +
* Készíts programrészt, mely két paramétert vár. Amennyiben a programot nem két paraméterrel hívjuk meg, adjon hibaüzenetet! <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
if [ $# -ne 2 ]
 +
then
 +
  echo "A program hasznalata: $0 param1 param2"
 +
  exit
 +
fi
 +
</code>
 +
</megoldas>
 +
* Készíts programot, mely 10-nél több paramétert is képes sorban megjeleníteni! Tehát a <b>sokParameter 1 2 3 4 5 6 7 8 9 10 11 12 13 14</b> eredménye mind a 14 paraméter megjelenítése legyen a válasz! (Ne feledje, a paraméterek csak $9-ig érhetők el!) <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
for I in $@ ;do
 +
  echo $I
 +
done
 +
</code>
 +
</megoldas>
 +
* Oldja meg az előző feladatot úgy, hogy egy paraméter feldolgozása után a további paramétereket a shift paranccsal rotálva éri el! <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
while [ "$1" != "" ] ; do
 +
  echo -n "$1 "
 +
  shift
 +
done
 +
</code>
 +
</megoldas>
 +
* Készíts programot parmtest néven, mely paraméterként két paramétert vár, egy kezdő- és egy végértéket, melyek közül bármelyik elhagyható. A program dologozza fel a két paramétert úgy, hogy azok tetszőleges sorrendben legyenek megadhatók a következő formában: <b>parmtest -v vegertek -k kezdoertek</b> (tehát a <b>parmtest -k kezdoertek -v vegertek</b> is helyes megadási forma). A feldogozás végén a script a kezdőértéket K-ba, a végértéket V-be tegye, és jelenítse meg e két adatot! <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
# Kezdőérték és végérték default értékének beállítása
 +
K="Nincs megadva"
 +
V="Nincs megadva"
 +
I=$1
 +
while [ "$I" != "" ]
 +
do
 +
  case $I in
 +
  -v | --veg )
 +
      shift
 +
      V=$1
 +
      ;;
 +
  -k | --kezdet )
 +
      shift
 +
      K=$1
 +
      ;;
 +
  *)  echo "Érvénytelen paraméter. A program használata: $0 [-v kezdet] [-k vég]"
 +
      exit 1
 +
      ;;
 +
  esac
 +
  shift
 +
  I=$1
 +
done
 +
echo "Kezdőérték=$K, végérték=$V"
</code>
</code>
</megoldas>
</megoldas>

A lap 2024. március 26., 10:05-kori változata

  • Create a text file of any content with mcedit! The file name should be test.txt!
  • Create a shell script that prints the following text: Hello world!
  • Create a program that assigns a value to variable named A and then shows its value.
  • Create a program that asks for the user's name from the keyboard and greets him in person!
  • Create a program that prints the English abbreviation of today's name! The name of the current day can be obtained by properly parameterizing the date command. (Use the man page of the date command!)
  • Create a program that asks the length of the side of a square and then displays its perimeter and area. The name of the program should be square!
  • Create a program that takes its parameters from a configuration file! Read the variables named NAME and AGE from the params.conf file and print their values.
Content of params.conf:
NAME="Kiss Lajos"
AGE=60
  • Create a program that asks for a user's login name and looks up its full name in the / etc / passwd file! The name of the program should be usersearch.


Kivonat: if, case, &&, ||
  • Create a program that determines if a number requested at run time is greater than 5!
  • Create a program that prompts for two numbers and prints the larger one on the screen. If the two numbers are equal, it is also displayed.
  • Extend the previous task by creating a detailed log entry of events that occurred during the run in the "~ /events.log" file so that each run generates its own content, overwriting previous logging results.
  • Create a program that asks for the serial number of a day of the week and prints the name of the corresponding day!
  • Create a program that asks for the size of the sides of a rectangle and prints its area. The program must check if the specified page lengths are greater than 0.
  • Create a program that checks if the root user is running it, if not, give an error message!
  • Create a program that asks for a user's login name and looks up its full name in the /etc/passwd file! The program will check if the user has actually given a name to search for and give a meaningful answer even if the user you are looking for is not in the system!

Error handling

Kivonat: $?
  • Create a program that creates a directory called /tmp/testDir. Do not display any error messages, but if the directory creation fails, write your own error message.

Command Line Parameters

Paraméterek

Kivonat: $0, $#, $@, exit, shift
  • Készíts programot parmstest néven, mely kiírja saját nevét, paramétereinek számát, a paraméterek listáját, és a paramétereket egyenként! (Példa a kipróbálásra: ./parmstest alma körte dió)
  • Készíts programrészt, mely két paramétert vár. Amennyiben a programot nem két paraméterrel hívjuk meg, adjon hibaüzenetet!
  • Készíts programot, mely 10-nél több paramétert is képes sorban megjeleníteni! Tehát a sokParameter 1 2 3 4 5 6 7 8 9 10 11 12 13 14 eredménye mind a 14 paraméter megjelenítése legyen a válasz! (Ne feledje, a paraméterek csak $9-ig érhetők el!)
  • Oldja meg az előző feladatot úgy, hogy egy paraméter feldolgozása után a további paramétereket a shift paranccsal rotálva éri el!
  • Készíts programot parmtest néven, mely paraméterként két paramétert vár, egy kezdő- és egy végértéket, melyek közül bármelyik elhagyható. A program dologozza fel a két paramétert úgy, hogy azok tetszőleges sorrendben legyenek megadhatók a következő formában: parmtest -v vegertek -k kezdoertek (tehát a parmtest -k kezdoertek -v vegertek is helyes megadási forma). A feldogozás végén a script a kezdőértéket K-ba, a végértéket V-be tegye, és jelenítse meg e két adatot!
Skin by RIL Partner