tagged: unix

Git: Seeing the current branch in your command line

You can make a file in your home directory called .bash_profile if it doesn't already exist.
This also work on Mac OS X.
Then add the following to the bottom.

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

Then type source ~/.bash_profile.
Now your Teminal will tell you git what git branch you're in, if you're in a git repo.

unix git

A simple markdown blog using three lines of bash

Imagine you've got a load of markdown files such as post1.md etc.
And that you have a command, markdown, that converts markdown to html. (This exists: look it up).
This will convert all your markdown files to HTML and put them in a directory called tmp which you will have to create.

for i in `ls *.md`; do    markdown $i > tmp/`basename $i .md`.html; done

Now this next command will surround all your new HTML files with a <div class='entry'> tag and output the whole lot into entries.html.

ls tmp/* | xargs -n 1 awk 'BEGIN {print "<div class=\"entry\">" } {print $0} END {print "</div>"}' > entries.html

Finally create a template.html file with the lines <!-- insert here --> somewhere in the middle.
Then this final command will insert everything in entries.html into your template.html file and output the result into a new file, index.html

sed -e '/<!-- insert here/r entries.html' template.html > index.html

unix bash

Insert one file into the middle of another using Sed

Let's say you have a file called file1. And let's also say you have a file called file2.
And within file1 you have this text in the middle of your file: Yusef Lateef.
If you run this command,

sed -e '/Yusef Lateef/r file2' file1

Then below Yusef Lateef in file1 you will find the content of file2.

unix sed

Linux: Connect to open and closed networks from the command line with a single command.

You can easily connect to an open network though

iwconfig wlan0 essid "THE NETWORK NAME"

Closed networks work with wpa_supplicant. You normally send a configuration file with that but you can simulate the file with a named pipe:

wpa_supplicant -iwlan0 -c <(echo -e 'network={\n ssid="THE NETWORK NAME" \n psk="THE PASSWORD" \n}') -B

wlan0 is your network interface name. -B means go into the background. The \ns in the configuration file is needed sadly and so we pass -e to echo to interpret them.
iwconfig should show your new connection but prehaps only after a second.
After this dhclient wlan0 will get you a dhcp address and you'll be ready to go. You may want to kill any prior instances of dhclient.

unix wifi

Upload an image to an imgur album from the command line with curl

First make sure you have a client id through registering at https://api.imgur.com/oauth2/addclient
Then make a command to create an album:

curl --request POST   --url https://api.imgur.com/3/album   
--header 'authorization: Client-ID YOUR_CLIENT_ID'

This will return an anonymous public album. Note the 'deletehash' id in the below:


The empty album is available at https://imgur.com/a/SOME_ID
Now let's upload an image to that album. Because it's public and anonymous we need to use the deletehash as the album ID:

curl --request POST   --url https://api.imgur.com/3/image   --header 'authorization: Client-ID YOUR_CLIENT_ID'   
--header 'content-type: multipart/form-data;' -F "album=THE_DELETE_HASH" -F "image=@/PATH/TO/THE/IMAGE.png"

The json returned should say everything is okay. Upload a couple of images to that album and you should be able to see them at https://imgur.com/a/SOME_ID.
You can get a json response of all the images in that album through curl --request GET --url https://api.imgur.com/3/album/SOME_ID --header 'authorization: Client-ID YOUR_CLIENT_ID'. It will include the array of images: "images":[{"id":"ANOTHER_ID",... link":"https:\/\/i.imgur.com\/ANOTHER_ID.png"},...] The link property is the direct link to the image without all the imgur.com html around it.

unix imgur curl

Page 1 of 14