In [1]:
pwd
Out[1]:
In [2]:
ls
In [3]:
cat bio.txt
In [4]:
!wc *.txt
In [5]:
%rehashx
In [6]:
wc bio.txt
In [7]:
%alias getpage wget -rkp -np %l
In [8]:
getpage http://isitpyconyet.com/
In [9]:
free
In [10]:
cat Abstract.txt
In [11]:
%magic
In [12]:
!head 047-validator.sh
In [13]:
!./047-validator.sh
In [14]:
%load 047-validator.sh
In [ ]:
#!/bin/sh
# VALIDATOR - Checks to ensure that all environment variables are valid
# looks at SHELL, HOME, PATH, EDITOR, MAIL, and PAGER
#
# from Wicked Cool Shell Scripts by Dave Taylor
# http://intuitive.com/wicked/wicked-cool-shell-script-library2.shtml
errors=0
in_path()
{
# given a command and the PATH, try to find the command. Returns
# 1 if found, 0 if not. Note that this temporarily modifies the
# the IFS input field seperator, but restores it upon completion.
cmd=$1 path=$2 retval=0
oldIFS=$IFS; IFS=":"
for directory in $path
do
if [ -x $directory/$cmd ] ; then
retval=1 # if we're here, we found $cmd in $directory
fi
done
IFS=$oldIFS
return $retval
}
validate()
{
varname=$1 varvalue=$2
if [ ! -z $varvalue ] ; then
if [ "${varvalue%${varvalue#?}}" = "/" ] ; then
if [ ! -x $varvalue ] ; then
echo "** $varname set to $varvalue, but I cannot find executable."
errors=$(( $errors + 1 ))
fi
else
if in_path $varvalue $PATH ; then
echo "** $varname set to $varvalue, but I cannot find it in PATH."
errors=$(( $errors + 1 ))
fi
fi
fi
}
####### Beginning of actual shell script #######
if [ ! -x ${SHELL:?"Cannot proceed without SHELL being defined."} ] ; then
echo "** SHELL set to $SHELL, but I cannot find that executable."
errors=$(( $errors + 1 ))
fi
if [ ! -d ${HOME:?"You need to have your HOME set to your home directory"} ]
then
echo "** HOME set to $HOME, but it's not a directory."
errors=$(( $errors + 1 ))
fi
# Our first interesting test: are all the paths in PATH valid?
oldIFS=$IFS; IFS=":" # IFS is the field separator. We'll change to ':'
for directory in $PATH
do
if [ ! -d $directory ] ; then
echo "** PATH contains invalid directory $directory"
errors=$(( $errors + 1 ))
fi
done
IFS=$oldIFS # restore value for rest of script
# The following can be undefined, and they can also be a progname, rather
# than a fully qualified path. Add additional variables as necessary for
# your site and user community.
validate "EDITOR" $EDITOR
validate "MAILER" $MAILER
validate "PAGER" $PAGER
# and, finally, a different ending depending on whether errors > 0
if [ $errors -gt 0 ] ; then
echo "Errors encountered. Please notify sysadmin for help."
else
echo "Your environment checks out fine."
fi
exit 0
In [15]:
%%bash
#!/bin/sh
# VALIDATOR - Checks to ensure that all environment variables are valid
# looks at SHELL, HOME, PATH, EDITOR, MAIL, and PAGER
#
# from Wicked Cool Shell Scripts by Dave Taylor
# http://intuitive.com/wicked/wicked-cool-shell-script-library2.shtml
errors=0
in_path()
{
# given a command and the PATH, try to find the command. Returns
# 1 if found, 0 if not. Note that this temporarily modifies the
# the IFS input field seperator, but restores it upon completion.
cmd=$1 path=$2 retval=0
oldIFS=$IFS; IFS=":"
for directory in $path
do
if [ -x $directory/$cmd ] ; then
retval=1 # if we're here, we found $cmd in $directory
fi
done
IFS=$oldIFS
return $retval
}
validate()
{
varname=$1 varvalue=$2
if [ ! -z $varvalue ] ; then
if [ "${varvalue%${varvalue#?}}" = "/" ] ; then
if [ ! -x $varvalue ] ; then
echo "** $varname set to $varvalue, but I cannot find executable."
errors=$(( $errors + 1 ))
fi
else
if in_path $varvalue $PATH ; then
echo "** $varname set to $varvalue, but I cannot find it in PATH."
errors=$(( $errors + 1 ))
fi
fi
fi
}
####### Beginning of actual shell script #######
if [ ! -x ${SHELL:?"Cannot proceed without SHELL being defined."} ] ; then
echo "** SHELL set to $SHELL, but I cannot find that executable."
errors=$(( $errors + 1 ))
fi
if [ ! -d ${HOME:?"You need to have your HOME set to your home directory"} ]
then
echo "** HOME set to $HOME, but it's not a directory."
errors=$(( $errors + 1 ))
fi
# Our first interesting test: are all the paths in PATH valid?
oldIFS=$IFS; IFS=":" # IFS is the field separator. We'll change to ':'
for directory in $PATH
do
if [ ! -d $directory ] ; then
echo "** PATH contains invalid directory $directory"
errors=$(( $errors + 1 ))
fi
done
IFS=$oldIFS # restore value for rest of script
# The following can be undefined, and they can also be a progname, rather
# than a fully qualified path. Add additional variables as necessary for
# your site and user community.
validate "EDITOR" $EDITOR
validate "MAILER" $MAILER
validate "PAGER" $PAGER
# and, finally, a different ending depending on whether errors > 0
if [ $errors -gt 0 ] ; then
echo "Errors encountered. Please notify sysadmin for help."
else
echo "Your environment checks out fine."
fi
exit 0
In [16]:
%%ruby
say = "if that's your thing"
puts say.upcase
In [17]:
%%script perl
open (ABSTRACT, 'Abstract.txt');
while (<ABSTRACT>) {
$_ =~ s/([A-Z][a-z]*)/reverse($1)/ge;
print $_;
}
In [18]:
%load http://catherinedevlin.pythoneers.com/047-validator.sh
In [ ]:
#!/bin/sh
# VALIDATOR - Checks to ensure that all environment variables are valid
# looks at SHELL, HOME, PATH, EDITOR, MAIL, and PAGER
#
# from Wicked Cool Shell Scripts by Dave Taylor
# http://intuitive.com/wicked/wicked-cool-shell-script-library2.shtml
errors=0
in_path()
{
# given a command and the PATH, try to find the command. Returns
# 1 if found, 0 if not. Note that this temporarily modifies the
# the IFS input field seperator, but restores it upon completion.
cmd=$1 path=$2 retval=0
oldIFS=$IFS; IFS=":"
for directory in $path
do
if [ -x $directory/$cmd ] ; then
retval=1 # if we're here, we found $cmd in $directory
fi
done
IFS=$oldIFS
return $retval
}
validate()
{
varname=$1 varvalue=$2
if [ ! -z $varvalue ] ; then
if [ "${varvalue%${varvalue#?}}" = "/" ] ; then
if [ ! -x $varvalue ] ; then
echo "** $varname set to $varvalue, but I cannot find executable."
errors=$(( $errors + 1 ))
fi
else
if in_path $varvalue $PATH ; then
echo "** $varname set to $varvalue, but I cannot find it in PATH."
errors=$(( $errors + 1 ))
fi
fi
fi
}
####### Beginning of actual shell script #######
if [ ! -x ${SHELL:?"Cannot proceed without SHELL being defined."} ] ; then
echo "** SHELL set to $SHELL, but I cannot find that executable."
errors=$(( $errors + 1 ))
fi
if [ ! -d ${HOME:?"You need to have your HOME set to your home directory"} ]
then
echo "** HOME set to $HOME, but it's not a directory."
errors=$(( $errors + 1 ))
fi
# Our first interesting test: are all the paths in PATH valid?
oldIFS=$IFS; IFS=":" # IFS is the field separator. We'll change to ':'
for directory in $PATH
do
if [ ! -d $directory ] ; then
echo "** PATH contains invalid directory $directory"
errors=$(( $errors + 1 ))
fi
done
IFS=$oldIFS # restore value for rest of script
# The following can be undefined, and they can also be a progname, rather
# than a fully qualified path. Add additional variables as necessary for
# your site and user community.
validate "EDITOR" $EDITOR
validate "MAILER" $MAILER
validate "PAGER" $PAGER
# and, finally, a different ending depending on whether errors > 0
if [ $errors -gt 0 ] ; then
echo "Errors encountered. Please notify sysadmin for help."
else
echo "Your environment checks out fine."
fi
exit 0
In [19]:
<html>
<head>
<meta http-equiv="refresh" content="0; url=http://hotspot.smartcity.com/login.html">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="expires" content="-1">
</head>
<body>
</body>
</html>
In [ ]:
ls
In [ ]:
filenames =! ls *.{txt,md}
filenames
In [ ]:
for filename in filenames:
(stem, extension) = filename.split('.')
newname = stem + ".bkup." + extension
! cp {filename} {newname}
In [ ]:
ls *.{md,txt}
In [ ]:
!wc *.bkup.{md,txt}
In [ ]:
bkupfiles =! ls *.bkup.*
for bkupfile in bkupfiles:
wc_result =! wc {bkupfile}
wordcount = int(wc_result[0].split()[1])
print(bkupfile)
print(wordcount)
if wordcount > 50:
!chmod 400 {bkupfile}
print('making read-only')
else:
print('leaving it alone')
In [ ]:
ls -l *.bkup.*
In [ ]:
!chmod 700 *.bkup.*
In [ ]:
!rm *.bkup.*
In [20]:
for filename in textfilenames:
!wc -w {filename}
In [21]:
round(82.621, 1)
Out[21]:
In [22]:
/round 82.621 1
Out[22]:
In [23]:
len("I wonder how long this sentence is.")
Out[23]:
In [24]:
;len I wonder how long this sentence is.
Out[24]:
In [25]:
max("go", "to", "the", "head", "of", "the", "class")
Out[25]:
In [26]:
,max go to the head of the class
Out[26]:
In [27]:
output =! wc -w *.txt
output
Out[27]:
In [28]:
type(output)
Out[28]:
In [29]:
output.s
Out[29]:
In [30]:
print(output.s)
In [31]:
output.fields()
Out[31]:
In [32]:
smallest = 10000000000000
for (char_size, file_name) in output.fields():
print(file_name + " has " + char_size + " characters")
size = int(char_size)
if size < smallest:
smallest = size
print("Files as small as " + str(smallest) + " characters!")
In [33]:
def wordcount(filename):
output =! wc -w {filename}
return int(output[0].split()[0])
wordcount('bio.txt')
Out[33]:
In [34]:
textfilenames = !ls *.txt
for filename in textfilenames:
words = wordcount(filename)
!echo {filename} has {words} words.
In [35]:
%quickref
In [36]:
%lsmagic
Out[36]:
In [37]:
%load?
In [38]:
%load??
In [39]:
pwd
Out[39]:
In [40]:
cd ~
In [41]:
cd /home/catherine/proj/opensourceshakespeare/
In [42]:
cd -0
In [43]:
cd --olf
In [44]:
cd --shakes
In [45]:
"ni!".upper()
Out[45]:
In [46]:
In[64]
In [47]:
Out[64]
In [48]:
%rerun 64
In [49]:
%logstart
In [50]:
!head ipython_log.py
In [51]:
cd --olf
In [52]:
!head 047-validator.sh
Complex multi-language process? Fold it into one annotated Notebook!
In [53]:
diskuse_raw = !du --max-depth=1 /home/catherine
In [54]:
diskuse_raw.fields()
Out[54]:
In [55]:
diskuse = [[int(line[0]), line[1]]
for line in diskuse_raw.fields()[:-1]
if int(line[0]) > 1000000]
In [56]:
diskuse
Out[56]:
In [57]:
bytes = [b for (b, n) in diskuse]
In [58]:
names = [n for (b, n) in diskuse]
In [59]:
names
Out[59]:
There are faster ways to extract the columns, but they're harder to understand
In [60]:
%matplotlib inline
import matplotlib.pyplot as plt
In [61]:
plt.pie([bytes for (bytes, name) in diskuse],
labels=[name for (bytes, name) in diskuse])
Out[61]:
In [62]:
class DiskUse(object):
def __init__(self, directory='.', max_depth=1, min_bytes = 1000000):
self.directory = directory
self.max_depth = max_depth
self.min_bytes = min_bytes
self.raw_data =! du --max-depth={self.max_depth} {self.directory}
self.data = [(int(line[0]), line[1]) for line in self.raw_data.fields()[:-1]]
self.bytes = [bytes for (bytes, name) in self.data if bytes > self.min_bytes]
self.names = [name for (bytes, name) in self.data if bytes > self.min_bytes]
def _repr_html_(self):
plt.pie(self.bytes, labels=self.names)
In [63]:
DiskUse('/home/catherine')
Out[63]:
In [63]: