The following post is designed more to be a cheatsheet rather than being read, but hey please read if that’s your bag
Setting up and learning to navigate in your shell is paramount in becoming a productive developer.
DDG (DuckDuckGo) have cheatsheets see LICENCE and this one is for bash
Customizing your .bashrc
file is important to get the most out of your terminal experience. I need to walk through my dotfiles
This section has some boiler plate code I use quite frequently.
Here is a simple example of a script used to backup your home folder ($HOME
) to a compressed tar file which includes a timestamp in its filename. A useful alternative for a timestamp is date -Iseconds
, you can change your accuracy to your needs. Its also useful to replace non-alphanumeric characters which can be done with: date -Iseconds|tr -c '[A-z,0-9]' '-'
#!/bin/bash
TS=$(date +%Y%m%d)
OF=/var/my-backup-$TS.tgz
tar -cZf $OF $HOME
Next up is a little demonstration of how variables have scope - just as in a programming language. See this example of a function defining a locally scoped variable of the same name.
#!/bin/bash
HELLO=Hello
function hello {
local HELLO=World
echo $HELLO
}
echo $HELLO
hello
echo $HELLO
If statements (see man test
) can be executed in a number of ways. In a script it is most common to see it written something like this:
#!/bin/bash
T1="foo"
T2="bar"
if [[ "$T1" = "$T2" ]]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
Note the double square brackets are used here, for more information read this section in the LDP
However, conditional branching can be done in a number of ways, for example without the if statement at all:
[ -f myfile ] && cat myfile || echo "Not found"
Or, of course you can use the test command itself:
test -f myfile && cat myfile || echo "Not found"
For loops are very straight forward for command output:
#!/bin/bash
for i in $( ls ); do
echo item: $i
done
… or a simple sequence:
#!/bin/bash
for i in `seq 1 100`;
do
echo $i
done
This sort of sequence can be easily constructed in the terminal, for example here to output all the background colors you can use with tput
:
for f in `seq 1 256` ; do tput setab $f ; echo -n " COL:$f " ; tput sgr0 ; done
Loops are easy enough
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
#!/bin/bash -x
This can can be highly frustrating, but try this command to configure:
CFLAGS="-m32" LDFLAGS="-m32" ./configure --target=x86
Below is example code to stop processes with a particular name.
#!/bin/bash
TAG=<process-name-group>
how_many() {
n=`ps aux|grep $TAG|grep -v grep|wc -l`
}
how_many
echo "Found $n processes running."
if [ $n -gt 0 ]
then
ps aux | grep $TAG | grep -v grep | awk -F ' ' '{ print $2 }'| xargs kill -9
else
echo "No process running!"
fi
for (( ; n>0 ; ))
do
how_many
echo "Waiting for $n processes to stop..."
sleep 1
done
Simplest usage:
curl http://here.com
Usage with POST:
curl -X POST -d "username=test&password=test" http://here.com
Additional arguments to get any redirects (HTTP 302):
-L -w "Last URL: %{url_effective}"
Usage with Cookies, add the following arguments:
-c cookie.txt -b cookie.txt
Parse the output for the TOKEN
grep csrf cookie.txt|cut -f7
Now use this token to format your next request…
curl -X POST -c cookie.txt -b cookie.txt -d "username=tesr&password=test&csrfmiddlewaretoken=<<TOKEN_FROM_COOKIE_FILE>>" http://here.com