Showing posts with label Shell. Show all posts
Showing posts with label Shell. Show all posts

Saturday, October 11, 2008

XML operation using javascript E4X from Rhino javascript shell

Firstly, download Rhino and install it. Here is a tutorial.
Then, run below command line:

$> java org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 1 2008 03 06
js> var user = <user id="1"><name type="firstname">Yu</name><name type="lastname">test</name><country>zh_CN</country></user>;
js> user
<user id="1">
<name type="firstname">Yu</name>
<name type="lastname">test</name>
<country>zh_CN</country>
</user>
js> typeof a
undefined
js> typeof user
xml
js> user.name
<name type="firstname">Yu</name>
<name type="lastname">test</name>
js> user.country
zh_CN
js> user.id

js> user.@id
1
js> user.@id = 100
100
js> user
<user id="100">
<name type="firstname">Yu</name>
<name type="lastname">test</name>
<country>zh_CN</country>
</user>
js> user.name[0]
Yu
js> user.name += <name type="nickname">_test</name>;
<name type="firstname">Yu</name>
<name type="lastname">test</name>
<name type="nickname">_test</name>
js> user
<user id="100">
<name type="firstname">Yu</name>
<name type="lastname">test</name>
<name type="nickname">_test</name>
<country>zh_CN</country>
</user>
js> user.name[1]
test
js> user.name[1].@type
lastname
js> for each(var n in user.name) print("The " + n.@type + " name is " + n);
The firstname name is Yu
The lastname name is test
The nickname name is _test
js> for each(var n in user..name) print("The " + n.@type + " name is " + n);
The firstname name is Yu
The lastname name is test
The nickname name is _test
js> quit();

The user..name selector just like the "//" operator in XPath.
$> cat echo.js
for (i in arguments) {
print(arguments[i])
}

$> java org.mozilla.javascript.tools.shell.Main echo.js test yu from google
test
yu
from
google

Reference:
http://developer.mozilla.org/en/Rhino_Shell

Friday, August 31, 2007

CentOS 默认语言设置

如果安装了中文为默认语言时,centos语言编码是“zh_CN.UTF-8”,在本机上的SHELL是能看到正常的中文的,但是通过SSH连上来就会发现所有的汉字变成乱码了。
编辑/etc/sysconfig/i18n,修改“LANG="zh_CN.UTF-8"”为“LANG="zh_CN.GB18030"”,重新登录即可。

Thursday, July 26, 2007

Rhino javascript shell

firstly, download mozilla rhino.
$> unzip rhino.zip
$> cd rhino
$> export CLASSPATH=$CLASSPATH:/${toYourRhinoPath}/js.jar
$> java org.mozilla.javascript.tools.shell.Main
($> java -classpath js.jar org.mozilla.javascript.tools.shell.Main)
js> quit();

Sunday, May 27, 2007

Useful Shell Shortcuts

from Nuby on Rails by topfunky

A few weeks ago someone asked me to recommend a few useful books on Ruby. The first one? Using csh & tcsh. I spend a lot of time in the terminal and these shortcuts help in both tcsh and bash.
Reuse previous arguments

The ! operator gives you a quick way to refer to parts of the previous command.
!! (Full contents of previous command)

How many times have you tried to edit a file, then realize that you need to be root to do it? Use bang-bang to quickly repeat the previous command, with other commands before or after.

emacs /etc/init.d/mongrel_cluster
=> Permission Denied
sudo !!
=> Now opens the file as root

!$ (Last arg of previous command)

Sometimes I need to reuse the last argument with another command, like here where I forgot to quote a string.

wget http://weather.yahooapis.com/forecastrss?p=98117
=> wget: No Match
wget '!$'
=> Now it works

!^ (First argument of previous command)

I rarely use this, although it could have been used in the previous tip.

echo fish and chips
echo !^
=> fish # First argument

!:1 (Argument by number)

!:1 is the same as !^. The difference is that you can reference any element of the previous command.

echo fish and chips
=> fish and chips
echo !:1
=> fish # Same thing as !^

echo fish and chips
echo !:2
=> and # Word 2 in previous command, zero-indexed

echo fish and chips
echo !:0
=> echo # The very first word in the previous command

echo fish and chips
echo !:1-3
=> fish and chips # A range

!pattern (Repeat last command in history with pattern)

The bang is useful for re-running a command that you’ve run before. Spell out the first few letters and hit ENTER (or TAB to show the completion in tcsh). The shell will search backwards in your history until it finds a command that starts with the same letters.

I like the tcsh behavior since you can hit TAB to see what you’re asking for. Using this in bash can be more adventurous since you don’t know what command will be run until you hit ENTER.

rake test:recent
...
!rak
=> Runs 'rake test:recent' or last command starting with 'rak'

Sets
{a,b} (A set)

How often to you rename just part of a file? The {} syntax is convenient.

mv file.{txt,xml}
=> Expands to 'mv file.txt file.xml'

mv file{,.orig}
=> Expands to 'mv file file.orig'

mkdir foo{1,2,3}
=> Expands to 'mkdir foo1 foo2 foo3'

Mac OS X-specific
pbcopy and pbpaste

In Mac OS X, you can copy things to the clipboard and read them back out. This is nice because you can reuse it in the shell or back in the OS with Apple-C or Apple-V.

./generate_random_password | pbcopy

pbpaste > file.txt