Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

2013-08-06

How to read input from a bunch of commands in bash

You can use the <( ... ) subshell syntax to read from a bunch of commands, that you whip up on the fly:
cat < <( echo "foo"; sleep 2; echo "bar" )
You can use this for example for accessing IMAP servers, which need a short delay during login.

2013-07-29

How to get Ctrl+Arrow working for programs running in tmux?

The key combination of Ctrl+arrow key is often used for skipping forward or backward whole words. This can be used in the bash command line, Emacs and many other programs. However, when I am using tmux, this will not work. You can fix this, by adding the following to your ~/.tmux.conf:

set-window-option -g xterm-keys on

This was explained in a nice superuser Q&A.
You can interactively try out tmux commands by hitting C-b : -- this will enter the command mode. You can use tab to complete commands.

2013-03-24

How to make the mpdas run as a daemon

The other day I installed the mpdas, which is the audio scrobbler for the music player daemon. Since there's no debian package for the Raspberry Pi, I compiled mpdas from scratch and installed it. Now I don't want to run it manually each time the Raspberry Pi boots up. So I found a nice template for writing your own debian-style init-script. I changed it a little and also installed the daemon tool, to turn the interactive mpdas program into a daemon. Just run apt-get install daemon to install it. Then put the following file under /etc/init.d/mpdas and run update-rc.d mpdas defaults. Then mpdas will be run automatically upon boot. Oh, one more thing: put your mpdas configuration under /usr/local/etc/mpdasrc or adjust the DAEMONOPTS in the init script accordingly.
#!/bin/bash
### BEGIN INIT INFO
# Provides:          mpdas
# Required-Start:    $remote_fs $syslog $mpd
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Audio Scrobbler for mpd
# Description:       Starts the Audio Scrobbler for the mpd music player daemon.
### END INIT INFO

DAEMON_PATH="/usr/bin/"

DAEMON=daemon
DAEMONOPTS="-u pi -r -X /usr/local/bin/mpdas"

NAME=mpdas
DESC="The mpdas audio scrobbler for mpd"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
printf "%-50s" "Starting $NAME..."
cd $DAEMON_PATH
PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
#echo "Saving PID" $PID " to " $PIDFILE
 if [ -z $PID ]; then
     printf "%s\n" "Fail"
 else
     echo $PID > $PIDFILE
     printf "%s\n" "Ok"
 fi
;;
status)
 printf "%-50s" "Checking $NAME..."
 if [ -f $PIDFILE ]; then
     PID=`cat $PIDFILE`
     if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
  printf "%s\n" "Process dead but pidfile exists"
     else
  echo "Running"
     fi
 else
     printf "%s\n" "Service not running"
 fi
;;
stop)
 printf "%-50s" "Stopping $NAME"
     PID=`cat $PIDFILE`
     cd $DAEMON_PATH
 if [ -f $PIDFILE ]; then
     kill -HUP $PID
     printf "%s\n" "Ok"
     rm -f $PIDFILE
 else
     printf "%s\n" "pidfile not found"
 fi
;;

restart)
$0 stop
$0 start
;;

*)
 echo "Usage: $0 {status|start|stop|restart}"
 exit 1
esac

2013-01-08

Quoting a set of parameters for program arguments using sed

I have a script which launches another program. That program takes some command line arguments, which in turn can have parameters. Those parameters may include ";", which has a special meaning in the Bash. To quote these, you can use the following sed command:
$ echo "foo --bar=1 --baz=2" | sed -e 's/\(--[^[:space:]]*=\)\([^[:space:]]*\)/\1"\2"/g'
foo --bar="1" --baz="2"

2012-09-08

Using Ctrl+Arrow keys in the OS X terminal

The key combination Ctrl + left or right arrow key can be used to go a word left or right in many programs such as GNU Emacs or the bash. In the default configuration of the OS X terminal, this is not the case. That's because Terminal.app sends the wrong key codes for bash's default configuration. So you just need to got to the preferences (cmd+,) and set the key codes for Ctrl+left and right to \033b and \033f respectively: