<commit_before>Bash, Screen, Vi, and SSH
=========================

Goals
-----

* Get comfortable working at the command line
* Gain experience with common Unix command line tools
* Understand the typical use of `ssh`_
* Practice writing simple `bash`_ scripts using `vi`_
* Practice using basic features of GNU `screen`_
* Understand pipes and redirects
* Recognize the benefits of the Unix Philosophy

Prep Materials
--------------

If time permits, try to review these resources before the meet-up. If you can't, come anyway.

* Read `Learning the Shell "Why Bother?" and "What is The Shell?" sections <http://linuxcommand.org/lc3_learning_the_shell.php>`_ on linuxcommand.org (~5 minutes)
* Read the `vi intro and Interface sections on Wikipedia <http://en.wikipedia.org/wiki/Vi>`_ (~5 minutes)
* Read the `GNU Screen Intro <http://aperiodic.net/screen/start>`_ and `Screen details sections on aperiodic.net <http://aperiodic.net/screen/detailed_description>`_ (~5 minutes)
* Watch the `TotT CLI slidecast <../_static/casts/cli.html>`_ (~30 minutes) which includes live demos of:

  * `Bash Basics <../_static/casts/cli.html#/8>`_ (~6 minutes)
  * `Bash Basics++ <../_static/casts/cli.html#/10>`_ (~4 minutes)
  * `Vi Simple Editing <../_static/casts/cli.html#/17>`_ (~3 minutes)
  * `Working in GNU Screen <../_static/casts/cli.html#/17>`_ (~8 minutes)

* Read `Unix Philosophy <http://en.wikipedia.org/wiki/Unix_philosophy>`_ on Wikipedia (~10 minutes)
* Read `Unix Pipeline <http://en.wikipedia.org/wiki/Pipeline_(Unix)>`_ on Wikipedia (~10 minutes)
* Watch the `TotT Unix Philosophy screencast <../_static/casts/unix.html>`_ (~25 minutes) with live demos of:

  * `Small Superlatives <../_static/casts/unix.html#/8>`_ (~3 minutes)
  * `Text Everywhere <../_static/casts/unix.html#/10>`_ (~5 minutes)
  * `Pipes and Redirects <../_static/casts/unix.html#/14>`_ (~8 minutes)

Exercises
---------

You will need to complete the :doc:`/setup` instructions before you proceed with these exercises. Once you are set up, SSH into *tottbox* using the ``vagrant ssh`` command from the setup instructions. Then tackle the problems below. `Document what you find in a gist <https://gist.github.com/>`_ and share it in the `TotT community`_ later.

Count on ``man``
################

Run the following command:

.. code-block:: console

  wc /vagrant/Vagrantfile

What does the ``wc`` command do? What does the output mean? Try ``man wc`` or Googling.

Now use the ``mkdir`` command to create a directory ``fun`` in ``~/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p``. Where should look for how to do this easily? (Hint: If you're making them one at a time, you're doing it wrong.)

Run in the background
#####################

Run the following commands:

.. code-block:: console

  cd ~
  git clone https://github.com/ether/etherpad-lite.git
  cd etherpad-lite
  bin/run.sh &

Wait a bit. When the console finally states "You can access your Etherpad-Lite instance at http://0.0.0.0:9001/", visit http://192.168.33.10:9001 in your web browser. Enter a pad name. Click new pad and enter some text. (Bonus: What happens when you try to access 0.0.0.0? What is 0.0.0.0? Why does it tell you this?)

The ampersand (&) on the last command you entered tells bash to run the command in the background. Control over the terminal returns to you immediately and the command continues to run in the background. Enter ``ls`` in the console to prove it.

Now type ``exit`` in the *tottbox* terminal. What do you see in your browser? What does this tell you about background tasks?

Open a new ssh connection to *tottbox*. Run the commands:

.. code-block:: console

  cd ~/etherpad-lite
  screen -S etherpad -d -m ./bin/run.sh

Refresh your web browser. What happens? Type ``exit`` in the *tottbox* terminal. What do you see in the browser? What's different this time?

SSH back into *tottbox* and type ``screen -S etherpad -X quit``. What does this command do?

Automate with bash
##################

