rilpoint_mw113

Linux:Shell Scripts

(Változatok közti eltérés)
(Command Line Parameters)
 
(10 közbeeső változat nincs mutatva)
21. sor: 21. sor:
#!/bin/bash
#!/bin/bash
echo -n "Your name: "
echo -n "Your name: "
-
read NEV
+
read NAME
-
echo "Nice to see you, $NEV"
+
echo "Nice to see you, $NAME"
</code>
</code>
</megoldas>
</megoldas>
48. sor: 48. sor:
</megoldas>
</megoldas>
-
* 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.<code lang="bash">
+
* 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.<megoldas>
 +
<code lang="bash">
#!/bin/bash
#!/bin/bash
. params.conf
. params.conf
61. sor: 62. sor:
</code>
</code>
</box>
</box>
 +
 +
* 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.<megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
echo -n "Login name: "
 +
read NAME
 +
FULLNAME=`cat /etc/passwd | grep ^$NAME: | cut -d: -f5`
 +
echo "Full name: $FULLNAME"
 +
</code></megoldas>
 +
 +
 +
 +
<kivonat>if, case, &&, ||</kivonat>
 +
* Create a program that determines if a number requested at run time is greater than 5! <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
echo -n "Number:"
 +
read NUMBER
 +
if [ "$NUMBER" -gt 5 ] ; then
 +
  echo "A $NUMBER greater than 5"
 +
else
 +
  echo "A $NUMBER less than 5"
 +
fi
 +
</code>
 +
</megoldas>
 +
* 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. <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
# Created by Magyar Péter
 +
echo -n "First number:"
 +
read NUMBER1
 +
echo -n "Second number:"
 +
read NUMBER2
 +
if [ "$NUMBER1" -gt "$NUMBER2" ] ; then
 +
  echo "A(z) $NUMBER1 is greater than $NUMBER2."
 +
else
 +
  if [ "$NUMBER2" -gt "$NUMBER1" ] ; then
 +
    echo "A(z) $NUMBER2 is greater than $NUMBER1."
 +
  else
 +
    echo "Equal numbers."
 +
  fi
 +
fi
 +
</code>
 +
</megoldas>
 +
* 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.  <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
# Created by Magyar Péter
 +
echo "Logging starts." > ~/events.log
 +
echo -n "First number:"
 +
read A
 +
echo "First number = $A" >> ~/events.log
 +
echo -n "Second number:"
 +
read B
 +
echo "Second number = $B" >> ~/events.log
 +
if [ "$A" -gt "$B" ] ; then
 +
  echo "A(z) $A is greater than $B."
 +
  echo "A(z) $A is greater than $B." >> ~/events.log
 +
else
 +
  if [ "$B" -gt "$A" ] ; then
 +
    echo "A(z) $B is greater than $A."
 +
    echo "A(z) $B is greater than $A." >> ~/events.log
 +
  else
 +
    echo "Equal numbers."
 +
    echo "Equal numbers." >> ~/events.log
 +
  fi
 +
fi
 +
echo "End of run." >> ~/events.log
 +
</code>
 +
</megoldas>
 +
 +
* Create a program that asks for the serial number of a day of the week and prints the name of the corresponding day! <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
echo -n "Number of the day: "
 +
read N
 +
case $N in
 +
  1) echo "Monday" ;;
 +
  2) echo "Tuesday" ;;
 +
  3) echo "Wednesday" ;;
 +
  4) echo "Thursday" ;;
 +
  5) echo "Friday" ;;
 +
  6) echo "Saturday" ;;
 +
  7) echo "Sunday" ;;
 +
  *) echo "Invalid day." ;;
 +
esac
 +
</code>
 +
</megoldas>
 +
 +
* 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. <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
echo -n "Length of A: "
 +
read A
 +
echo -n "Length of B: "
 +
read B
 +
if [ "$A" -gt 0 -a "$B" -gt 0 ]
 +
then
 +
  echo "Area: " ` expr $A \* $B ` "."
 +
else
 +
  echo "Invalid side lengths."
 +
fi
 +
</code>
 +
</megoldas>
 +
 +
* Create a program that checks if the root user is running it, if not, give an error message! <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
if [ $USER != "root" ] ; then
 +
  echo "You are not the system administrator."
 +
  exit
 +
fi
 +
echo "You are the system administrator."
 +
</code>
 +
 +
* Create a program that displays the greeting according to the time of day based on the current time! <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
HOUR=`date '+%H'`
 +
N="night"
 +
if [ $HOUR -ge 5 -a $HOUR -lt 12 ] ; then
 +
  N="morning"
 +
fi
 +
if [ $HOUR -ge 12 -a $HOUR -lt 18 ] ; then
 +
  N="afternoon"
 +
fi
 +
