---
# System prepended metadata

title: Lab 3. Shell scripting and Systemd

---

# Lab 3. Shell scripting and Systemd
## *Kseniya Evdokimova*
---

## **Questions to answer:**
#### 1. Create a simple script to print odd numbers from 1 to 10.

- Lets create a script 
`$vim myfunction`
- Inside vim type the following
![](https://i.imgur.com/RB2e3Vo.png)
- We need to make this script executable
`$chmod 700 myfunction`
- To run the script execute:
`./myfunction`
![](https://i.imgur.com/d8Pni3Z.png)
Or one more solution:
![](https://i.imgur.com/jly2rwK.png)

#### 2. Create a shell script which makes new users and their corresponding passwords for 10 accounts in the system. Script should read the data from the file users.txt(you can write data to this file).
- Create users.txt
`nano users.txt`
![](https://i.imgur.com/VSVYiLY.png)
- Create the script for 10 users
`nano userscreator`
![](https://i.imgur.com/QQvkI8s.png)
- We need to make this script executable
`$chmod 700 userscreator`
![](https://i.imgur.com/qak5dhm.png)
- To run the script execute:
`./userscreator`
![](https://i.imgur.com/zDXBct9.png)
And as the output we will get:
![](https://i.imgur.com/qslt1rl.png)

#### 3. Provide an example of the shell script where you can pass result (not an exit code) of the executed function in a subshell to the parent’s shell
The whole point of a subshell is that it doesn't affect the calling session. In bash a subshell is a child process, other shells differ but even then a variable setting in a subshell does not affect the caller. So the subshell should't have access to its parent's environment (within the abstraction that Bash can provide).

One alternative is for the subshell to write assignment statements to a temporary file for its parent to read:
`a=3`
`(echo 'a=4' > tmp)`
`. tmp`
`rm tmp`
`echo "$a"`
#### 4. Create script that redirects system date and disk utilization in a file with systemd.
