?

Log in

No account? Create an account

Originally published at Dom's Blog. You can comment here or there.

When using a slow connection such as via a mobile phone, even a command line prompt via ssh can be slow to type on at times.

The ‘rlwrap’ command works around the fact that each character sometimes take a long time to be echoed back to your client, by allowing you to prepare the line locally before hitting return whilst still allowing you to pass control characters.

In addition, the ‘autossh’ command will keep trying to automatically reconnect a remote login session whilst a connection is down, and ‘GNU Screen’ for multiplexing windows and being able to leave commands running on a system even whilst disconnected or not viewing the output, and you have a pretty powerful toolset.

Here is an example of how I connect to a remote system with these commands:

rlwrap autossh -M 22007 -t user@example.com 'screen -dr'
Tags:
 
 

Originally published at Dom's Blog. You can comment here or there.

The file ~/.bashrc can contain a definition of the variable PS1 which is used to determine the prompt used on a command line. Normally this would display the username, hostname, working directory and is often modified to include additional information such as the time of day the command completes, history number or exit code.

The exit code of a process is zero if the command has completed successfully, otherwise the number is determined by the application – these are normally documented in its manpage.

A function can be defined within the .bashrc file which prints different strings dependant upon the exit code of the previously executed application, and the prompt constructed to include this, but upon further testing an issue arises with the length of the line when the text being entered wraps, as the number of characters in the prompt is calculated incorrectly. The reason for this is that the escape codes used to change the colour within the prompt are assumed by bash to take up space, but in fact they do not. This can be worked-around by enclosing the variables expressing the colours in escaped square brackets, but the same is not true within the print command used within the exit-code dependant function – instead, the strings 01 and 02 are used to denote the non-printing colour changes:


Red="\33[0;31m"
Green="\33[0;32m"
Yellow="\33[0;33m"
Blue="\33[0;34m"
Magenta="\33[0;35m"
Cyan="\33[0;36m"
Normal="\33[m"

smiley() {
ret_val=$?
if [ "$ret_val" = "0" ]
then
printf "\001$Green02:)\001$Normal\002"
else
printf "\001$Red\002:(\001$Normal\002 ($ret_val)"
fi
}
PS1="\[$Magenta\]\u@\h\[$Normal\]:\[$Cyan\]\w \$(smiley) \[$Normal\]"

References:

1. http://superuser.com/questions/301353/escape-non-printing-characters-in-a-function-for-a-bash-prompt
2. http://www.gnu.org/software/bash/manual/bashref.html#index-prompting-257

Tags:
 
 
15 October 2011 @ 02:20 pm

Originally published at Dom's Blog. You can comment here or there.

OS X has no ‘rename’ command which can be used on some Linux distributions to apple a perl regular expression to modify the name of a file, so I cooked-up this line instead to change m4v filenames to mp4:

for i in *.m4v ; do mv "$i" "`echo $i | sed 's/m4v/mp4/'`" ; done

The three types of quotes are used to either protect strings which may contain spaces or, in the case of the backticks, execute commands and return the output. The single and double quotes are used separately since they are nested one within the other. Single quotes would not allow variables to be expanded within them if they were used on the outside.

 
 
13 September 2011 @ 03:06 pm

Originally published at Dom's Blog. You can comment here or there.

A few applications I’ve encountered recently:

  • Rember: GUI-based memory test tool
  • AppleJack: text-mode disk maintenance
  • DiskWave: measure disk space usage by files and directories
  • BetterTouchTool - add gesture actions for touchpad, magic mouse and more

 

Tags:
 
 
05 June 2011 @ 12:42 pm

Originally published at Dom's Blog. You can comment here or there.

Sometimes when files are downloaded by automated download scripts, or just saved directly from a website, the filenames contain ASCII characters encoded with the percentage symbol, e.g.:

This%20filename%20contains%20spaces.html

Whilst this is considered bad practice, it is sometimes unavoidable (for example when downloading content which is saved dependant on the title of a page, not the filename of the source), so I wrote this little script to rename each of the files in a directory:

#!/opt/local/bin/perl -w
# A script to replace the ASCII codes in filenames with their actual characters
# Note: no checking is done
if ($#ARGV < 0 ) {
print "usage: substitute_ascii.pl \n";
exit;
}

# Process each argument in turn, saving the original name
# then replacing each ASCII code with its actual character
foreach $value (@ARGV) {
$original = $value;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ( rename("$original", "$value") ) { print "$original -\> $value\n"; } else { print "Rename operation failed\n"; }
}

Tags: