Skip to content

Lecture 1

JunjieW edited this page Jan 27, 2017 · 11 revisions

Environment Setup

  1. Python 3.5 is required.
  2. Anaconda 3 on Windows:
  3. Class based on swcarpentry:

Intro to Shell & Basic Commands

  1. Download a set of data for practice: http://swcarpentry.github.io/shell-novice/setup/

Lab

  1. pipes, diff of > (truncate) and >> (append)

  2. filter and wildcards. wildcards can be very complex, but simple syntax are used often.

  3. wc command

  4. cut command

#TODO
  1. tail and head
#TODO
  1. double quotes vs single quote
#TODO
  1. shell history
$ !$ # which retrieves the last word of the last command. 
$ <command> !$ # to another execute the command on the last parameter of the last command
# build a simple script from history
$ history | tail -n <number_of_commands> > <your_simple_script.sh>
  1. shell script basic
# To get the argument when running a script, 
# use $<number_starts_from_1> or $@ to get all arguments
echo $1
echo $@
  1. standard input & stdout & stderr.
# Interesting fact
# In the following code, wc assumes it is supposed to process standard input, so it just 
# sits there and waits for us to give it some data interactively
$ wc -l | sort -n
  1. debug
# (1) dry-run
# dry-run technique for debugging simple shell script
# The way to check what a loop would do is to echo the commands 
# it would run instead of actually running them.

# (2) use -x option when run a script by bash command
$ bash -x <a_script.sh>
  1. grep. “grep” is a contraction of “global/regular expression/print”. grep finds and prints lines in files that match a pattern.

  2. Milse: funny commands

# print type y's endlessly
$ yes 

# removes adjacent duplicated lines from its input. 
# use `sort` command to get unique for each one
$ uniq  

Assignments

    1. Sort content of file, specifying the key. Remember you need to specify where sort keys start and where they end, otherwise (as in when you use -k9 instead of -k9,9) they end at the end of the line.
# http://unix.stackexchange.com/a/78926
$ sort -t '\t' -k9,9 -k14,14 -k16,16n
    1. date & pipline & redirect
# show current date and time
$ echo date 

# print text on screen
$ echo 'some_text_in_single_quote'

# redirect text to a text file (append to the end of the file)
$ echo 'some_text_in_single_quote' >> <path_to_a_file>

# use -e option to interpret escaped special character
$ echo -e "Hello\nworld"
for <var_name> in <a_list>
do
   # do something here
done

# for example
for filename in data_*.txt
do
   # do something here
done

Clone this wiki locally