Take control of Linux Flashcards

1
Q

$ sudo [command]

A

For every command that requires root permission.

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

$ sudo su

A

Same as putting sudo in front of a command.

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

$ pwd

A

print working directory , or “where I am”.

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

$ ls

A

List the content of the current directory.

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

$ ls -a

A

List the content of the working directory, including hidden files.

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

$ cd [directory]

A

Change the current directory.

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

$ cd ..

A

Move to parent directory of current directory.

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

$ mkdir [directory]

A

Create a directory.

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

$ cp [source] [destination]

A

Copy and paste a file.

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

$ mv [source] [destination]

A

Move (rename) a file or directory.

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

$ cp -r [source] [destination]

A

$ Copy and paste a directory.

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

$ rm [file]

A

Remove file.

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

$ rm -rf [directory]

A

Recursive removal of directory.

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

$ cat [file]

A

cat means “concatenate”. View the content of a file.

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

$ cat [file] [file]

A

View the content of multiple files.

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

$ cat >[file]

A

Create a file.

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

$ cat [file1] > [file2]

A

Redirect standard output of file1 into new file2. Existing content of file2 will be overwritten.

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

$ cat [file1] >> [file2]

A

Append standard output of file1 into file2, without overwritting.

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

$ cat < [file]

A

Output of the file will be shown in terminal.

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

$ cd (or $ cd ~)

A

Navigate to home directory.

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

$ cd /

A

Navigate to root directory.

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

$ cd -

A

Navigate to previous directory.

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

$ ln -s [target] [source]

A

Create a symbolic link at [source] referencing the original file [target].

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

What is a symbolic link?

A

Also called soft link or symlink, it can be thought of as a shortcut to a file or directory.

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

$ stat [file_or_dir]

A

Display file status.

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

$ stat -c %a [file_or_dir]

A

Display access rights in octal (numeric) of file.

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

What are the different classes of users for file access?

A

owner: #– or -###—— group: -#- or —-###— other: –# or ——-###

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

What are the different permissions for file access?

A

read: 4 or r write: 2 or w execute: 1 or x

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

How to change permissions with letters? Example: add other & execute.

A

+: add permission. -: remove permission. =: set permission. Example: chmod o+wx [file]

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

$ ls -l [file]

A

Display access rights of file in letters.

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

How to change permissions with numbers? Example: -rw-rw-rwx.

A

Add the value of each permissions you want to set for each group. Example: owner: 4+2=6 group: 4+2=6 other: 4+2+1=7 $ chmod 667 [file]

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

$ sudo chown [username]:[group] [directory]

A

Change the ownership of both the user and the group for this directory.

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

$ sudo chown -R [username]:[group]

A

Change the ownership of both the user and the group of all files and directories within this directory.

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

How to make root the only user able to write the file?

A

$ sudo chown root:root [file] $ sudo chmod 700 [file]

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

What is the command apt-get?

A

apt is one of the tools used to interact with the APT (Advanced Packaging Tool) to install, remove and manage packages in Debian-based Linux distributions.

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

What is the command apt?

A

apt consists of some of the most widely used features from apt-get and apt-cache leaving aside seldom used features. It is considered more secure to use apt.

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

$ tail [file]

A

View the last 20 lines of a file.

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

$ tail -n [number]

A

View an arbitrary number of the most recent files.

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

$ tail -f [file]

A

Follow a file as more lines get appended to it.

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

$ apt-get update

A

Update the list of available packages and their versions, but do not install or upgrade any package.

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

$ apt-get upgrade

A

Actually installs newver versions of the packages. To be used after having updated the package lists.

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

$ apt-get install [package]

A

Install a new package.

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

$ apt-get check

A

Does an update of the package lists and checks for broken dependencies.

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

$ apt-get autoclean

A

Removes .deb files for packages that are no longer install on the system.

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

$ apt-get clean

A

Remove all packages from the package cache (more powerfull than apt-get autoclean).

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

$ apt-get remove [package]

A

Removes an install package, leaving the configuration file intact.

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

$ apt-get purge [package]

A

Completely removes an installed package and the associated configuration files.

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

$ apt-get autoremove

A

Remove packages that were installed by other packages and are no longer needed.

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

$ apt-cache search [search_term]

A

Lists packages whose name or description matches search term.

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

$ apt-cache show [package]

A

