-
Notifications
You must be signed in to change notification settings - Fork 0
Lecture 1
JunjieW edited this page Jan 27, 2017
·
11 revisions
- Python 3.5 is required.
- Anaconda 3 on Windows:
- Class based on swcarpentry:
- Download a set of data for practice: http://swcarpentry.github.io/shell-novice/setup/
-
pipes, diff of > (truncate) and >> (append)
-
filter and wildcards. wildcards can be very complex, but simple syntax are used often.
-
wccommand -
cutcommand
#TODO-
tailandhead
#TODO- double quotes vs single quote
#TODO- 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>- shell script basic
# To get the argument when running a script,
# use $<number_starts_from_1> or $@ to get all arguments
echo $1
echo $@
- 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- 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>-
grep. “grep” is a contraction of “global/regular expression/print”. grep finds and prints lines in files that match a pattern.
-
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 -
- 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-
- 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