Choose one of the following:
- Record a video of yourself performing the tasks described in part 1
- Create a file that lists the commands you used to perform the tasks described in part 1
- Hint:
history | tail -11
then...
- Complete part 2. Answer the questions in the doc, then schedule a time to meet with your mentor and discuss the commands and how they work
- Use
echoand text redirection>to create a file and add at least one line of text - Use
echoand text redirection>>to append lines to a second file - Use
cat <file1> <file2>to output the contents of both files - Use cat and text redirection to create a third file
- In one terminal windows use text redirection and
echoto append lines to a file, in a second terminal window usetail -fto view the lines being appended. Stop thetail -f - Use
tail -f | grep <pattern>and continue to append lines to the file. Notice that only lines that match your pattern are displayed by grep. - Do the above, but invert the match so only lines without the pattern are displayed.
grep -v
- Use
catandsortto display the contents of all the files alphabetically.cat three.txt | sort
- Use
catandwc -lto count the number of lines in all the files.cat three.txt | wc -l
- Use
headto display the first 3 items from the above step
-
Inspect your environment with
env -
exportvariable on the command lineexport TESTVAR1=testvar1 -
Use
grepto find that variable in your environmentenv | grep $TESTVAR1
-
Set a second variable on the command line without exporting it
- Is that variable in the environment?
- Without
exportthe variable is only available to the shell - With export, the variable is added to the environment is available to all processes(?)
- Question: is this related to the
sourcecommand ? - https://jvns.ca/blog/2017/03/26/bash-quirks/
- Without
- Is that variable in the environment?
-
exportthat variable. No need to re define it.- Does it show up in the
envnow?
- Does it show up in the
-
Use
whichto find the path of thegrepbinarywhich grep
-
What are the permissions on the
grepbinary?ls -l-rw-r--r--: read and write access- http://www.zzee.com/solutions/linux-permissions.shtml
-
Make a directory
binin your home directory -
Create a script that displays ‘Hello World’ and the in the
$HOME/bindirectory.- Can you run it without using the
bashorshcommand?- No. I have to change the file permission from having just read and write access to having access to execute
- Can you run it without using the
-
What are the permissions on the files you created above?
- read and write access
-
Make the script executable using
chmod +xchmod +x say_hello.sh- Now the permissions look like this:
-rwxr-xr-x
-
Find use
envandgrepto find the$PATHenvironment variableenv | grep $PATH
-
Add
$HOME/binto your$PATH, be sure toexportit- The script should now be executable from anywhere without using its full path
- Can you find your script using
whichwhich say_hello.sh
-
Open a new terminal window.
- Can you still use
whichto find your script? - Why or why not?
- Is
$HOME/binstill in your$PATH
- Can you still use
-
Find hidden files in your home directory using
ls -a $HOME -
Edit
.bash_profileto make your change to$PATHpersist- The difference between
.bash_profileand.bashrcand why OSX is different: http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html- add
export PATH=$PATH:$HOME/binto.bash_profile
- add
- The difference between
-
Three ways to set env variables:
- foo=bar (local scope)
- export foo=bar (global scope inside your terminal window => 1 session)
- putting the declaration into a file and sourcing the file x
-
echo "echo $var" >>into your script -
Set
var=testin your terminal and run the script.- Does it output “test”
-
export varand run the script.- Does it output “test”
- Why or why not?
-
Open a new window and run the script
- Does it output “test”
- Why or why not?
-
echo "export var2='exported in the script'" >>into your script and run it- Did
$var2get set in your current shell? - Why or why not?
- No. I need to reload the script with
sourcehttps://linuxize.com/post/bash-source-command/
- No. I need to reload the script with
- Did
-
sourceyour script and see if$var2is set this time.- Did
$var2get set in your current shell?- yes
- Why or why not?
sourceruns the script in the current environment so the scope overlaps. If you do notsourceit runs in a sub-shell and cannot set new variables in the current session.
- Did
-
Create another executable script that outputs “Goodbye World” in you home directory but not in your
bindirectory. Create a symlink to the goodbye_world script in thebindirectory so that it is accessible if your path is set correctly. https://www.youtube.com/watch?v=l_1Q3DG3uoE -
Inspect your symlink using
ls -llrwxr-xr-x- You should now be able to run your new script from anywhere even though it is only a symlink in the
bindirectory- When is this pattern helpful?
- When you have something that is used in multiple projects and you don’t want to recreate the resource in multiple places
- When is this pattern helpful?
-
Use Homebrew to install python, ansible and terraform
-
Use
ls -l $(which <program>)to see if the binaries are linked. If they are not usebrew linkandwhichto see where the binaries exist- ex:
ls -l $(which ansible)
- ex:
-
Inspect the path of each binary and determine if it is a symlink and if so, where the actual binary is on the file system
ls /usr/local/bin/terraformshows me that terraform is a symlink (on my machine, it’s pink and has an@at the end of the name)realpath /usr/local/bin/ansible=> /usr/local/Cellar/ansible/2.5.4/libexec/bin/ansiblerealpath /usr/local/bin/terraform=> /usr/local/Cellar/terraform/0.11.7/bin/terraformrealpath /usr/local/bin/python=> /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/bin/python2.7
-
Symlinks
- To keep a directory organization and have bin files and keep in organized like brew install