Bash scripting Flashcards

1
Q

What are the layers of an OS?

A

The Outer “Shell”

Shell - Commands - apps - scripts

Network service - libraries - file systems - frameworks

Sys.calls - device drivers

Kernel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a shell?

A

The outer layerof an OS, where users interact through a graphics-based or text based interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is bash?

A

A command line shell

A program that executes commands on the user’s behalf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What user has UID 0?

A

Root (superuser)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does # and $ mean in shell?

A

When the shell displays a $, it means it is waiting for the user to pass a command.

means the user that is running the shell, is the root user (superuser with administrative rights)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the “date” command?

A

Displays the current date

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the “pwd” command?

A

Print working directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the “ls” and “ls -l” commands

A

list, and long listing

Prints content of working directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the “echo” command?

A

Prints a string or value of a variable to stdout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the first line of a bash script?

A

!/bin/bash

Shebang: Acombination of bash (#) and bang (!), followed by the bash shell path

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does shebang do?

A

Tells the shell to execute the script via bash shell

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is shebang?

A

An absolute path to the bash interpreter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What command can be used to find the bash shell path?

A

which bash

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between !¤$ and $_ in the following commands:

mkdir folder && cd !$
mkdir folder && cd $_

A

!$ gives the last argument of the previous command in the shell history, in this case, the command that was passed to the shell before the mkdir command.

$_ gives the last argument of the previous command, in this case, the argument of mkdir

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the comman “read var_name” do?

A

Reads input and stores the input value in the following variable, in this case var_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How are variables assigned in bash?

A
  1. Assign value directly: country=Norway
  2. Command substitution:
    same_country=$country
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is command substitution?

A

Get a value based on the output obtained from a program or command.

$ is required to access an existing variable’s value

18
Q

How can variables be accessed in bash?

A

Appending $ to the variable name: $country_name

19
Q

What does $1, $2, and so on mean in bash?

A

The first, second, and so on, argument that was passed to the function.

20
Q

What command can you use to redirect output from a command and writing it to a file.

A

ls > output.txt

21
Q

What command can you use to append output from a command to a file.

A

ls&raquo_space; output_log.txt

22
Q

What is the touch command?

A

Creates a new file, if name does not exist

23
Q

What command removes a file or directory?

A

rm

24
Q

What command copies a file or directory?

A

cp

25
Q

What command moves or renames a file or directory?

A

mv

26
Q

What is the grep command?

A

Searches for a pattern in a file

27
Q

What is the df command?

A

Displays the amount of disk space available

28
Q

What is the history command?

A

Displays a list of previously executed commands

29
Q

What is the ps command?

A

Displays information about running processes

30
Q

What is the OR operator in bash?

A

-o

31
Q

What is the AND operator in bash?

A

-a

32
Q

What is Cron?

A

A utility used for job scheduling.
Can be configured to set up automated jobs to run daily, weekly, monthly, or specific time basis.

33
Q

What is crontab?

A

Used to add and edit cron jobs

34
Q

What command is used to list already scheduled scripts for a user?

A

crontab -l

35
Q

What command is used to add and edit the cron?

A

crontab -e

36
Q

How is debug enabled in bash?

A

Add “set -x” at the beginning of a script (after shebang #!)

37
Q

What does set -x do in a script?

A

Enable debug, causing bash to print each command that it executes to the terminal, preceded by a + sign

38
Q

How can you check the exit code of the most recent command?

A

Print the $? variable

e.g.:
echo $?

39
Q

What exit code does a command have on success?

A

0

40
Q

What does set -e do?

A

When added to the beginning of a script, when a command in the script fails, bash will exit immediately.