From 0478f635e7e96392aab7f0fdd226b67999ff1408 Mon Sep 17 00:00:00 2001 From: abregman Date: Sun, 27 Jun 2021 03:15:34 +0300 Subject: [PATCH] Update --- README.md | 232 ++++++++++++++----------- exercises/scripts/elevator/solution.py | 8 + 2 files changed, 136 insertions(+), 104 deletions(-) create mode 100644 exercises/scripts/elevator/solution.py diff --git a/README.md b/README.md index 259dfbf..fc75169 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ programming
Programming
Python
Python
go
Go
- Bash
Shell Scripting
+ Bash
Scripts
kubernetes
Kubernetes
Prometheus
Prometheus
Mongo
Mongo
@@ -2630,7 +2630,7 @@ Alternatively if you are using a distro with systemd it's recommended to use sys history command or .bash_history file -##### Linux Permissions +#### Linux Permissions
How to change the permissions of a file?
@@ -2703,6 +2703,109 @@ True * No permissions
+#### Linux Shell Scripting + +
+What this line in scripts mean?: #!/bin/bash
+ + +`#!/bin/bash` is She-bang + +/bin/bash is the most common shell used as default shell for user login of the linux system. The shell’s name is an acronym for Bourne-again shell. Bash can execute the vast majority of scripts and thus is widely used because it has more features, is well developed and better syntax. + +
+ +
+True or False?: when a certain command/line fails, the script, by default, will exit and will no keep running
+ +Depends on the language and settings used. +When a script written in Bash fails to run a certain command it will keep running and will execute all other commands mentioned after the command which failed. +Most of the time we would actually want the opposite to happen. In order to make Bash exist when a specific command fails, use 'set -e' in your script. +
+ +
+Explain what would be the result of each command: + + * echo $0 + * echo $? + * echo $$ + * echo $@ + * echo $#
+
+ +
+How do you debug shell scripts?
+ +Answer depends on the language you are using for writing your scripts. If Bash is used for example then: + + * Adding -x to the script I'm running in Bash + * Old good way of adding echo statements + +If Python, then using pdb is very useful. +
+ +
+How do you get input from the user in shell scripts?
+ +Using the keyword read so for example read x will wait for user input and will store it in the variable x. +
+ +
+Explain continue and break. When do you use them if at all?
+
+ + +
+Running the following bash script, we don't get 2 as a result, why? + +``` +x = 2 +echo $x +``` +
+ +Should be `x=2` +
+ +
+How to store the output of a command in a variable?
+
+ +
+How do you check variable length?
+
+ + +
+Explain the following code: + +:(){ :|:& };: + +
+
+ +
+Can you give an example to some Bash best practices?
+
+ +
+What is the ternary operator? How do you use it in bash?
+ +A short way of using if/else. An example: + +[[ $a = 1 ]] && b="yes, equal" || b="nope" +
+ +
+What does the following code do and when would you use it? + +diff <(ls /tmp) <(ls /var/tmp) + +
+It is called 'process substitution'. It provides a way to pass the output of a command to another command when using a pipe | is not possible. It can be used when a command does not support STDIN or you need the output of multiple commands. +https://superuser.com/a/1060002/167769 +
+ #### Linux systemd
@@ -7285,6 +7388,17 @@ with open('file.txt', 'w') as file: Can you write a function which will print all the file in a given directory? including sub-directories
+
+Write a dictionary (variable) to a file
+ +``` +import json + +with open('file.json', 'w') as f: + f.write(json.dumps(dict_var)) +``` +
+ #### Python OS
@@ -8810,17 +8924,7 @@ Bones question: What is the random seek time in SSD and Magnetic Disk? Answer: Magnetic is about 10ms and SSD is somewhere between 0.08 and 0.16ms
-## Shell Scripting - -
-What this line in scripts mean?: #!/bin/bash
- - -`#!/bin/bash` is She-bang - -/bin/bash is the most common shell used as default shell for user login of the linux system. The shell’s name is an acronym for Bourne-again shell. Bash can execute the vast majority of scripts and thus is widely used because it has more features, is well developed and better syntax. - -
+## Scripts
What do you tend to include in every script you write?
@@ -8828,59 +8932,24 @@ Answer: Magnetic is about 10ms and SSD is somewhere between 0.08 and 0.16ms Few example: * Comments on how to run it and/or what it does - * Adding "set -e" since I want the script to exit if a certain command failed + * If a shell script, adding "set -e" since I want the script to exit if a certain command failed -You can have an entirely different answer. It's based only on your experience. +You can have an entirely different answer. It's based only on your experience and preferences.
-True or False?: when a certain command/line fails, the script, by default, will exit and will no keep running
- -Depends on the language and settings used. -When a script written in Bash fails to run a certain command it will keep running and will execute all other commands mentioned after the command which failed. -Most of the time we would actually want the opposite to happen. In order to make Bash exist when a specific command fails, use 'set -e' in your script. -
- -
-Today we have tools and technologies like Ansible. Why would someone still use shell scripting?
+Today we have tools and technologies like Ansible. Why would someone still use scripting?
* Speed * The module we need doesn't exist * We are delivering the scripts to customers who don't have access to the public network and don't necessarily have Ansible installed on their systems.
-
-Explain what would be the result of each command: - - * echo $0 - * echo $? - * echo $$ - * echo $@ - * echo $#
-
- -
-How do you debug shell scripts?
- -Answer depends on the language you are using for writing your scripts. If Bash is used for example then: - - * Adding -x to the script I'm running in Bash - * Old good way of adding echo statements - -If Python, then using pdb is very useful. -
- -
-How do you get input from the user in shell scripts?
- -Using the keyword read so for example read x will wait for user input and will store it in the variable x. -
-
Explain conditionals and how do you use them
-#### Shell Scripting - Loops +#### Scripts - Loops
What is a loop? What types of loops are you familiar with?
@@ -8890,32 +8959,9 @@ Using the keyword read so for example read x will wait Demonstrate how to use loops
-
-Explain continue and break. When do you use them if at all?
-
+#### Writing Scripts - -
-Running the following bash script, we don't get 2 as a result, why? - -``` -x = 2 -echo $x -``` -
- -Should be `x=2` -
- -
-How to store the output of a command in a variable?
-
- -
-How do you check variable length?
-
- -##### Practical +Note: write them in any language you prefer
Write a script which will list the differences between two directories
@@ -8937,7 +8983,6 @@ then mailx -s "Server $SERVERIP is down" -t "$NOTIFYEMAIL" < /dev/null fi ``` -
@@ -8960,36 +9005,15 @@ done
-Explain the following code: +Write a script that simulates an elevator
-:(){ :|:& };: +Hints: -

+* Think what properties an elevator has (direction, position, ...) +* Don't forget to use constraints and conditionals (can the elevator move down/up in any case?) +* It might be more comfortable to implement it with a language that supports OOP
-
-Can you give an example to some Bash best practices?
-
- -
-What is the ternary operator? How do you use it in bash?
- -A short way of using if/else. An example: - -[[ $a = 1 ]] && b="yes, equal" || b="nope" -
- -
-What does the following code do and when would you use it? - -diff <(ls /tmp) <(ls /var/tmp) - -
-It is called 'process substitution'. It provides a way to pass the output of a command to another command when using a pipe | is not possible. It can be used when a command does not support STDIN or you need the output of multiple commands. -https://superuser.com/a/1060002/167769 -
- - ## SQL
diff --git a/exercises/scripts/elevator/solution.py b/exercises/scripts/elevator/solution.py new file mode 100644 index 0000000..8860def --- /dev/null +++ b/exercises/scripts/elevator/solution.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python +# coding=utf-8 + +class Elevator(object): + + def __init__(self, direction=0, position=0): + self.direction = directopn + self.position = position