Shows description of package as well as version, size, dependencies and conflicts.

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

$ man apt-get (or apt-cache or dpkg)

A

Shows the manual associated with these tools.

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

With apt-get, for what the -s flag is used for?

A

To simulate an action. Example: apt-get -s install [package] will simulate installing the package, showing what will be installed and configured.

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

What is Zsh?

A

An alternative shell to the traditional Bash (Bourne Again Shell) with syntax highlighting, tools integration and plugins.

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

chsh -s /bin/zsh

A

Make Zsh the default shell.

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

What are two popular plugin managers for Zsh?

A

oh-my-zsh and zprezto.

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

What is tmux? What is it usefol for?

A

tmux is a terminal multiplexer, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from a command-line interface, and for separating programs from the Unix shell that started the program.

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

$ tmux

A

Start a tmux session.

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

How are commands triggered in tmux?

A

By a prefix key (C-b or Ctrl+b) followed by a command key.

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

C-b %

A

Split a tmux pane into a left pane and right pane.

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

C-b “

A

Split a tmux pane into a top pane and bottom pane.

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

C-b

A

Navigate panes in tmux.

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

$ exit or C-d

A

Close a pane in tmux.

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

C-b c

A

Create a new window in tmux, similar to creating a new virtual desktop.

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

C-b p / C-b n

A

Navigate to previous or to next window in tmux.

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

C-b

A

Navigate to a specific window in tmux, given its number.

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

C-b d

A

Detach current session in tmux and leave it running in the background for later reuse.

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

$ tmux ls

A

Figure out which sessions are running in the background, possibly to attach to them.

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

$ tmux attach -t [number]

A

Attach to a tmux session with a given number.

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

$ tmux new -s [name]

A

Start a tmux session with a given name.

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

$ tmux rename-session -t [old number or name] [name]

A

Rename a tmux session.

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

What does it mean that the text editor Vim is modal?

A

The actions we can perform in Vim depends of the mode we are using.

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

What are the 3 modes of Vim?

A

Insertion mode: similar to a classic editor, the text typed is inserted where the cursor is. Normal/Command mode: enable text navigation, copy-pastes, deletions… Visual mode: enable text selections.

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

What is a Vim plugin managers and some of its absolutely necessary plugins?

A

Plugin manager: Vundle. Plugins: NERDtree (file hierarchy), ack (search for a character chain), CtrlP (fuzzy search of files), Surround (add/modify quotes, double quotes, parentheses…)

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

$ vim [file]

A

Edit a file or create a new file if it does not exist yet.

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

In Vim: i

A

Enter insertion mode before the current position.

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

In Vim (insertion): ESC

A

Enter command mode.

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

In Vim: :wq

A

Save the file (Write) and then quit Vim.

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

In Vim: :q!

A

Quit and discard changes.

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

How to enter a command in Vim?

A

Hit the semicolon key :.

80
Q

In Vim (command): :q

A

Quit without discard changes.

81
Q

How to move within Vim?

A

Either use the arrow keys, or use the following keys: h: left j: down k: up l: right

82
Q

In Vim (command): x

A

Delete a character from command mode.

83
Q

In Vim: a

A

Enter insertion mode after the current position.

84
Q

In Vim: o

A

Enter insertion mode below the line.

85
Q

In Vim: O

A

Enter insertion mode above the line.

86
Q

What is the format of a command in Vim?

A

[operator][number][motion]

87
Q

In Vim: d

A

Delete operator (acts like a “cut” command).

88
Q

In Vim: c

A

Change operator.

89
Q

In Vim: r

A

Replace operator.

90
Q

What is the w motion in Vim?

A

Until the start of the next word, excluding its first character.

91
Q

What is the e motion in Vim?

A

To the end of the current word, including the last character.

92
Q

What is the $ motion in Vim?

A

To the end of the line, including the last character.

93
Q

What are counts in Vim?

A

Counts are optional and let you put a multiplier to your command and motion.

94
Q

In Vim: dw

A

Delete a word. If in the middle, it will stop where the word ends.

95
Q

In Vim: d$

A

Delete to end of line.

96
Q

In Vim: u

A

Undo command.

97
Q

In Vim: G

A

Move to the bottom of a file.

98
Q

In Vim: gg

A

Move to the start of a file.

99
Q

In Vim: Ctrl+g

A

