🕹ī¸ Do Something Great! 😄

Tag: bash

  • Searching bash history when it’s in multiple files

    When I look for previous commands that I have ran in bash, I use history | grep COMMAND. That works well when you’re history is in one file, but I now save my history in multiple files from each shell and my simple command doesn’t work.

    With my history files saved in ~/.bash_history_log/ I needed a different solution. The one I came up with is grep -ri COMMAND ~/.bash_history_log/*. That seems awfully long, lets make it shorter. You can’t pass arguments to bash aliases, but you can to functions. In .bash_profile (or .bashrc, which ever runs for you by default) I added the following function:

    function searchhistory() {
        grep -ri "${1}" ~/.bash_history_log/*
    }
    alias shistory=searchhistory
    

    And now a simple shistory COMMAND is all it takes to find that command I ran a long time ago.

  • A bash script to check domain expirations

    I have a bad habit, and that’s of buying domain names. I have about 20, with several different registrars. If I was smart, I would consolidate them under one registrar, but, even then, checking expirations on the domains is a pain. I usually have autorenew turned on, but I still like to know when they are getting close to expiration.

    Before re-inventing the wheel, I did a Google search, and found Domain Expiration Check Shell Script. It worked pretty well, except for a few top level domains like .me which didn’t work. I modified the script, and just had to put some finishing touches on it today (.com, .net, and .org was broken). Check out the gist of the script on Github.

    I put all of my domains in a text file, and can then run it with ./domain-check -f mydomains.txt. It’s pretty cool.

  • Fixing my .taskpaper reset alias for bash

    There was a little problem with my alias to reset a .taskpaper list by removing all of the @dones. It didn’t remove any spaces before the @done, so each line would gain a space every day. This fixes it:

    alias rst="sed -i '' 's/ *@done\(.*\)//g'"
    

    Now it removes one or more spaces that could be before @done.

  • Using a Here Document with bash and MySQL

    This past week I worked on a bash script to create some input files from a MySQL database. My problem was trying to use multiple line MySQL statements, which was messing everything up. The solution was to use bash’s Here Documents. It’s a way to direct lines into a command. For me, the command looked like this:

    mysql DBNAME <<EOF
    SELECT *
    FROM table
    WHERE item = 'WhatIWant'
    EOF
    

    Everything between the two EOF would be submitted to MySQL. The next problem was that bash was trying to still interpret the MySQL statement which had backticks in it. To solve this issue, I learned that I needed to enclose the first EOF in quotes.

    mysql DBNAME <<"EOF"
    

    Bash then left my MySQL command alone, and all was right in the world.

  • Resetting a daily taskpaper list

    I have a couple daily taskpaper lists that by the end of the have an @done on each line and I need to reset it for the next day. Instead of trying to do a search and replace everyday, I added a bash alias to .bash_profile (or .bashrc):

    alias rst="sed -i '' 's/@done\(.*\)//g'"
    

    The extra '' after the -i is because OS X’s sed requires an extension for backups. Set it to nothing and OS X won’t create backups.

  • Saving bash shell history for multiple shells

    The history from the commands typed into bash are very useful from time to time, especially when I think “Oh, I’ll remember that command next time I need it”, and then I don’t. The problem with bash’s history are twofold:

    • It only saves history when the terminal exits cleanly, so if your connection drops or you have to kill the current process, you lose your history.
    • If you are running multiple terminals, or using a terminal multiplexer like tmux (my favorite!) or screen, the last bash to exit overwrites any other history with its history.

    So, after a little searching I’ve found a solution that works for me for now.

    # Save history
    #Set history suffix to the current terminal
    HISTSUFFIX=`tty | sed 's/\///g;s/^dev//g'`
    # All terminals' histories will be stored in separate files
    # in .bash_history_log. That folder will need to be created.
    HISTFILE="$HOME/.bash_history_log/bash_history_$HISTSUFFIX"
    # Time stamp the commands in the history file
    HISTTIMEFORMAT="%y-%m-%d %H:%M:%S "
    # Don't save duplicate commands
    HISTCONTROL=ignoredups:ignorespace
    # Control the size of the file
    HISTSIZE=1000
    HISTFILESIZE=5000
    # Bash will append to the history file
    shopt -s histappend
    # Bash will append to the history file after each time
    # the prompt is shown. This way it's always saved.
    PROMPT_COMMAND='history -a'
    

    Put that in your .bashrc and you’re good to go. Comments are welcome!

  • Resetting a .taskpaper file

    I use a couple of .taskpaper files for checklists of things that need to be done every day. In it, after a task is completed the task gets an @done tag added. But the next day I want to start fresh. I could do a find/replace, but sed at the command line works better:

    alias rst="sed -i 's/@done\(.*\)//'"
    

    Adding the above alias to my ~/.bash_profile adds the ability to reset a taskpaper file with a simple rst FILE.taskpaper. Under OS X, the -i parameter requires an extension for a backup file. You can simply have ” to set it to nothing and then a back up file won’t be created.