Linux:Shell Scripts
(Változatok közti eltérés) (→Paraméterek) |
(→Command Line Parameters) |
||
228. sor: | 228. sor: | ||
</megoldas> | </megoldas> | ||
- | |||
<kivonat>$0, $#, $@, exit, shift</kivonat> | <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"> | <code lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
- | echo " | + | echo "Name of the program: $0" |
- | echo " | + | echo "Number of parameters: $#" |
- | echo " | + | echo "List of parameters: $@" |
- | echo " | + | echo "parameters each by each: " |
for I in $@ ; do | for I in $@ ; do | ||
echo $I | echo $I | ||
243. sor: | 242. sor: | ||
</code> | </code> | ||
</megoldas> | </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"> | <code lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
if [ $# -ne 2 ] | if [ $# -ne 2 ] | ||
then | then | ||
- | echo " | + | echo "Usage: $0 param1 param2" |
exit | exit | ||
fi | fi | ||
</code> | </code> | ||
</megoldas> | </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"> | <code lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
261. sor: | 260. sor: | ||
</code> | </code> | ||
</megoldas> | </megoldas> | ||
- | * | + | * Solve the previous problem by rotating the shift command after processing one parameter to reach the other parameters. <megoldas> |
<code lang="bash"> | <code lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
270. sor: | 269. sor: | ||
</code> | </code> | ||
</megoldas> | </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"> | <code lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
# Kezdőérték és végérték default értékének beállítása | # Kezdőérték és végérték default értékének beállítása | ||
- | K=" | + | K="Unknown" |
- | V=" | + | V="Unknown" |
I=$1 | I=$1 | ||
while [ "$I" != "" ] | while [ "$I" != "" ] | ||
do | do | ||
case $I in | case $I in | ||
- | -v | -- | + | -v | --start ) |
shift | shift | ||
V=$1 | V=$1 | ||
;; | ;; | ||
- | -k | -- | + | -k | --end ) |
shift | shift | ||
K=$1 | K=$1 | ||
;; | ;; | ||
- | *) echo " | + | *) echo "Invalid parameter. Usage: $0 [-v startvalue] [-k endvalue]" |
exit 1 | exit 1 | ||
;; | ;; | ||
295. sor: | 294. sor: | ||
I=$1 | I=$1 | ||
done | done | ||
- | echo " | + | echo "Startvalue=$K, Endvalue=$V" |
</code> | </code> | ||
</megoldas> | </megoldas> |
A lap 2024. március 26., 10:10-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.
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.