Advanced terminal Flashcards Preview

Phase_2 > Advanced terminal > Flashcards

Flashcards in Advanced terminal Deck (28)
Loading flashcards...

What are package managers?

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.


What are the differences between Homebrew, Yarn and npm?

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 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.


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

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 would you install Yarn?

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


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

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


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

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).


Why use Homebrew over npm?

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).


What is cURL and wget?

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 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 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.


What are brew services and give examples of them.

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


What is the bin directory and where is it found?

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 would you create a custom bin folder?

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 --> $


Explain piping

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

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.


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

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 >> 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

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

I (pipe)
> (redirect)


What do these do:
$ ^Z
$ ^R
$ ^C
$ fg
$ Bg
$ ^T
$ ^K

^ (ctrl)

$ ^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


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

$ 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).
$ 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 would you call a shell script?

$ sh


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.

echo Hello World!

echo "hello, $USER. I wish to list some files of yours"
echo "listing files in the current directory, $PWD"
ls # list files

X = "" --> empty string
if[ -n $X ]; --> -n tests to see if the argument is not empty

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.";;

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


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

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.


Can you give examples of some environment variables?

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


What is the shell PATH?

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.


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

you need to add it to your ~ /.profile file.
export PATH=$PATH:/path/to/dir


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

yourcommand '&>' filename
This redirects both stdout and stderr to filename


How does one use UNIX command flags and arguments?

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!


How would you describe UNIX file permissions?

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.


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

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


How would you set file permissions?

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.


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

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


How to write an executable script in Bash

Three very simple things:
( 1. ) Use proper extension (.sh), we might name our file:
( 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