The Archives

  • 10.Aug.17
    Interlacing file lines shell | fernape | (0)
    Recently I needed to mix several files into one single file. The point was that I needed to interlace the files so I had all the first lines of every file followed by all the second lines and so on. Put it simply, this is what I had: fapesteguia@fapesteguia:~/test$ cat file1 file 1 row 1 file 1 row 2 file 1 row 3 file 1 row 4 file 1 row 5 file 1 row 6 file 1 row 7 file 1 row 8 file 1 row 9 file 1 row 10 fapesteguia@fapesteguia:~/test$ cat file2 file 2 row 1 file 2 row 2 file 2 row 3 file 2 row 4 file 2 row 5 file 2 row 6 file ...
  • 13.Jan.16
    Add timestaps to your bash history shell | fernape | (0)
    bash history is very useful to access your past commands. Sometimes it would be just great to know when a command was typed to be able to correlate it to another event. Imagine we know that there was a change in a repository that might have fixed a heisenbug. But we know that not long ago, we saw the bug again. But are we sure it was before the change in the repository was made? We can solve this (and other problems) by adding time stamps to our bash history. Add the following line to your .bash_profile file: export HISTTIMEFORMAT="%d/%m/%y %T " The ...
  • 04.Apr.15
    String manipulation in Bash shell | rafacas | (0)
    I rely on Bash on much of my scripting needs although it is usually not worthy when you need to do something complicated. However, we usually think Bash limits are lower than they really are. In this post I am going to explain the surprising number of string manipulation operations that Bash supports. String Length The syntax to know the lenght of a string is: ${#string} For example: $ string=abcdefghijklmnopqrstuvwxyz $ echo ${#string} 26 Substring Extraction ${string:position} Extracts substring from $string at $position. $ string=abcdefghijklmnopqrstuvwxyz $ echo ${string:1} bcdefghijklmnopqrstuvwxyz Note that strings are zero-based indexed. If you want to extract a substring of a specific length then: ${string:position:length} Extracts $length characters from $string at $position. For example: $ echo ${string:5:3} fgh It ...
  • 12.Oct.12
    Make colored output shell | fernape | (0)
    I found a nice script in stackoverflow about how to color the output of make in bash. It works by filtering the output and looking for certain expressions to highlight. Placing this function definition in your .bashrc file defines a bash make function which gets executed instead of the command. This way we can shadow the real make command and create our own custom version. make() { ccred=$(echo -e "33[1;31m") ccyellow=$(echo -e "33[1;33m") ccend=$(echo -e "33[0m") /usr/bin/make "$@" 2>&1 | sed -e "s/[Ee]rror:/$ccred&$ccend/g" -e "s/[Ww]arning:/$ccyellow&$ccend/g" return ${PIPESTATUS[0]} } Of course, you can use this ...
  • 19.Mar.10
    Shell scripting for Nautilus shell | fernape | (0)
    It has been a while since I wrote my last post. Sorry for the delay, but I was a bit busy lately. In this post, I shall explain how to get the most of your nautilus file manager by using shell scripts. Nautilus provides some facilities available from shell scripts. Combining them with a small utility called zenity can improve your daily tasks. Nautilus has the ability of executing shell scripts applying them to the selected files. The executable scripts are those present in the following directory: ~/.gnome2/nautilus-scripts Every script present in that directory will be shown in the "Scripts" entry of the contextual ...
  • 03.Sep.09
    Refreshing bash configuration cmd, shell | rafacas | (2)
    After writting some changes in the .bashrc file, you can reload it running this command: $ source ~/.bashrc or the even simpler $ . ~/.bashrc
  • 02.May.09
    Saving bash lines shell | rafacas | (0)
    Thanks to the short-circuit behaviour of the && operator, if-statements can be replaced by: [[ test ]] && if_true_do_this || otherwise_do_that So, instead of writing: if [[ "$1" == "$2" ]]; then echo "$1 equals $2" else echo "$1 differs from $2" fi One can write: [[ "$1" == "$2" ]] && echo "$1 equals $2" || echo "$1 differs from $2" Braces can be used to run more than a single command: [[ test_condition ]] && { true_stuff_1; true_stuff_2; } || { false_stuff_1; false_stuff_2; } Via | commandlinefu
  • 30.Apr.09
    Changing your login shell shell | fernape | (0)
    Yes, I have to admit this is the first thing I do on my account after installing FreeBSD :). This example however, comes from a Linux box (you may notice it due to the default installation paths for the shells). $ chsh -l /bin/ash /bin/bash /bin/csh /bin/tcsh The command above lists the available shells. (I have omitted some of the entries to shorten the list). $ chsh -s /bin/bash The next time you log in, your shell will be bash. Enjoy!
  • 20.Apr.09
    Monitoring HTTP requests from the command line network | rafacas | (0)
    Some days ago Javisantana sent me a tweet with a link to a one-liner HTTP requests monitor. It goes as follows with some ouput added: $ sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E "Host\: .*|GET \/.*" Password: tcpdump: listening on en1, link-type EN10MB (Ethernet), capture size 65535 bytes GET / HTTP/1.1 Host: GET / HTTP/1.1 Host: GET / HTTP/1.1 Host: GET / HTTP/1.1 Host: GET /intl/en_com/images/logo_plain.png HTTP/1.1 Host: [...] Replace en1 with your network interface's name. Usually enX in BSD-like OSes and ethX in Linux. You can create an alias named httpdump and add it to ~/.bash_profile: alias httpdump='sudo tcpdump -i en1 -n -s 0 -w ...
  • 08.Mar.09
    System information cmd, shell | fernape | (0)
    $ echo $OSTYPE $MACHTYPE freebsd7.1 amd64-portbld-freebsd7.1 Easy way of getting information with bash