Lesson 4: Shell Navigation
Homepage | Content | Slides | Video |
Warning
This lesson is under construction. Learn from it at your own risk. If you have any feedback, please fill out our General Feedback Survey.
Homepage | Content | Slides | Video |
Warning
This lesson is under construction. Learn from it at your own risk. If you have any feedback, please fill out our General Feedback Survey.
A shell is a text-based user-interface for a computer.
sh | Required by all POSIX Operating Systems. |
bash | Default on most GNU/Linux-based Operating Systems. |
csh | Default shell on most BSD (Unix) based Operating Systems |
zsh | The hip new shell on the block. |
fish | Yet another hip new shell on the block. |
# Prints the current working directory (where you are)
$ pwd
# Prints the contents of the current working directory
$ ls
# Navigates to a new directory.
$ cd <path/to/other/directory>
# Prints a string to the screen.
$ echo "some thing $AND_VARS"
# Prints the contents of a file(s) to the screen.
$ cat foo.txt bax.txt
# Searches `file.txt` for the string `foo`
$ grep foo file.txt
# Prints a file to the screen so you can arrow up/down.
$ less file.txt
# Prints environment variables to the screen.
$ env
# Prints out current user
$ whoami
# When in doubt, always type help.
$ help
about_me.sh
#!/bin/sh
if [ $(whoami) == "root" ]; then
echo "You're root!"
else
echo "Your username is $(whoami)"
echo "Your home-directory is $HOME"
echo "Your current directory is $PWD"
echo "Your computer's host-name is $HOSTNAME"
fi
Invoke with:
# Tell Linux that this can be run as a program
$ chmod +x about_me.sh
# Invoke the script.
$ ./about_me.sh
Your username is dobc
Your home-directory is /home/dobc
Your current directory is /home/dobc
Your computer's host-name is dobc
$ grep 'searchstring' files/* | less
$ true || echo 'never gets here'
$ false && echo 'never gets here'
$ echo 'this now an error message' 1>&2 | grep -v error
this is now an error message
!$ # last argument to last command
$ cat /dir
cat: /dir/: Is a directory
$ cd !$
cd /dir
$ pwd
/dir
$ for x in 1 2 3; do echo $x; done # Use seq for longer sequences
1
2
3
$ var='this is a var'; echo ${var//this is } # Deletes 'this is '
a var
$ ls -l `which bash`
-rwxr-xr-x 1 root root 1029624 Nov 12 15:08 /bin/bash
$ set -a blocks
$ blocks="10.0.0.0/24"
$ set -a ips
$ ips=`fping -g 10.0.0.0/24 2>&1 | grep unreachable | tr \\ \\n`
$ for ip in $ips; do
$ nmap -p 22 $ip && ips=`echo ${ips//$ip} \
| tr -s \\n`
$ done
$ echo $ips
name () {
# code goes here
}
You should know the following:
Variable | Meaning |
---|---|
$* | All arguments passed |
$? | Return code of last command run |
"$@" | All arguments passed as a list |
$CDPATH | Colon-delimited list of places to look for dirs |
$HOME | Location of user homedir |
$IFS | Internal Field Seperator |
$OLDPWD | Previous PWD |
Variable | Meaning |
---|---|
$PATH | Colon-delimited list of places to find executables |
$PWD | Present Working Directory |
$SHELL | Path to running shell |
$UID | User ID |
$USER | Username |
You should also read the EXPANSION section of the bash man page.
. | The current directory |
.. | The parent directory |
~ | Alias for your home directory |
/ | Separates directories: one_dir/another_dir/last_dir Alone, or at the start of a path, it is the root directory. |
$ tree -F
.
├── bar/
│ ├── one/
│ └── two
├── baz/
└── foo/
└── a/
└── b
5 directories, 2 files
Used as a stand-in for any character(s).
Example: cat *.log cats all files in the current working directory ending in .log.
Used to specify a set.
Example: ls {foo,bar,baz}ley-thing expands to ls fooley-thing barley-thing bazley-thing
Escape special characters (treat them as normal characters) with the escape character (\).
Pressing the tab key auto-completes a command, file-path, or argument in your shell.
Pressing tab multiple times completes the command to the best of the shells ability and then lists the possible completions (if there are any).
$ ls b # <tab>
$ ls ba # <tab>
bar_thing/ baz_thing/
$ ls bar # <tab>
$ ls bar_thing
Nano is a great terminal text editor to start with. Later in your career you may start using emacs or vi/vim but to start with nano is familiar, easy to use, and gets the job done.
To use nano simply execute it like any other command in the terminal.
$ nano # Open with empty file
$ nano <file_name> # Edit a specific file
This editor is almost exactly like any word processor or plain-text editor except that you don't have a mouse -- only keyboard shortcuts. The instruction bar at the bottom of the screen is explains all of the key-bindings from saving, to exiting, to cut and pasting.
Using nano create a file called hello_world.sh and put the following in it:
#!/bin/bash
# declare STRING variable
STRING="Hello World"
# print variable on a screen
echo $STRING
Now make the script executable using chmod and run the script. What does it do?
$ chmod +x hello_world.sh
$ ./hello_world.sh
Hello World
When you pass arguments to a bash script, you can reference them inside of the script using $1, $2, etc. This means if you do something like 'foo.sh bar', $1 will return bar. You can also use $@ to reference all arguments.
For example:
#!/bin/bash
echo $1 # prints argument #1 given to script
echo $@ # prints all arguments given to script
You can also take input using the read command which then stores it in a variable. If you pass read '-a', it puts the input into a bash array.
For example:
#!/bin/bash
echo -e "Tell me a word: \c"
read word
echo "Your word is $word"
$ ./read.sh
Tell me a word: foo
Your word is foo
Bash conditionals use if, else, then and fi operators. You can compare strings, files and even command output. An example:
#!/bin/bash
if [ "$1" == "foo" ] ; then
echo "You said $1"
else
echo "You did not say foo"
fi
$ ./sayfoo.sh foo
You said foo
$ ./sayfoo.sh bar
You did not say foo
We likely won't have enough time to cover these all, but feel free to try later!
#!/bin/bash
# The flags for tar do the following:
# v - verbose
# c - compress
# z - use gzip
# f - output to file
tar -vczf /tmp/dobc-backup.tar.gz /home/dobc
$ ./backup.sh
tar: Removing leading `/' from member names
/home/dobc/
/home/dobc/backup.sh
/home/dobc/hello_world.sh
/home/dobc/.bash_profile
/home/dobc/.bashrc
/home/dobc/.bash_logout
Bonus: How could you list the contents of the file?
#!/bin/bash
echo $@
echo $3 $2 $1
$ chmod +x args.sh
$ ./args.sh DOBC is awesome
DOBC is awesome
awesome is DOBC
Bonus #1: What happens if you give the script nothing? Bonus #2: What happens if you give it the string "DOBC is awesome" with quotes?
#!/bin/bash
echo -e "Tell me a noun: \c"
read noun
echo -e "Tell me a verb: \c"
read verb
echo -e "Tell me an adjective: \c"
read adj
echo "I plan to $verb a $adj $noun"
$ ./input.sh
Tell me a noun: apple
Tell me a verb: eat
Tell me an adjective: large
I plan to eat a large apple
#!/bin/bash
if [ "$1" == "$2" ] ; then
echo "Yay, they match!"
else
echo "Boo, they don't match :("
fi
$ ./ifelse.sh foo foo
Yay, they match!
$ ./ifelse.sh foo bar
Boo, they don't match :(