Advanced terminal Flashcards

1
Q

What are package managers?

A

A Package management system is a collection of software tools that automate the process of installing, upgrading, configuring and removing computer programs for a computers OS and/or web dev environment in a consistent manner.

Package managers such as npm or Yarn basically help speed up the process of downloading and using sourcecode (their package format) from the web in your app development.

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

What are the differences between Homebrew, Yarn and npm?

A

Homebrew is a package manager for your OS, Yarn and npm are managers for source code useful during software development. Homebrew can be used to download Node and Yarn. Node.js comes with its own installer on Nodejs.org. Homebrew is also written in the Ruby version that comes installed in your mac. Homebrew also is wired up to git so when you choose to install any new OS feature you also connect to git and run your ruby code.

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

What was the downloading process before package managers? With package managers?

A

Before package managers, if you wanted to download code from a company you would download it like you would a compressed zipped file (in this case the file would be an archive of code), you would need to unzip that file (or unarchive it), you would then have to run a bunch of cryptic commands to configure the code.

Package managers do all these things for you plus it uploads all dependencies needed to run the code successfully (if you don’t already have them) and it looks to see if there are any updated versions of your desired package automatically.

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

How would you install Yarn?

A

$ brew install yarn -g (npm would NOT be the package manager to use)

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

What are some commands for Homebrew useful in maintaining the package manager?

A

$ brew update, $ brew doctor, $ brew upgrade. $ brew outdated (returns programs that are outdated on ur machine)

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

When you run update what’s the one specific technical thing that happens?

A

In homebrew we deal with instructions to install called formula or formulae. Running brew upgrade mongodb would trigger homebrew finding and installing the formula needed to upgrade mongodb. Whereas homebrew update would have homebrew update itself. When you update your Homebrew, it searches for updates and downloads all new (ruby written) formulae (instructions on how to install and where to find the necessary files and dependencies).

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

Why use Homebrew over npm?

A

Homebrew helps you install OS software that Apple computers don’t come installed with. Brew is not geared towards software development and software packages (unless of course you’re downloading another package manager).

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

What is cURL and wget?

A

cURL and wget are tools available with Homebrew to install software from the web (you use cURL when writing the rubbish ruby code to install Yarn for example). Both can also help in downloading entire websites by typing the urls.

The GNU Linux wget command, formerly named Geturl, is used to download non-interactive information from web servers such as web sites info. It supports HTTP, HTTPS, FTP, and reading of HTTP proxies. You might type in for example wget http://website.com/files/file.zip. It does NOT come installed in macs.

macs comes preinstalled with cURL. IOS does not come preinstalled with curl. curl is more interactive than wget in that it lets us read information from web servers but also let’s us write to them as well. It allows for 10+ different protocols. It also has a ton of features like authentication, cookie mgt., HTTP post, saving files from the web and testing RESTful APIs!!!

Running $ curl google.com would return the entire html script from the google website.

If you ran $ curl http://localhost/:3000 you would have returned to your cli whatever would otherwise be returned on a browser, you’d also get a response status code, a Content-Type (ex: application/json), Content-length (95), Server (server name), Date (date type).

curl -d “first=Mike&last=Yolo” http://localhost:3000/methods would actually run a POST method.

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

What are brew services and give examples of them.

A

Brew services are tools used in integrating the Homebrew formulas downloaded during software updates to MacOs, in the end helping use the formulas to download and configure code.

Brew services are tools built inside Homebrew useful in integrating Homebrew formulae with macOS. Some examples of these services are:

  • $ brew install mysql
  • $ brew services start mysql
  • $ brew services run mysql
  • $ brew services stop mysql
  • $ brew services restart mysql
  • $ brew services list
  • $ brew services run|start|stop|restart -all
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the bin directory and where is it found?

A

The bin directory stores applications, many of them essential binary code necessary to run some of your important utilities like your terminal. It is part of the filesystem hierarchy standard created by Linux for Unix-styled software to follow.

Bin is short for binary, this folder contains essential binary files or binary appications or system applications such as shells (like bash) and commonly used commands like ‘cp’,’mv’,’rm’,’cat’. it’s where you can expect to find apps on your OS.

To find the bin directory on your mac open your finder application from the dock and in the mac menu bar click on ‘Go’, then ‘go to folder’, lastly type in what you want to go to like ‘/usr/local/bin’.

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

How would you create a custom bin folder?

A

Easy. Create a folder as you would otherwise (mkdir) then make sure to include the following code in your bin file: PATH=$PATH:$HOME/bin, this will ensure your bin folder is in your $PATH variable. Then to use it simply add working scripts into the bin folder. To run the scripts simply call them from the cli like so –> $ yourShellScript.sh

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