View current cursor position.

100
Q

In Vim: +G

A

Jump to a specific line.

101
Q

In Vim: /

A

Search a page after the cursor position.

102
Q

In Vim: ?

A

Search a page before the cursor position.

103
Q

In Vim: n

A

Navigate to the next search match.

104
Q

In Vim: N

A

Navigate to the previous search match.

105
Q

In Vim: %

A

For ( and ), [and], { and }, move the cursor to the matching counter part.

106
Q

In Vim: :s//

A

Find and replace the first instance of word1 by word2.

107
Q

In Vim: :s///g

A

Find and replace all instances of word1 by word2.

108
Q

In Vim: dd

A

Cut a line.

109
Q

In Vim: diw

A

Cut a word.

110
Q

In Vim: daw

A

Cut a word and all spaces after it. If there are no spaces after it, delete the spaces before it.

111
Q

In Vim: v

A

Select characters.

112
Q

In Vim: V

A

Select whole lines.

113
Q

In Vim: Ctrl-v

A

Select rectangular blocks.

114
Q

In Vim: y

A

Copy (or “yank”) operator.

115
Q

In Vim: yy

A

Copy whole line.

116
Q

In Vim: p

A

Paste after cursor operator

117
Q

In Vim: P

A

Paste before cursor operator.

118
Q

In Vim: yiw

A

Copy current word excluding surrounding spaces.

119
Q

In Vim: yaw

A

Copy current word including leading or trailing spaces.

120
Q

In Vim: I

A

Enter insertion mode at the start of the current line.

121
Q

In Vim: a

A

Enter insertion mode after the current position.

122
Q

In Vim: A

A

Enter insertion mode at the end of current line.

123
Q

In Vim: ciw

A

Cut current word excluding surrounding spaces and enter insertion mode at the same position.

124
Q

In Vim: caw

A

Cut current word including leading or trailing spaces and enter insertion mode at the same position.

125
Q

$ git init

A

Initialize a git repository.

126
Q

$ git status

A

See current state of project.

127
Q

$ git add [file]

A

Add a file to the staging area to start tracking changes.

128
Q

$ git commit -m “[message]”

A

Store the staged changes in the repository.

129
Q

$ git log

A

Display the history of commits.

130
Q

$ git remote add [remote_name] [repository_URL]

A

Push local repo to the GitHub server where a new empty GitHub repository was created beforehand.

131
Q

$ git push -u [remote_name] master

A

Push the local repository to the [remote_name] repo with default local branch name being master. The -u tells Git to remember the parameters for the next time.

132
Q

$ git pull [remote_name] master

A

Pull down any new changes to the master branch.

133
Q

$ git diff HEAD

A

After pulling new changes, check what is different from our last commit using the HEAD pointer.

134
Q

$ git diff –staged

A

Check what is different from our last commit compared to the changes we just staged.

135
Q

$ git reset [file]

A

Remove file from staging area.

136
Q

$ git checkout – [file]

A

Change the file back to how it was at the last commit.

137
Q

What is a repository?

A

A directory where Git has been initialized to start version controlling the files.

138
Q

What are the different file statuses in Git?

A

Staged: files are ready to be commited. Unstaged: files with changes and not prepared to be commited. Untracked: not tracked by Git yet. Usually new files. Deleted: file has been deleted and is waiting to be removed from Git.

139
Q

$ git add -A .

A

Add all files to staged area. The dot stands for current directory. The -A ensures even file deletions are included.

140
Q

What is a git commit?

A

A snapshot of a repository to which we can come back if we ever need to.

141
Q

$ git log –summary

A

See more information for each commit, such as where new files were added for the first time or where files were deleted.

142
Q

What is the use of Git hooks for a webserver?

A

Upload directly to a webserver whenever you push to the master remote instead of having to upload the site with an FTP client.

143
Q

$ git stash

A

Sometimes when you oull, you may have changes you do not want to commit. Before you pull, stash your changes.

144
Q

$ git stash apply

A

Re-apply stashed changes after you pull.

145
Q

$ In git, where does the HEAD pointer points to and what does it hold?

A

By default HEAD points to the most recent commit. HEAD holds your position within all the different commits.

146
Q

What does the – mean in Git?

A

It promises the command line that there are no more options after the –. Example: useful to checkout (revert) a file instead of switching to a branch with the same name.

147
Q

