Created
October 24, 2012 23:37
-
-
Save vedantk/3949618 to your computer and use it in GitHub Desktop.
Command line workshop notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
man | |
* man grep | |
* apropos uname | |
if, then, else, fi | |
* -e (file exists) | |
* -d (file is directory) | |
* -z (string is empty) | |
* -N (modified after last read) | |
* -gt, -lt, -eq, -ne, ... | |
$ if [ -d / ]; then echo "/ is a directory."; fi | |
$ [ -e README ] | |
$ if [ $? -eq 0 ]; then | |
echo ":-)" | |
else | |
echo ":'|" | |
fi | |
for, in, do, done | |
$ for file in $(ls); do echo $file; done | |
$ for line in $(cat README); do | |
echo $line | grep -q "*" | |
if [ $? -eq 1 ]; then | |
echo $line | |
fi | |
done | |
$ grep -v "*" README | |
Intermezzo - Run Daemon | |
#!, sleep, cat << EOF, File Streams (>, >>), Daemons | |
$ cat daemon.sh | |
#!/bin/sh | |
while [ 1 ]; do | |
for file in $(find . -name "*.py"); do | |
if [ -N $file ]; then | |
echo "$file has been updated, executing..." | |
python $file | |
fi | |
done | |
sleep 1 | |
done | |
$ chmod +x daemon.sh && ./daemon.sh & | |
$ cat << EOF > hello.py | |
#!/usr/bin/python | |
print("Hello world.") | |
EOF | |
Intermezzo - Factorial | |
variables, $(), while loops, math, read, arguments, chmod | |
$ cat factorial.sh | |
#!/bin/sh | |
n=$1 | |
if [ -z $n ]; then | |
echo -n "Enter a number: " | |
read n | |
fi | |
factorial=1 | |
while [ $n -gt 0 ]; do | |
factorial=$(( $factorial * $n )) | |
n=$(( $n - 1 )) | |
done | |
echo $factorial | |
$ chmod +x factorial.sh | |
$ ./factorial.sh 7 | |
$ ./factorial.sh | |
grep | |
* -q (quiet) | |
* -r (recursive) | |
* -i (case insensitive) | |
* -v (inverted match) | |
* -n (print line number) | |
* -o (only matching) | |
* -C (with context lines) | |
* -P (with perl-style regex) | |
Pick any corpus of text you have available, any project, and play with it. | |
regex | |
* <regex> | <char-class> | <special-char> | |
* | |
* <regex> : | |
* R? | |
* R+ | |
* R* | |
* R{n} | |
* R{n,} | |
* R{n,k} | |
* R{,k} | |
* R|R | |
* (R) | |
* ^R | |
* R$ | |
* | |
* <char-class> : | |
* [...] | |
* [^...] | |
* | |
* <special-char> : \{, \}, \(, \), ., ... | |
* | |
* Back References : (...) -> \n | |
$ grep -Po "\d" | |
$ grep -Po "[A-Z]+" | |
$ grep -Po "[0-9]*" | |
$ grep -Po "[a-z]{5}" | |
$ grep -Po "^Hello" | |
$ grep -Po "Hello$" | |
$ grep -Po "^Hello$" | |
$ grep -Po "I am (\w+) you are \1." | |
I am glad you are sad. | |
I am very sad you are sad. | |
I am happy you are happy. | |
I am happy you are happy. | |
sed | |
* s/foo/bar/g | |
* -i | |
$ sed 's/ /\t/g' | |
find | |
* -name R | |
* -type f|d | |
* -exec | |
find /tmp -exec echo {} \; | |
find ~/projects/misc-python -name "*.pyc" -exec rm {} \; | |
Appendix | |
* http://tldp.org/LDP/abs/html/refcards.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reminder: cover screen's Ctl+A+C, Ctl+A+A, Ctl+A+"