if [ $HOUR -ge 18 -o $HOUT -lt 5 ] ; then
 +
  N="night"
 +
fi
 +
echo "It is $HOUR o\' clock! Good $N!"
 +
</code>
 +
</megoldas>
 +
 +
* 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! <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
echo -n "Login name: "
 +
read NAME
 +
if [ -z "$NAME" ] ; then
 +
  echo "Missing login name."
 +
  exit
 +
fi
 +
FULLNAME=`cat /etc/passwd | grep ^$NAME: | cut -d: -f5`
 +
if [ ! -z "$FULLNAME" ] ; then
 +
  echo "Full name: $FULLNAME"
 +
else
 +
  echo "There is no user with name: $NAME."
 +
fi
 +
</code>
 +
</megoldas>
 +
 +
=== Error handling ===
 +
<kivonat>$?</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. <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
# Errors should be redirected to /dev/null
 +
mkdir /etc/alma 2>/dev/null
 +
# The return value is in the variable named ERR
 +
ERR=$?
 +
if [ $ERR -ne 0 ] ; then
 +
  echo "Error creating directory: $ERR"
 +
fi
 +
</code>
 +
</megoldas>
 +
 +
=== Command Line Parameters ===
 +
 +
<kivonat>$0, $#, $@, exit, shift</kivonat>
 +
* Create a program called parmstest that prints its own name, the number of its parameters, the list of parameters, and the parameters one by one. (Example to test: ''./parmstest alma pear walnut'') <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
echo "Name of the program: $0"
 +
echo "Number of parameters: $#"
 +
echo "List of parameters: $@"
 +
echo "parameters each by each: "
 +
for I in $@ ; do
 +
  echo $I
 +
done
 +
</code>
 +
</megoldas>
 +
* Create a program part that expects two parameters. If the program is not called with two parameters, give an error message. <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
if [ $# -ne 2 ]
 +
then
 +
  echo "Usage: $0 param1 param2"
 +
  exit
 +
fi
 +
</code>
 +
</megoldas>
 +
* Create a program that can display more than 10 parameters in a row! So, the result of <b>manyParameters 1 2 3 4 5 6 7 8 9 10 11 12 13 14</b> should be the answer to display all 14 parameters! (Remember, the parameters are only available up to $9!) <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
for I in $@ ;do
 +
  echo $I
 +
done
 +
</code>
 +
</megoldas>
 +
* Solve the previous problem by rotating the shift command after processing one parameter to reach the other parameters.  <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
while [ "$1" != "" ] ; do
 +
  echo -n "$1 "
 +
  shift
 +
done
 +
</code>
 +
</megoldas>
 +
* Create a program called parmtest that takes two parameters, a start and an end value, either of which can be omitted. The program should parse the two parameters in such a way that they can be given in any order in the form <b>parmtest -v endvalue -k startvalue</b> (so <b>parmtest -k startvalue -v endvalue</b> is also the correct form). At the end of the parsing, the script should put the start value in K and the end value in V and display these two values. <megoldas>
 +
<code lang="bash">
 +
#!/bin/bash
 +
# Setting the value of StartValue and EndValue
 +
K="Unknown"
 +
V="Unknown"
 +
I=$1
 +
while [ "$I" != "" ]
 +
do
 +
  case $I in
 +
  -v | --start )
 +
      shift
 +
      V=$1
 +
      ;;
 +
  -k | --end )
 +
      shift
 +
      K=$1
 +
      ;;
 +
  *)  echo "Invalid parameter. Usage: $0 [-v startvalue] [-k endvalue]"
 +
      exit 1
 +
      ;;
 +
  esac
 +
  shift
 +
  I=$1
 +
done
 +
echo "Startvalue=$K, Endvalue=$V"
 +
</code>
 +
</megoldas>

A lap jelenlegi, 2024. március 26., 10:12-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

Kivonat: $0, $#, $@, exit, shift
  • Create a program called parmstest that prints its own name, the number of its parameters, the list of parameters, and the parameters one by one. (Example to test: ./parmstest alma pear walnut)
  • Create a program part that expects two parameters. If the program is not called with two parameters, give an error message.
  • Create a program that can display more than 10 parameters in a row! So, the result of manyParameters 1 2 3 4 5 6 7 8 9 10 11 12 13 14 should be the answer to display all 14 parameters! (Remember, the parameters are only available up to $9!)
  • Solve the previous problem by rotating the shift command after processing one parameter to reach the other parameters.
  • Create a program called parmtest that takes two parameters, a start and an end value, either of which can be omitted. The program should parse the two parameters in such a way that they can be given in any order in the form parmtest -v endvalue -k startvalue (so parmtest -k startvalue -v endvalue is also the correct form). At the end of the parsing, the script should put the start value in K and the end value in V and display these two values.
Skin by RIL Partner