$ git branch [name]

A

Create a copy (branch) of the repo, to later be merged back into the main master branch.

148
Q

$ git branch

A

List all the branches of our repo.

149
Q

$ git checkout [branch_name]

A

Switch to a different branch.

150
Q

$ git rm [file]

A

Remove files from the system as well as stage the removal of the files.

151
Q

How to use wildcards in Git?

A

Wildcards need to be enclosed by quotes. Example: $ git rm ‘*.txt’

152
Q

What should we do in got when working on multiple features?

A

Separate the code base into multiple branches to work on and commit separately.

153
Q

$ git checkout -b [name]

A

Checkout and create a branch at the same time.

154
Q

$ git rm -r [directory]

A

Recursively remove all folder and files from the given directory in our repository.

155
Q

$ git commit -am “[message]”

A

Auto remove deleted files with the commit, without having to $ git rm the files (simply $ rm them).

156
Q

What is a pull request?

A

A pull request allows the boss of the project to look through the changes and make comments before deciding to merge in the change.

157
Q

$ git merge [branch_name]

A

Back on the master branch, merge the changes of the other branch into the master branch. Note: you have to $ git checkout master first.

158
Q

$ git branch -d [branch_name]

A

Delete a branch after a merge. The -d flag won’t let you delete something that has not been merged.

159
Q

$ git branch -d -f [branch_name] or $ git branch -D [branch_name]

A

Force delete a branch even if it was not merged, for example if you do not want a feature anymore.

160
Q

What are the seven rules to a great commit message?

A

1) Seperate subject from body with a blank line. 2) Limit the subject line to 50 characters. 3) Capitalize the subject line. 4) Do not end the subject line with a period. 5) Use the imperative mood in the subject line. 6) Wrap the body at 72 characters. 7) Use the body to explain what is the problem and why you are making this change (and not how)

161
Q

Redirect result into a file.

A

$ cmd > file (overwriting) $ cmd >> file (appending)

162
Q

Redirect errors.

A

$ cmd 2> file (overwriting) $ cmd 2>> file (appending) $ cmd 2>&1 file (redirect both standard output and error to same file) $ cmd > file_standard 2> file_error (redirect standard output and error to different files)

163
Q

Read from file or keyboard.

A

$ cmd < file (read from file) $ cmd << [stop word] (read from keyboard input until stop word is given)

164
Q

Chain commands.

A

$ cmd1 | cmd2 | cmd3 (connect the output of one command to the input of another)

165
Q

When should one compile a program from the source and what does it involve?

A

When a program is not listed in apt-get (usually not well known programs or programs in development). One should download the source, extract a compressed archive and compile manually to create an executable (equivalent of Windows .exe optimized for the user’s computer).

166
Q

What is a .deb file?

A

Equivalent of a Windows installation program for Debian distributions (e.g. Ubuntu). A user can double-click on it and it will launch. The .deb has to correspond to the computer (32/64bits) and dependencies must already be installed.

167
Q

Typical compilation process of a Linux executable.

A
  1. Install the package “build-essential” ==> compilation tool
  2. Find the source code of the program.
  3. Download and uncompress the compressed archive.
  4. Find the ./configure directory in the source code and execute it: $ ./configure ==> program checking all dependencies
  5. If there is an error, usually you have to find and install a missing dependency. Then rerun ./configure.
  6. Launch the compilation: $ make
  7. Install the executable: $ sudo make install
  8. To uninstall the program, run $ sudo make uninstall
168
Q

How to clone an existing git repository?

A

$ git clone [repo_URL]

169
Q

$ kill -15 [process_id] or $ kill [process_id]

A

Sends a SIGTERM signal to an application. The shutdown behaviour is up to the particular application:

  • The process may stop imemdiately.
  • The process may stop after a short delay after cleaning up resources.
  • The process may keep running indefinitely.
170
Q

$ kill -9 [process_id]

A

Sends a SIGKILL signal to init. At that point, init will stop the process. The process never gets the opportunity to catch the signal and act on it.

171
Q

Is a SIGKILL signal always enough to kill a process?

A
  • No, the kernel may not be able to successfully kill the process if the process is waiting for network or disk I/O.
  • Zombie processes and processes caught in an uninterruptible sleep cannot be stopped by the kernel either.
  • A reboot is required to clear those processes from the system.
172
Q

