Trim file extension using basename








see this post.


Shell Script: remove file extension








  • Use sed:
    echo "file.txt" | sed 's/\(.*\)\..*/\1/'
    
  • Use bash string operations:
    f="filename.txt"; echo "${f%.*}"
    



Shell Script: get file extension








See this post.


Java: ++i or i++ in for loops








See this post.


Parcellite: Ubuntu Clipboard Manager








See this post.


Java: Trim characters in a string








see this post.


ghex: a hex editor for Ubuntu Linux (Gnome)








  1. To install ghex:
    sudo apt-get install ghex
    
  2. To start ghex:
    ghex2
    or "Applications" -> "Programming" -> "Hex Editor"



ibus icon disappeared in the system tray








Running Ubuntu lucid 10.04 LTS. Installed and enabled ibus as input method engine.

ibus icon disappeared in the system tray since last system restart. And when Ctrl + Space to input Chinese, no input method window is shown though you can input Chinese.

The solution is run the following command in the terminal window:

ibus-daemon -x -r -d



Eclipse: Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not accessible due to restriction on required library








Imported some source code in a Eclipse java project, and got error:

Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not accessible due to restriction on required library /usr/lib/jvm/java-6-sun-1.6.0.24/jre/lib/rt.jar

The fix it:

  1. Right-click the project, and click "Properties"
  2. open "Java Compiler", 
  3. select "Errors/Warnings",
  4. "Enable project specific settings", 
  5. open "Deprecated and restricted API"
  6. change to "Warning" for "Forbidden reference(access rules)"



Shell Script: split string using cut








# the string is dot separated.
a=1a.2b.3c.4d.5e.6f

# split it and prints 6th column
echo $a | cut -d'.' -f6 

# prints 6f


See Also: [Shell Script: Split String Using tr and awk]


Shell Script: count the occurences of a specific character








#!/bin/bash

# dot-separated string
str=1.2.3.4.5.6

# prints the number of occurences of dot
echo $str | tr "\." "\n" | wc -l

# prints 5



Shell Script: split string tr and awk








# the string is dot separated.
a=1a.2b.3c.4d.5e.6f

# split it and prints 6th column
echo $a | tr "\." "\ " | awk '{print $6}'

# prints 6f


See Also: Shell Script: split string using cut


Shell Script: How to parsing server address string








#!/bin/bash

# $1 is wilson@site90.net:8000
i=`expr index $1 @`
user=${a:0:$((i-1))}
echo $user

j=`expr index $1 :`
host=${1:$i:$((j-i-1))}
echo $host