Start ``screen``. Create a second screen window (Ctrl-A, c). Start ``vi``. Practice flipping back and forth between the ``vi`` editor and prompt with the screen hotkey: Ctrl-A, Space.

When you're comfortable, use ``vi`` to write a script named ``etherpad.sh`` that automates the cloning and running steps you performed in the last section (the path using ``screen``, that is). Use the terminal in the other screen window to try running your script. Flip back and forth between the two windows to debug any problems.

Include your script in your gist. When complete, it should:

#. Clone etherpad-lite from GitHub
#. Start a ``screen`` session
#. Run etherpad-lite in ``screen``
#. Disconnect from the ``screen`` session immediately

Provision on ``vagrant up``
###########################

Check if you have etherpad running in a screen still using ``screen -ls``. If so, kill it before continuing.

Open your ``/vagrant/Vagrantfile`` in ``vi``. Modify it so that when *tottbox* starts, it executes your etherpad clone-and-run script. Test to see if it works using the ``vagrant provision`` on your laptop (**not** on *tottbox*). What does ``vagrant provision`` do again? When might provisioning be useful?

Provision from a gist
#####################

Revert your Vagrantfile back to its original state. If you destroy it, just download it again from the link in the setup assignment.

Look at the `heredoc <http://en.wikipedia.org/wiki/Here_document>`_ at the top of the Vagrantfile. What is it doing? What are some pros and cons of this approach?

Extend the script
#################

Extend your script to support any or all of the following. Share your solutions in your gist.

1. If the etherpad-lite repository already exists, execute ``git pull`` within it instead of cloning a new copy on top of it. (Hint: Google for "bash file test operator".)
2. Accept one command line argument: a string having value "start" or "stop". Do the right thing for each value, including checking to make sure a etherpad is not already running when starting or stopped when stopping. Some hints:
<commit_msg>Bash cleanup, navigation exercise<commit_after>Bash, Screen, Vi, and SSH
=========================

Goals
-----

* Get comfortable working at the command line
* Gain experience with common Unix command line tools
* Understand the typical use of `ssh`_
* Practice writing simple `bash`_ scripts using `vi`_
* Practice using basic features of GNU `screen`_
* Understand pipes and redirects
* Recognize the benefits of the Unix Philosophy

Prep Materials
--------------

If time permits, try to review these resources before the meet-up. If you can't, come anyway.

* Read `Learning the Shell "Why Bother?" and "What is The Shell?" sections <http://linuxcommand.org/lc3_learning_the_shell.php>`_ on linuxcommand.org (~5 minutes)
* Read the `vi intro and Interface sections on Wikipedia <http://en.wikipedia.org/wiki/Vi>`_ (~5 minutes)
* Read the `GNU Screen Intro <http://aperiodic.net/screen/start>`_ and `Screen details sections on aperiodic.net <http://aperiodic.net/screen/detailed_description>`_ (~5 minutes)
* Watch the `TotT CLI slidecast <../_static/casts/cli.html>`_ (~30 minutes) which includes live demos of:

  * `Bash Basics <../_static/casts/cli.html#/8>`_ (~6 minutes)
  * `Bash Basics++ <../_static/casts/cli.html#/10>`_ (~4 minutes)
  * `Vi Simple Editing <../_static/casts/cli.html#/17>`_ (~3 minutes)
  * `Working in GNU Screen <../_static/casts/cli.html#/17>`_ (~8 minutes)

* Read `Unix Philosophy <http://en.wikipedia.org/wiki/Unix_philosophy>`_ on Wikipedia (~10 minutes)
* Read `Unix Pipeline <http://en.wikipedia.org/wiki/Pipeline_(Unix)>`_ on Wikipedia (~10 minutes)
* Watch the `TotT Unix Philosophy screencast <../_static/casts/unix.html>`_ (~25 minutes) with live demos of:

  * `Small Superlatives <../_static/casts/unix.html#/8>`_ (~3 minutes)
  * `Text Everywhere <../_static/casts/unix.html#/10>`_ (~5 minutes)
  * `Pipes and Redirects <../_static/casts/unix.html#/14>`_ (~8 minutes)

Exercises
---------