What is the ps command for?

A

The ps (process status) command is used to provide information about the currently running processes, including their process identification numbers (PIDs).

173
Q

What is the difference between kill and pkill?

A

pkill allows you to kill a process by its name, user name, group name, terminal, UID, EUID, and GID.

174
Q

$ sudo killall -9 [process]

A

Kill all processes: childs and parent.

175
Q

$ ps -aux | less

A
  • -a flag: List the processes of all users on the system rather than just those of the current user, with the exception of group leaders and processes not associated with a terminal.
  • -u flag: provide detailed information about each process.
  • -x flag: adds to thel ist processes that have no controlling terminal, such as daemons.
  • | less: piped the output of the command to the less command.
176
Q

What is a daemon?

A

Program launched during booting and which run unobtrusively in the background until they are activated by a particular event or condition.

177
Q

% ps -aux | grep [process]

A

Limit the processes shown to those belonging to any given user by piping the output through grep, a filter used for searching text.

178
Q

What is a Bash script?

A

A Bash script is a plain text file which contains a series of commands. These commands are a mixture of commands we would normally type ourselves on the command line. it is a convention that Bash scripts have an extension of .sh.

179
Q

What is a program?

A

A program is a blob of binary data consisting of a series of instructions for the CPU and possibly other resources (images, etc…) organized into a package and typically stored on a hard disk.

180
Q

What is a process?

A

A running copy / instance of a program: instructions and resources of the program are copied from hard disk into working memory (or RAM). A bit of space in RAM is also allocated to store variables and a few flags used so that the OS can manage and tack the process.

181
Q

How to execute a Bash script?

A

$ chmod 755 myscript.sh (by default the permissions are not set)

$ ./myscript.sh

182
Q

What is the Shebang (#!) on the very first line of a Bash script?

A

Following the Shebang is the absolute path to the interpreter (or program) used to run (or interpret) the rest of the lines. For Bash scripts it is /bin/bash.

183
Q

What care should be taken with Bash scripts?

A

The presence or absence of a space can be the difference between th command working or not.

184
Q

How to set a variable in a Bash script?

A
  • variable = “value” ==> allows substitution (that is include variables within the setting of the value).
  • variable = ‘value’ ==> treats every character literally.
  • variable = $( some command… ) ==> save the output of a command as a value.
185
Q

How to export a Bash variable to a child process?

A

export var

./script2.sh

186
Q

How to ask the user for input when executing a Bash script?

A

Use read:

  • read var1 [var2] [var3…] ==>
    • If there are more items than variable names then the remaining items will be added to the last variable name.
    • if there are less items than variable names then the remaining variable names will be set to blank or null.
  • read -p ‘prompt_name’ var ==> allows you to specify a prompt
  • read -sp ‘prompt_name’ var ==> specify a prompt and make the input silent (do not display it).
187
Q

How to make a Bash script able to process data piped to it?

A

Read the relevant file:

  • cat /dev/stdin | some processing…

Then in the command line:

  • $ cat | myscript.sh
188
Q

When methods for getting user input should you use?

A
  • Favour command lines if the script is called by other scripts or processes (e.g. CRON) and so that the data is stored in the user’s command history.
  • Favour reading user input during script execution for login credentials and similar data.
  • if all the script is doing is processing data in a certain way then it is best to work with STDIN.
  • A combination of the previous can be used.
189
Q

How to control flow in Bash scripts?

A
  • if [] then elif [] then else fi
  • Case: case in ) ;; ) ;; esac
  • Boolean operators: && (AND) and || (OR)
190
Q

How to get a variable in a Bash script?

A

Use ${var}

191
Q

How to do arithmetic in Bash scripts?

A
  • let ==> make a vriable equal to an expression.
  • expr ==> print out the result of the expression.
  • $(( expression )) ==> return the result of an expression.
  • ${#var} ==> return the length of the variable.
192
Q

What are the types of loops in Bash scripts?

A
  • While loops: while [] do done
  • Until loops: until [] do done ==> execute the commands until the test becomes true.
  • For loops: for var in do done
193
Q

How to create a function in a Bash script?

A

function_name () {

}

Notes:

  • The parentheses are only a decoration: never pass anything to it.
  • The function definition must appear before any calls to it.
194
Q

How to pass arguments to a function in a Bash script?

A

function

195
Q
A