port=${1:$j:((${#1}-j))} 
echo $port

See this post for detail.


Shell Script: Get the last argument passed to the shell script








  • Method 1:
    last=$(eval "echo \$$#")
    
  • Method 2:
    last=${@:${#@}}
    
  • Method 3: Requires BASH 3.0+
    last=${!#}
    
  • Method 4: Requires BASH 3.0+
    last=$BASH_ARGV
    
  • Method 5: Portable solution. It uses the fact that for implicitly loops over the arguments if you don’t tell it what to loop over, and the fact that for loop variables aren’t scoped: they keep the last value they were set to.
    for last; do true; done
    



Backup your web site using wget (ftp)








wget --output-file=wget.log  --tries=2  --mirror  --ftp-user=YourUserName  --ftp-password=YourPassword   ftp://168.site90.net


Here is a wrapper script:
#!/bin/bash
usage() {
        echo ""
        echo "`basename $0` -u <ftp_user> -p <ftp_password> <ftp_address>"
        echo ""
}

for FTP_SITE; do true; done
until [[ -z $1 ]]
do
        case "$1" in
                "--user" | "-u" )
                FTP_USER=$2
                shift
                shift
                ;;
                "--password" | "-p" )
                FTP_PASSWORD=$2
                shift
                shift
                ;;
                * )
                shift
                ;;
        esac
done

if [[ -z $FTP_SITE ]]; then
        echo "ERROR: invalid arguments. No ftp address specified."
        usage
        exit 1
fi


if [[ -z $FTP_USER ]]; then
        echo "ERROR: invalid arguments. No ftp user specified."
        usage
        exit 1
fi

if [[ -z $FTP_PASSWORD ]]; then
        echo "ERROR: invalid arguments. No ftp password specified."
        usage
        exit 1
fi

if [[ -z "`which wget`" ]]; then
        echo "ERROR: wget not found."
        usage
        exit 1
fi

wget --output-file=wget.log  --tries=2  --mirror  --ftp-user=$FTP_USER  --ftp-password=$FTP_PASSWORD   $FTP_SITE




HTML: Word wrap in <pre>








To enable word wrap inside <pre> you need the following CSS style:
pre
{
 white-space: pre-wrap; /* CSS2.1 compliant */
 white-space: -moz-pre-wrap; /* Mozilla-based browsers */
 white-space: -o-pre-wrap; /* Opera 7+ */
}

Example:
before applying white-space:pre-wrap:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
after applying white-space:pre-wrap:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.



Shell Script: upper case to lower case or vice versa








  • Uppercase to lowercase:

    echo $VAR | tr '[:upper:]' '[:lower:]'
    

    or

    echo $VAR | tr '[A-Z]' '[a-z]'
    

  • Lowercase to uppercase:

    echo $VAR | tr '[:lower:]' '[:upper:]'
    

    or

    echo $VAR | tr '[a-z]' '[A-Z]'
    



Shell Script: substring








#!/bin/bash
str=abcdef
substr=${str:0:3} 
echo $substr # abc
substr=${str:3:3}
echo $substr # def
substr=${str:1:5} 
echo $substr # bcdef



File tests in Shell, Perl, Python and Ruby








http://blog.endpoint.com/2009/08/file-test-comparison-table-for-shell.html


Shell Script: read from keyboard input








See the example script below:
#!/bin/bash
read    -p "Username: " username
read -s -p "Password: " password

echo "$username"
echo "$password"

-s option can hide the input string, it is used to input passwords.


Bash script to check Java version








  • to check the java version:
    java -version 2>&1 | grep "java version" | awk '{print $3}' | tr -d \"
    

    or

    java -version 2>&1 | grep "java version" | awk '{print $3}' | sed -e "s/\"//g"
    
  • to check if the java version is greater than 1.5 (jdk 5), use the script below:
    #!/bin/bash
    VER=`java -version 2>&1 | grep "java version" | awk '{print $3}' | tr -d \" | awk '{split($0, array, ".")} END{print array[2]}'`
    if [[ $VER ge 5 ]]; then
        echo "Java version is greater than 1.5."
    else
        echo "Java version is lower than 1.5."
    fi
    



Create an ISO image from CD or DVD








  • Using cat
    • cat /dev/scd0 > /home/wilson/myfile.iso
    • cat /dev/cdrom > /home/wilson/myfile.iso
    • cat /dev/dvd > /home/wilson/myfile.iso
  • Using dd
    • dd if=/dev/scd0 of=/home/wilson/myfile.iso
    • dd if=/dev/cdrom of=/home/wilson/myfile.iso
    • dd if=/dev/dvd of=/home/wilson/myfile.iso
  • Using Brasero Disc Burner
    1. Open Brasero Disc Burner: Applications -> Sound & Video -> Brasero Disc Burner
    2. In the main window, select/click "Disc Copy"
    3. In the new window, set "Image File" below "Select a disc to write to"
    4. Click "Create Image" button
    5. It creates a .toc&.bin files can be converted to .iso file by following this guide.



Remove the line number from SyntaxHighlighter








I am using SyntaxHighlighter to display source code and scripts. In some cases, I want to hide/remove the line numbers.

To do that, the gutter attribute need to be set to false:

<pre class="brush: java; gutter: false;"></pre>

  • example 1: show line numbers: <pre class="brush: bash">cat /etc/issue</pre>
    cat /etc/issue
  • example 2: hide line numbers: <pre class="brush: bash; gutter: false;">cat /etc/issue</pre>
    cat /etc/issue



Convert Brasero .toc/bin Images into ISO format using cdrdao and bchunk








  • Install cdrdao and bchunk:
    sudo apt-get install cdrdao bchunk
  • Assume your .toc/.bin files are yourfile.toc and yourfile.bin, run the following commands:
    • toc2cue yourfile.toc yourfile.cue
    • bchunk yourfile.bin yourfile.cue yourfile.iso



Change Host Key in VirtualBox








1. Open VirtualBox, Select File -> Preferences

2. Select Input, click the Host Key area, the press the key you wish to be the new Host Key. (e.g. Left Ctrl), click OK button.



Unicode: UTF-8, UTF-16, UTF-32 & BOM








Name UTF-8 UTF-16 UTF-16BE UTF-16LE UTF-32 UTF-32BE UTF-32LE
Smallest code point 0000 0000 0000 0000 0000 0000 0000
Largest code point 10FFFF 10FFFF 10FFFF 10FFFF 10FFFF 10FFFF 10FFFF
Code unit size 8 bits 16 bits 16 bits 16 bits 32 bits 32 bits 32 bits
Byte order N/A big-endian little-endian big-endian little-endian
Fewest bytes per character 1 2 2 2 4 4 4
Most bytes per character 4 4 4 4 4 4 4

See Also: UTF-8, UTF-16, UTF-32 & BOM FAQ


老电影: 铁道游击队











老电影: 英雄儿女











老电影: 南海长城











Install Chinese Pinyin Input Method on Ubuntu Linux 10.04 (Lucid)








Ubuntu 10.04 (Lucid) LTS 安装中文输入法

1. System -> Administration -> Language Support

2. click "Install/Remove Languages..." button

3. select "Chinese(simplified)" and tick "Input methods", then click "Apply Changes" button. It will then start installing the fonts and input methods.

4. System -> Preferences -> IBus Preferences

5. enable "Input Method" tab, select "Pinyin" input method and click "Add" button. Once it is added, you can adjust its order by clicking "Up" or "Down" button.



Edit ISO Image in Ubuntu Linux








  • Install ISOMaster:
    sudo apt-get install isomaster
  • Applications -> Sound & Video -> ISO Master



Install FreeNX server on Ubuntu 10.04 (Lucid), 10.10 (Maverick) or 11.04 (Natty)








UPDATE: The instructions below works on Ubuntu 10.04, 10.10 and 11.04. However it does not work on Ubuntu 11.10. To install freeNX server on Ubuntu 11.10, see my another blog post: Install FreeNX server on Ubuntu 11.10 oneiric
  1. Enable the repository:
    sudo add-apt-repository ppa:freenx-team
  2. If you're using 10.10 Maverick, run
    sudo sed -i 's/maverick/lucid/g' /etc/apt/sources.list.d/freenx-team-ppa-maverick.list
  3. If you're using 11.04 Natty, run
    sudo sed -i 's/natty/lucid/g' /etc/apt/sources.list.d/freenx-team-ppa-natty.list
  4. Update the repository:
    sudo apt-get update
  5. Install FreeNX:
    sudo apt-get install freenx
  6. Download the setup script:
    cd /tmp;
    wget https://bugs.launchpad.net/freenx-server/+bug/576359/+attachment/1378450/+files/nxsetup.tar.gz;
    tar zxvf nxsetup.tar.gz
  7. Install the setup script:
    sudo cp nxsetup /usr/lib/nx/nxsetup
  8. Run the setup script:
    sudo /usr/lib/nx/nxsetup --install

See also: how to secure your FreeNX server