![]() |
|
Spaces home Kok-Yan's blogProfileFriendsBlogMore ![]() | ![]() |
|
Kok-Yan's blog7/17/2008 BSG Toaster
Lol, this just cracked me up when I saw it. It is actually availiable for sale at the NBC Universial store! 7/1/2008 Returning an array from a Bash functionFor those of you who are still learning Bash (including me...), I'm sure one of the things you would have asked yourself is "How on earth do I return an array from a bash function?". Well I've written a small script that will explain this: #!/bin/bash
IFS=$'\n\t'
function fnGo () {
array=(
a s d f
"gh ij" "kl mn"
)
echo "${array[*]}"
}
# -------- out - String variable --------
out=$(fnGo)
echo "\"out\" isn't an array: ${out[1]} - nothing"
echo $'\n'"\"out\" Works with iteration:"
for item in $out; do
echo "item:"$'\t'"$item"
done
# -------- out2 - An array --------
out2=($(fnGo))
echo $'\n'"\"out2\" now an array:"
for ((i=0; i<${#out2[*]}; i++)); do
echo "item $i:"$'\t'"${out2[i]}"
done
echo $'\n'"Though \"out2\" cannot be iterated anymore...:"
for item in $out2; do
echo "item:"$'\t'"$item"
done
Basically, it's exactly as you'd do for returning a single value from a function (use echo) - but, you need to make sure you surround the variable with quotes (in the function - Note you can only either choose to use an iterator method (out1), or an addressing method (out2), but not both - run the script and you'll see what I mean. Oh, one more thing (just as a tip for those who don't already know) - pay attention to your IFS variable (which determines how parameters are separated)! This is especially important if you're taking in quoted (escaped) command-line parameters that may have a space in them (such as file names) - in that case, I normally use "\n". MPlayer resume script (v2-alpha)
A while back I attempted to write a wrapper script for mplayer to resume a file. Unfortunately, that script was rather limiting where:
Now I have a new script that resolves the above issues! Unfortunately, there are some new issues with the new script:
Sounds cryptic? It'll be clearer after I've shown you some examples.
As with the previous script, an example usage (we'll assume we're trying to resume a file called "mediaFile.avi" at timecode 300, and the script is named "mp"): echo 300 > mediaFile.avi.txt mp mediaFile.avi Also as with the previous script, you'll probably want to enable the "statusline" display by adding "msglevel=statusline=9" into your mplayer config file (at "~/.mplayer/config" | "/etc/mplayer/config"), and ensure "quiet=0".
Now, to use a profile for all files (at the moment, it doesn't matter where you place "-profile" and "-ss" - they just get applied to every file...): mp -profile hd file1.avi "file 2.avi"
However, the real magic of the v2 script comes into play when you have multiple files, matching multiple profile switching definitions, with different resume files! For example: $ ls -d *.avi*
file1.mts
file1.mts.txt
2x03 SomeEpisode.mkv
2x03 SomeEpisode.mkv.txt
file2.avi
$ less mp
...
profiles=(
# [PF name] [RegExp]
"hd" "(/|^)[0-9]{,2}x[0-9]{,2} .*\.mkv$"
# ---- Generic profiles ----
".mts" "\.mts$"
)
...
$ mp file1.mts "2x03 SomeEpisode.mkv" file2.avi
The script will now use the ".mts" profile for the mts file, the "hd" profile for the .mkv file, the "global" profile for the .avi file, whilst picking the values out of the resume files and resume at the point specified in the .txt files! Anyway, you can get the script here. Of course, if anyone's interested in improving it, they're more than welcome to do so! Just don't forget to email me a copy! 6/27/2008 Restoring SMB sharing on XPA couple months ago, my brother's PC suddenly stopped sharing files via SMB (port 445). Since my brother told me he hasn't done anything, I was a bit mystified as to what exactly is going on. Initially I though it was a firewall problem - so I checked the firewall config and tried to access the share from localhost (on his PC), which failed as if nothing is shared (i.e. "net view \\127.0.0.1" just times out). I then tried "netstat -an" to see if the SMB server is actually listening for a connection, and it turns out nothing is listening on port 445! A restart of lanmanserver and lanmanworkstation (services) and a reboot didn't help, so in a last ditch attempt to get something shared I enabled NetBIOS over TCP/IP, which seems to have done the trick (i.e. I can now access the shares). However, port 445 is still no where to be seen in "netstat -an"... After a lot of searching on Google, I found this article which more less describes the problem I've been having. After turning off NetBT, applying the registry fix (adding HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT\Parameters:SmbDeviceEnabled = 1) and rebooting (I had also re-installed "File and Printer sharing" at the time), the shares were now accessible again via port 445. Unfortunately, I never got to the bottom of what caused this problem - seeing it wasn't simply a missing registry value, as all my other systems don't have the SmbDeviceEnabled value. But I guess I can consider this one being resolved via a work-around as sharing is back to normal... 5/19/2008 Re-partitioning XubuntuNote: if you prefer the bare-essential steps and code, please jump straight to the "The re-partition process" section. Having used Xubuntu for a while now on my transition test rig, I've decided to re-partition the HDD to better suit my needs. Here's why I'm doing this:
However, the main drawback (and my original reason for not having a separate data partition) is that your HDD's capacity is effectively reduced (Data = HDD - OS - Swap), and not to mention when your OS partition runs out of your space, you're boned (alright, I know what you're thinking, but let's not get carried away with hacks like creating symbolic links to the data partition...). First thing first, let's figure out what the setup should look like and how much space we need, then we can explore different options when we encounter them. Partition, swap and hibernation configAs much I want to make a complete transition over to Linux, there are some situations where Windows is required (like certain applications and firmware updates), so we need a Windows partition. The best way to judge how much space is required is to look at the existing space usage of "%SystemRoot%" and "%ProgramFiles%" - you can use a tool like "diruse" (from the Windows XP support tools) or folder properties in explorer.exe... Windows: In a clean(ish) install of XP SP3, %SystemRoot% + %ProgramFiles% comes to about 2.7 GiB (say, 3GiB), and allowing 1 GiB each (yeah, I know it's rather generous) for MS Office, IDEs, .NET, JDK/JRE and AutoRoute the minimum size so far comes to about 8 GiB. Adding a 15% defrag free space requirement, overhead for old (un)installation files, system restore files, pagefile.sys, hiberfil.sys, and some free space, the minimum size for the Windows partition comes to 12 GiB (which also fits nicely onto 3 DVDs when imaged). It is worth point out that if you have a lot of RAM and want to hibernate your Windows session, you'd obviously want more space to accommodate hiberfil.sys. A safe suggestion would be the next multiple of 4, say, 16 GiB (if you have 3 GiB of RAM) (to fit onto 4 DVDs). As for page file, it's up to your judgement / Windows's to determine how much is needed - and if you have a second HDD, put the page file there instead. Linux: As for the Linux partition, it's a little bit more difficult to estimate (seeing I haven't really used it for that long) - but base install rounds up to about 3 GiB. For now, I've assigned 16 GiB to it as I plan on installing some of the software above using WINE (like MS Office), OpenOffice, IDEs. Though I'd probably use a 12 GiB partition when I partition my main system - but I'll just have to wait and see how it goes. As per the Xubuntu default setup, the OS(es) should reside on a primary partition of its own, with the rest being on extended, logical partitions - you can only have up to 4 primary partitions on a volume. As for swap and hibernation (suspend to disk), I've opted for a 512 MiB swap file and a 512 MiB swap partition dedicated for hibernation, for the 512 MiB of RAM (though there are usage problems with this setup - I'll talk about that next). The reason behind using a swap file is because it is more flexible (can be resized dynamically) with no reported performance difference under the 2.6 kernel comparing to a swap partition. As for the swap partition, it is used because there doesn't seem to be a way to configure Xubuntu's in-kernel hibernation implementation to use a hibernation file. The main problem with this setup is that hibernation would fail if the entire swap file had been filled and overflowed onto the hibernation swap partition. Unfortunately, swapoff-ing the swap partition will result in a hibernation failure.
Update: 2008-05-27 @ 03:09 Hacks aside, there are actually solutions to enable proper hibernation to a file - such as TuxOnIce, but it requires patching and re-compilation of the kernel - which is something I don't plan on doing in this project. While we're (kinda) still on the subject on partitions, there is something that can resolve the issues with partition sizes - LVM (Logical Volume Manager). In theory, pretty much everything (except "/boot") can be setup to use LVM, but I've decided against it because:
For now, I'll leave LVM alone until I need a heavy-duty file server with RAID (or equivalent) that runs only on Linux. File systemSo far, we've decided on the partition configuration and now, it's time to choose the file systems. Since the aim of this project was to re-partition the drive, I've decided to stick with Ext3 in Linux as I don't see any benefits in using another FS for my needs. As for Windows (XP), it's a no-brainer - NTFS (linux can mount that as ntfs-3g type). I have chosen Ext3 as the file system for the data partition as I will be using Linux primarily, and Windows XP can use the "Ext2 Installable File System for Windows" to access that partition. Pre-requisites and toolsWe're now almost at the point where we can start re-partitioning the drive - now that we know what the disk setup is going to be. We obviously need a re-partitioning tool, but we'd also need something to backup and restore the drive (in case anything horrible were to happen), and somewhere to store the HDD images. Not long ago, I came across the SystemRescueCd (possibly on Hak5 or this article on linux-mag.com (free subscription required) - can't remember exactly which...) which demoed the imaging and bare-metal restore capability of the live CD. As well as the partimage tool on the live CD, it also contains GParted (for re-partitioning) and samba (for smb-based network backups). But there are so many more tools on this CD it is definitely worth keeping in your toolbox. The re-partition processHere's the target partition configurations (for a single HDD setup): 12GiB NTFS (primary):
(Windows) C:\
16GiB ext3 (primary):
(Xubuntu) /
* (extended):
[*]GiB ext3 (logical):
/home/[user]/ # Replace [user] with the users
[RAM]GiB swap (logical):
hibernation "swap" partition == RAM size
The essential steps of this project are:
That's it! Simple eh? lol. Some links to the interesting articles: 5/9/2008 MPlayer resume script
I might as well share this - below are 2 scripts I've written for MPlayer to resume playback at a position saved in a text file. It works by appending the "-ss" parameter to mplayer when a resume file, in the format "[mediaFileName.ext].txt" is detected.
An example usage (we'll assume we're trying to resume a file called "mediaFile.avi" at timecode 300, and the script is named "mp"): echo 300>mediaFile.avi.txt mp mediaFile.avi
You'll probably want to enable the "statusline" display by adding "msglevel=statusline=9" into your mplayer config file (at "%ProgramFiles%/MPlayer/mplayer/config" | "~/.mplayer/config" | "/etc/mplayer/config"), and ensure "quiet=0".
Windows installation: @echo off setlocal ENABLEDELAYEDEXPANSION set mplayerbin=%ProgramFiles%\MPlayer\mplayer.exe set cli= FOR /F "usebackq delims==" %%c IN (`echo %*^|grep -c -i " \-profile "`) DO set count=%%c if !count! == 0 ( set cli="!mplayerbin!" -profile global %* ) else ( set cli="!mplayerbin!" %* ) REM Resume stuff - this'd only work if filename is specified as first argument. FOR /F "usebackq delims==" %%c IN (`echo %*^|grep -c -i "\-ss"`) DO set count=%%c set resumeFile=%1.txt if exist "!resumeFile!" ( if !count! == 0 ( FOR /F "usebackq delims==" %%c IN (`head -n 1 "!resumeFile!"`) DO set pos=%%c set cli=!cli! -ss !pos! ) ) echo ^>^>^>^> !cli! !cli! endlocal
Linux installation: #!/bin/bash
# Script to launch mplayer and resume if [filename.ext].txt exists
# TODO - Integrate the new parser into the old windows script below
mplayerbin=mplayer
# Unfortunately, something simple like: for i in $*; do echo $i; done
# doesn't work with spaces (even when it's escaped) - it'd separate parameters out by space...
# This also assumes the first argument with a space is the filename
argc=$#
i=0
while ((i < argc)); do
# escape spaces - can't use <code>echo $1|grep "\ "</code> or it'll display the params...
if [ "$(echo $1|grep -cE "[ ()$]")" == 1 ]; then # Any others?
argv[$i]=\"$1\"
else
argv[i]=$1
fi
shift; ((i++))
done
cli=${argv[*]}
# Old windows port of the script below
count=$(echo $cli|grep -c -i " \-profile ")
if [ $count == 0 ]; then
cli="$mplayerbin -profile global $cli"
else
cli="$mplayerbin $cli"
fi
# Resume stuff - this'd only work if filename is specified as first argument.
count=$(echo $cli|grep -c -i "\-ss")
resumeFile="${argv[0]}.txt"
# -f ensures the file (not directory) exist
if [ -f "$resumeFile" ] && [ $count == 0 ]; then
pos=$(head -n 1 "$resumeFile")
cli="$cli -ss $pos"
fi
echo \>\>\>\> $cli
eval $cli
There is however a bug in the Windows version due to the use of delayed expansion - if you have a file with an exclamation mark, you'd need to escape it with "^^" (e.g. "^^!"). Also, it'd be a good idea to specify the filename as the first parameter - I haven't done any extension check to figure out which parameter the filename is at.
You can find more information about the command-line parameters for MPlayer at:
Update: 2008-07-10 @ 03:54 5/7/2008 XP SP3 is out!
Windows XP Service Pack 3 is out, you can get it at: 5/4/2008 Synaptic or Aptitude?Short answer: Aptitude!
Long answer (and the story): Here's a list of executables beginning with "k" before installation: kbd_mode kbdrate kill killall killall5 klogd koi8rxterm And here's the list I got after specifying complete removal of "kdenlive" and "kdenlive-data": kab2kabc kde-config kdostartupconfig kinstalltheme koi8rxterm kaddprinterwizard kded kfile kioexec kpac_dhcp_helper kbd_mode kdeinit kfmexec kio_http_cache_cleaner ksendbugmail kbdrate kdeinit_shutdown kgrantpty kioslave kshell kbuildsycoca kdeinit_wrapper khotnewstuff kio_uiserver kstartupconfig kcmshell kde-menu kill klauncher ktelnetservice kconf_update kdesu_stub killall klogd ktradertest kcookiejar kdontchangethehostname killall5 kmailservice kwrapper Turns out Synaptic forgot to remove the "kdelibs4c2a" and "kdelibs-data" package... Even after installing "deborphan", as well as following this advice to filter for orphaned packages, Synaptic still didn't present those 2 packages for removal. Aptitude on the other hand took care of all the dependencies on installation and removes the 2 extra packages upon removal! So next time when you want to install a package using Synaptic, or see a command like: sudo apt-get update && sudo apt-get install some_package Make sure you use Aptitude UI or the following command instead, so you can uninstall with Aptitude later: sudo aptitude update && sudo aptitude install some_package Note: This was tested on Xubuntu 8.04 so I'm not using an outdated version of Synaptic / apt-get.
Oh, just in case you are interested in my video editor of choice (for now at least...) - it's Kdenlive! 4/30/2008 Save and restore iptables over reboot (making it persistent) using upstartAs you may already know, the configurations of iptables disappears after a reboot. My solution below is to create 2 scripts to run on shutdown and startup to save and restore the iptables rules. (Note I'm using upstart as opposed to SysVinit to ensure future compatibility) You'll need to login as root ("su"), then go to "/etc/event.d" and create the following 2 files (name it whatever you want): iptables_save: # Save the iptables entries on shutdown start on runlevel 0 # Shutdown start on runlevel 6 # Reboot script SCRIPTDIR="/var/tmp/sys" SCRIPT="iptable_rules.txt" if ! test -d "$SCRIPTDIR"; then mkdir "$SCRIPTDIR"; fi iptables-save > "$SCRIPTDIR/$SCRIPT" end script iptables_restore: # Restores the iptables entries on startup / create secure default if file not present # Debian doesn't distinguish between 2 --> 5, i.e. == 2 start on runlevel 2 script SCRIPTDIR="/var/tmp/sys" SCRIPT="iptable_rules.txt" if test -f "$SCRIPTDIR/$SCRIPT"; then iptables-restore < "$SCRIPTDIR/$SCRIPT" else # Block * incoming, allow associated outgoing connections # Flushes all chains iptables -F # Delete all chains iptables -X # Default policies iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT DROP # Allow related connections - new connections are allowed in following block iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow new connections be made - Allow localhost --> any iptables -A OUTPUT -m state --state NEW -j ACCEPT fi end script The second part of iptables_restore is meant to restore a set of secure(ish) default rule (as opposed to iptables' default of allowing everything...) if no saved rules exist. Replace it with your own defaults if mine suck (and drop me a comment saying why!). Please evaluate the script before using it - I've only tried this on my Xubuntu 8.04 setup. As usual, if you have any suggestions / comment about this, I would love to hear from you!
Just in case you are interested, here's a great article on linux.com about upstart: 3/21/2008 Windows Command Prompt (cmd) behaviour emulation in Linux terminal (Bash)![]() Update 2008-03-25 @ 00:32: This post was originally titled "Emulate Command Prompt (cmd) tab auto-completion behaviour in Linux terminal (Bash)" (originally posted on 2008-03-20 @ 23:45), and have since been updated to cover more grounds. Being a long time Windows command prompt (cmd) user, there are 2 things that the Linux terminal really annoyed me - tab auto-completion and case-sensitivity. For instance, if you have "file-1.txt", "file-2.txt" and you do "fi[tab]", you'd get "file-", instead of "file-1.txt". And if you try doing something like "ls -l File*" you wouldn't get anything returned... Luckily, you can change the above behaviour by editing 2 files. Tab-completion:In order to get the windows command prompt's tab-completion behaviour, you can edit / adding the following to "/etc/inputrc" (or "~/.inputrc"): # Tab TAB: menu-complete # Shift-Tab (reverse menu-complete) "\e[Z": "\e--\t" # Ignores the case of the letter set completion-ignore-case on According to the blog entry where I got this from, the "--" part of the Shift-TAB entry acts as an argument to the menu-complete function. In this case, it takes the previous entry of menu-complete. Also, to enhance the terminal, you can also make the following amendments: # The old style auto-complete (now mapped to Ctrl+space) - so you can still use it... Control-SPACE: complete # Automatically show everything instead of you having to press Ctrl+Space (as configured here) twice... set show-all-if-ambiguous on set show-all-if-unmodified on # Display a "/" at the end for symlinked directories set mark-symlinked-directories on # Shut the bell up... set bell-style none Case-insensitive expansion ("globbing"):If you want "ls -ld File*" to also display "file-1.txt", "file-2.txt", as well as "FiLe.txt" and "FILE.txt", you can add the following to "/etc/bash.bashrc": # Disable case sensitive expansion - for command like "ls ld a*" to list "A_file"
# Note for things like "ls -ld a_file.txt", we still need to force it to glob by "ls -ld [a]_file.txt"
shopt -s nocaseglob
However, as mentioned in the comments, listing a complete filename is still sensitive - you'd have to force it to glob by doing "ls -ld [a]_file.txt". If you know of any other useful modifications, drop me a comment!
|
||||||||||||||||