Explain piping

A

To understand piping you need to understand what the Process object is. The process object is a global object within Node (meaning it’s always available to Node apps without using the require( ) method) that allows users to control the flow of data in a current running process.

Process listens for several events that fire during your process such as event warning, event unhandledRejection, uncaughtException, etc. These events are basically things that your process can be asked to listen to and responde with functions to.

Every process/running program has 3 pipes, each pipe has one of three data streams: stdin(0), stdout(1), stderr(2), all of which handle data flow. process.on( ‘warning’, function )

Then you have methods that sit on the Process object, methods like argv, abort, chdir, config, env, stdin, stderr, stdout!!! You’d call these using:

process. stdin.on( ‘readable’, function )
process. stdin( ‘end’, function )
process. stdin.setEncoding( ‘utf8’ )
process. stdout.pipe( process.stdout )
process. env –> returns an object w/ env info about a process
process. argv(2)

These pipes have 3 data streams: STDIN(0), STDOUT(1) STDERR(2). Think of STDIN as text we pass into a file and STDOUT as text we pass into the terminal or shell. When the program errors out it goes through the STDERR pipe.

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

You’re familiar with piping (using data streams) and redirecting of data give examples of how you would do this.

A

If you were to use pipe or redirect you should know that pipe and redirect are really just ways to manage I/O system inputs and outputs. If we want to redirect the output of a certain process to a file we might type:

$ ps -ax > myFile.txt

To append output of a process to a file:

$ ps -ax&raquo_space; myFile.txt

To pass output of a process to something that does some function on that output we’d use piping:

$ ps -ax | grep Finder

http://www.westwind.com/reference/os-x/commandline/pipes.html

$ cat ace - print file contents to the shell
$ echo ‘start of something great’ > myFile.txt
$ echo ‘and more awesome’&raquo_space; myFile.txt
$ mail -s ‘I…have a dream’ mike < speech.txt
$ ps aux | less

I (pipe)
> (redirect)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
SHELL COMMANDS.
What do these do:
$ ^Z 
$ ^R
$ ^C
$ fg
$ Bg
$ ^T
$ ^K

^ (ctrl)

A

$ ^Z –> ( ctrl z ) suspends a program, never to be brought up again.
$ ^R –> ( ctrl r ) “search terminal history.”
$ ^C –> ( ctrl c ) the nice version of suspending a process, this is a polite kill. Entered twice.
$ fg –> foreground. This recovers the work if you suspend a program. Brings program into foreground.
$ Bg –> background. This puts the process into the bg
$ ^T –> open new tab.
$ ^K –> clear page

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
SHELL COMMANDS
What do these do
$ tail /path/to/file.log
$ tail -f /path/to/file.log
$ tail -20 /path/to/file.log
$ grep 'find me in the file' rolodex.txt
$ man cd
$ kill
$ sudo kill | sudo open | sudo -u mike rm ~/mike/Documents/deleteme.txt
$ wc fileName.txt ( think wordcount )
$ du fileName.txt
$ chmod
$ ps
$ pdcopy
$ pbpaste
$ file fileName.txt
A

$ tail /path/to/file.log –> this prints to the screen the last 10 lines of the file.
$ tail -f /path/to/file.log –> this prints to the screen the last 10 lines of the file and anything added.
$ tail -20 /path/to/file.log –> this prints to the screen the last 20 lines using the STDOUT data stream.
$ grep ‘find me in the file’ rolodex.txt –> what do you think grep does?
$ man cd –> easy, think on it.
$ kill –> kills processes and for use on system processes you’d need to prepend sudo to it.
$ sudo kill | sudo open | sudo -u mike rm ~/mike/Documents/deleteme.txt –> system changes! Sudo usually needs to confirm the user password and username though.
$ wc fileName.txt –> think word count, this returns information about the file to the screen. It displays the number of newlines, words and bytes (-c), words (-w), and lines (-l) inside the file, and even multibyte characters (-m).
$ du fileName.txt –> returns disk usage info.
$ chmod –> change mode is used to change permissions or alter special mode flags on files and directories. Usage: chmod o-w file1 (r=readable, w=writable, x=executable, d=directory, -=if a file). https://www.seas.upenn.edu/cets/answers/chmod.html
$ ps –> process status command is used to provide info about the current running process, reminiscent of brew services list.
$ pdcopy –> clipboard copy.
$ pbpaste –> clipboard paste.
$ file fileName.txt –> determine the file type (usually unneccessary since the extension usually tells the file type).

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

How would you call a shell script?

A

$ sh myShellScript.sh

17
Q

bash is written for small repetitive jobs or light task automation like doing your backups. Bash is a Turing complete langage, but it is not intended to build software. Give an exampel of a shell script you would write.

