Skip to content

Instantly share code, notes, and snippets.

@vedantk
Created October 24, 2012 23:37
Show Gist options
  • Save vedantk/3949618 to your computer and use it in GitHub Desktop.
Save vedantk/3949618 to your computer and use it in GitHub Desktop.
Command line workshop notes
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
@vedantk
Copy link
Author

vedantk commented Oct 24, 2012

Reminder: cover screen's Ctl+A+C, Ctl+A+A, Ctl+A+"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment