Unix101 Lesson 2

Concepts we covered

  • Hidden files and directories:
    • Add a . to the beginning of a filename or directory name to make it hidden
    • use ls -a to show all the hidden files and directories
  • The ls -a command always shows a . and a .. file in each directory.
    • . is the current directory
    • .. is the directory above. Try cd ..
  • Get ssh login to work without a password:
    • Use ssh-keygen to generate a keypair on your local machine
    • Copy your public key to server (on Linux use this command on your local machine: ssh-copy-id ssh-copy-id -i .ssh/id_rsa.pub <username>@b3rlin.net
  • Does the ssh connection die after a certain time of inactivity?
  • The .bashrc file
    • This is a script that is executed each time the user logs in
    • Add echo hello to the end of your .bashrc file to see what happens when you login
    • TIP: if you want to persist a variable, add it to this file.
  • Variables
    • Setting a variable: FOOD=sausage
    • Reading a variable: echo $FOOD
    • To set an environment variable, you have to use export <variable_name>
    • To see all environment variables, enter printenv

    • Environment variables determine how your command-line functions. For example: the PS1 variable. Try entering PS1=aargh
    • A very very very important environment variable is PATH.
    • See here for some more information on environment variables
  • The PATH environment variable
    • This determines which directories UNIX looks in when you tell it to run a command.
    • To find out which directory a command came from, use which. For example: which ls
    • Some commands are built-in, like cd, therefore which cd returns no result.
    • You can edit your PATH variable (and should know how to do this).
    • Try temporarily breaking your command line by entering PATH=woop and seeing what happens. Fix it by logging in and out again.
    • Further information here and and here.

Exercises

Exercise 1

First of all, make it so that every time you login to the server, you are greeted by

  • A cow saying a fortune
  • Ensure the fortune is always a short one (read the man page)
  • NOTE: if you make a mistake, you could break your login so that it no longer works.

Exercise 2

Create a directory called homework102 in your home directory. Copy your info script from last time into this directory. Rename it to info.sh. Now change it so that

  • it only uses variables (i.e. no commands other than echo)
  • the output contains less linebreaks than last time (compare the output below to last time)
  • it outputs the contents of the FOOD variable which you set before running the script.

This should be the output:

Hello world!
I am a script located in the following location: <working directory here>		
I am being run by the following user: <username>
I like to eat: <content of FOOD variable>
Goodbye.

Exercise 3

Now write a script called more_info.sh that asks you for your name and then outputs the date.

  • To make the script ask your for input, use read VARIABLE. Test it on the command line.
  • To output the date, use this command: date +%F. Test it on the command line.
  • HINT: you can put the output of a command in a variable by doing this: VARIABLE=$(fortune).

This should be the output:

What is your name?	<enter name here>
Hello, <your name>.
Today's date is 2012-04-04.

Exercise 4

Create a directory called scripts in your home directory. Add the scripts directory to your PATH variable (yes, you can do it!) and make sure it is persistent (in other words, still there after you log in and out again). Create a script called how_many.sh which counts the number of normal and hidden files in the current directory.

  • HINT: to count the number of normal files in a directly use ls | wc -l. Read the manpage of wc again to remind yourself how it works.
  • This script should be executable in whatever directory you are in by simply entering how_many.sh

The output should be:

The number of normal files in this directory is <number of files>
The number of normal plus hidden files is <number of files>