You will need to complete the :doc:`/setup` instructions before you proceed with these exercises. Once you are set up, SSH into *tottbox* using the ``vagrant ssh`` command from the setup instructions. Then tackle the problems below. `Document what you find in a gist <https://gist.github.com/>`_ and share it in the `TotT community`_ later.

Learn to navigate
#################

The shell prompt is stateful. We often say we are "in" such-and-such a directory and that we "go to" other directories. How we refer to files at the command prompt depends on our current working directory.

Enter the following commands exactly as shown. As you go along, document what each command does and where you wind up as a result.

.. code-block::

   cd /vagrant
   ls
   ls -l
   ls -al
   cd ..
   pwd
   cd .
   pwd
   cd ~
   pwd
   ls
   ls ..
   ls ../..
   cd -
   pwd

Count on ``man``
################

Run the following command:

.. code-block:: console

  wc /vagrant/Vagrantfile

What does the ``wc`` command do? What does the output mean? Try ``man wc`` or Googling.

Now use the ``mkdir`` command to create a directory ``fun`` in ``~/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p``. Where should look for how to do this easily? (Hint: If you're making them one at a time, you're doing it wrong.)

Run in the background
#####################

Run the following commands:

.. code-block:: console

  cd ~
  git clone https://github.com/ether/etherpad-lite.git
  cd etherpad-lite
  git checkout 1.3.0
  bin/run.sh &

Wait a bit. When the console finally states "You can access your Etherpad-Lite instance at http://0.0.0.0:9001/", visit http://192.168.33.10:9001 in your web browser. Enter a pad name. Click new pad and enter some text. (Bonus: What happens when you try to access 0.0.0.0? What is 0.0.0.0? Why does it tell you this?)

The ampersand (&) on the last command you entered tells bash to run the command in the background. Control over the terminal returns to you immediately and the command continues to run in the background. Enter ``ls`` in the console to prove it.

Now type ``exit`` in the *tottbox* terminal. What do you see in your browser? What does this tell you about background tasks?

Open a new ssh connection to *tottbox*. Run the commands:

.. code-block:: console

  cd ~/etherpad-lite
  screen -S etherpad -d -m ./bin/run.sh

Refresh your web browser. What happens? Type ``exit`` in the *tottbox* terminal. What do you see in the browser? What's different this time?

SSH back into *tottbox* and type ``screen -S etherpad -X quit``. Try etherpad in your browser again. What does this command do? Where should you look if you can't figure it out?

Automate with bash
##################

Start ``screen``. Create a second screen window (Ctrl-A, c). Start ``vi``. Practice flipping back and forth between the ``vi`` editor and prompt with the screen hotkey: Ctrl-A, Space.

When you're comfortable, use ``vi`` to write a script named ``etherpad.sh`` that automates the cloning and running steps you performed in the last section (without screen or &). Use the terminal in the other screen window to try running your script. Flip back and forth between the two windows to debug any problems.

Provision on ``vagrant up``
###########################

Check if you have etherpad running in a screen still using ``screen -ls``. If so, kill it before continuing.

Open your ``/vagrant/Vagrantfile`` in ``vi``. Modify it so that when *tottbox* starts, it executes your etherpad clone-and-run script in a screen session. Test to see if it works using the ``vagrant provision`` on your laptop (**not** on *tottbox*). What does ``vagrant provision`` do again? When might provisioning be useful?

Provision from a gist
#####################

Revert your Vagrantfile back to its original state. If you destroy it, just download it again from the link in the setup assignment.

Look at the `heredoc <http://en.wikipedia.org/wiki/Here_document>`_ at the top of the Vagrantfile. What is it doing? What are some pros and cons of this approach?

Extend the script
#################

Extend your script to support any or all of the following. Share your solutions in your gist.

1. If the etherpad-lite repository already exists, execute ``git pull`` within it instead of cloning a new copy on top of it. (Hint: Google for "bash file test operator".)
2. Accept one command line argument: a string having value "start" or "stop". Do the right thing for each value, including checking to make sure a etherpad is not already running when starting or stopped when stopping. Some hints:

{'fraction_non_alphanumeric': 0.12921169739351557, 'fraction_numerical': 0.010251112523839796, 'mean_word_length': 3.9488792764451435, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '880481', 'n_tokens_mistral': 3907, 'n_tokens_neox': 3658, 'n_words': 1719}