Today I continued working on the CLI. We discussed environment variables, which store configuration information on our computers. Environment variables store a value associated with a name. Our environment variables will be written in all upper case, and the values they hold will be strings. So, for:
HOME=/home/treehouse
HOME is the environment variable, and /home/treehouse is the value.
And for:
PS1=/u /w $
The PS1 environment variable defines value the CLI prompt uses. When shown on the screen, the /u is replaced by treehouse (our username) and the /w is replaced by the "~" character (in other words, our current working directory, because the ~ stands in for our current working directory). These are then followed by the $ sign, so that the prompt looks like:
treehouse ~ $
The echo command prints the argument to the screen, it literally just echoes back the arguments we give it. However, if we use an environment variable with a dollar sign before it, like so:
echo $HOME
We will get an output of /home/treehouse
So,
cd $HOME is the same as cd /home/treehouse
Both will take us home. Entering the bash command will actually open a new instance of bash (the old instance will still be running, but inside of it, the new instance will be running). Bash is a POSIX shell. The name stands for Bourne-again shell, referring to its objective as a free replacement for the Bourne shell.
Note: On the Treehouse testing terminal, the "~" sign stands for /home/treehouse.
If I enter this on the command command line:
MESSAGE="hello world"
And then enter echo $MESSAGE on the next command line, the output will be:
hello world
If we create a new bash, then enter the above commands, the output on the new bash will NOT be "hello world," and this is because the environment variable, by default, does not pass down to a new session of bash. To prevent this, we can do this:
export MESSAGE="hello world"
Export will communicate the value down to any new bash instances created.
If I leave out the $, the output will be MESSAGE, as echo will simply echo the text, instead of the value for the environment variable. Environment variables are useful for controlling programs. Programs we write have access to the environment variables of their own processes, which means any environment variable exported will be available to that program.
$PATH will show the list of directories to search for when we run an executable (as in, where the bash will look for the program, the path it will take). the "which" command will print the location of a program. So, since echo is a program, entering:
which echo
In the command line will result in an output (at least, in my test environment) of:
/bin/echo
Also, if I enter:
/bin/echo Hello
OR
echo Hello
"Hello" will be the output for both. The "find" command allows us to locate a file based on its name using patterns, while the "grep" command allows us to look for a specific pattern within a file. A "." can be used to represent the current directory. So, we can look for a file like this:
find . -name "how_to_go_home.txt"
The video said the quotation marks are optional, but recommended. The "." means the search will be conducted in the current directory and any directories under it. If instead you search like this:
find / -name "sudousers"
Then the entire computer will be searched, and this could take a long time. You could also run:
find documents bin -name "how_to_go_home.txt"
And this would look for the document in the documents folder and in the bin folder. The grep command stands for Global Regular Expression Print, which means we want to search globally through a file for a regular expression. Grep will look through a file for a pattern, then print out the line that the pattern appears on. The command looks like so:
grep "is" hello.txt
And would return something like:
^^ The output would highlight the places where a match was made in the file for the item in the quotation marks. The quotation marks are actually not necessary, and the different CLI's may or may not color the results. The command grep -n will put line numbers in front of each result, and also, will separate each instance into its own line. The grep-i command will perform a case insensitive search. The grep-v command will find the lines that do not contain the search query. So, for example:
grep-v "e" mydocument.txt
Will find every line that does NOT contain an e. Entering the "man grep" command will give you grep options (stands for manual grep). If we ever enter grep followed by just a word, the output is blank, and will allow you to keep entering text, which grep will then scan for results. In this case, ctrl + d will exit the grep process.
Input redirection is done with the symbol <, while output redirection is done with the > symbol. A single > will rewrite the destination with the results. Double >> will append the results to the destination. The /dev/null is where you can send any outputs you want deleted. We learned about pipes (|) today. A pipe will pipe the output of command A to the input of command B. It can be chained on and on, so B will go into C and so on.
Opening Ruby from the CLI
On a side note, I opened Ruby today for the first time from the Command Line Interface on my Mac! I typed in irb (this stands for interactive Ruby), and this opened the irb program. Then I typed in puts "hello world" and received output! I used spotlight (command + space bar) and searched for terminal to access the CLI. I already had the CLI in my dock, but today I learned how to use spotlight (I just bought my mac like a week ago, so I'm becoming familiar with the Mac OS). I exited the program by typing exit into the irb.
SUMMARY OF CODING SKILLS
Total Treehouse Points: 3,717
Treehouse Points by Subject Matter: HTML 663, CSS 1,599, Design 1,193, Development Tools 232, and Miscellaneous
Treehouse Ranking (%): "You have more total points than 86% of all students."
Badge(s) Earned Today:
Environment and Redirection
Courses Completed:
How to Make a Website
HTML
CSS Foundations
CSS Layout Techniques
Aesthetic Foundations
Design Foundations
Photoshop Foundations
Design Foundations
Photoshop Foundations
Books Read or in Progress:"Head First HTML and CSS," by E. Robson & E. Freeman (In progress, I've read the 37 pg. preface and the first 255 pgs. of actual content, which is the HTML section of the book)
Hours Spent Coding Today: 4.5
Total Hours Coding: 219
No comments:
Post a Comment