A

1.)
#!/bin/bash
echo Hello World!

2.)
#!/bin/bash
echo “hello, $USER. I wish to list some files of yours”
echo “listing files in the current directory, $PWD”
ls # list files

3)
X = “” –> empty string
if[ -n $X ]; –> -n tests to see if the argument is not empty
fi

4)
while true; do
read -p “Do u wish to install program?” yn
select yn in “Y” “N”; do –> one way of asking user same question.
caser $yn in
[Yy]* ) make install; break;;
[Nn]* ) exit;;
* ) echo “Please answer yes or no.”;;
esac
done

$PWD and $USER are considered variables (I think of them as placeholders, but variables are a better descriptive term).

18
Q

What is a shell variable in linux, and an environment variable?

http://sc.tamu.edu/help/general/unix/vars.html

A

In linux every process runs in a specific environment. Every environment consists of a single table of environmental variables, each with an assigned value.

Whenever you login certain login files are executed. These files initialize the single table located in the environment with all its environmental variables.

When this file passes the process to the shell the table is accessible to the shell. And if a child process is called (maybe by a parent process) the child process is given a copy of the parent processes table.

The shell maintains a set of internal variables known as shell variables, these variables cause the shell to work in a particular way. Shell variables are local to the shell in which they are defined and are not available to parent or child shells.

19
Q

Can you give examples of some environment variables?

https://www.youtube.com/watch?v=pjh9rU9h22Q

A
PATH - ...
ENV - displays all environment variables
USER - username
HOME - default path to users home directory
UID - users unique id
SHELL - shell being used by user
TERM - default terminal emulator
EDITOR - path to your preferred text editor
20
Q

What is the shell PATH?

A

PATH is an environment variable where you specify in which directory specific files are located.

$PATH is a hidden folder on your machine. It contains a list of directories which hold executable files.

$ echo $PATH –> brings up the location of the hidden $PATH directory.

This variable contains a colon separated list of directories in which your system looks for executable files.

21
Q

How does one permanently modify the shells $PATH from the terminal?

https://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux

A

you need to add it to your ~ /.profile file.

export PATH=$PATH:/path/to/dir

22
Q

How does one redirect STDOUT to a file, in the terminal?

A

yourcommand ‘&>’ filename
This redirects both stdout and stderr to filename

https://stackoverflow.com/questions/7526971/how-to-redirect-both-stdout-and-stderr-to-a-file

23
Q

How does one use UNIX command flags and arguments?

A

when writing unix commands anything that follows the name of the command can be called one of 3 things:
an option
a parameter
an argument (arg or argv, holds true in the C language)

example: $ ls -a

An option tells the terminal how to act. A flag is a type of option which is false by default and like other options does something specific.

A great example of the use of flags and arguments would be utilizing the | or > (piping or redirecting) of process I/O. Sure we could decide just to pipe a process output to a file but adding an option allows more control over how we want that information to be sent!

24
Q

How would you describe UNIX file permissions?

https://www.tutorialspoint.com/unix/unix-file-permission.htm

A

In the Unix OS every file has an owner and/or group of owners, each level of ownership (owner, group and other/world) can be allowed certain permissions. A files permissions are the first line of defense in the security of a linux system, with the basic building blocks of each permission are read, write and execute.

To change some permissions use the change mode or chmod command.

25
Q

Can you get a file’s permissions in the terminal? How?

A

To see the permissions of a file run the following:
ls -l /path/to/file

Of course being able to read what the permissions actually imply will take some work.

rw: read-write permission
x: execute permission
d: directory permission

26
Q

How would you set file permissions?

A

Find the file you want to set permissions for and right click the file to get it’s ‘Get info’ details. Click the padlock at the bottom of the Get info interface and you’ll be able to set the Read only and Read permissions at the bottom.

27
Q

How does one add a custom bin directory to a shell config?

A

Edit .bashrc in your home directory and add the following line: $ export PATH=”/path/to/dir:$PATH”
Then you will need to source your .bashrc or logout/login (or restart the terminal) for the changes to take effect. To source your .bashrc, simply type: source ~/.bashrc

https://askubuntu.com/questions/60218/how-to-add-a-directory-to-the-path

28
Q

How to write an executable script in Bash

A

https://askubuntu.com/questions/521919/making-a-bash-script-executable-programatically

Three very simple things:
( 1. ) Use proper extension (.sh), we might name our file: myscript.sh.
( 2. ) Reference the script interpreter at the beginning of a script (#!/bin/bash).
( 3. ) Include whatever you want inside your shell file (shell scripts are typically used for light task automation like doing backups)
( 3. ) Set permissions to make executable. chmod +x myscript.sh