Monthly Archives: January 2009

Emacs 简单脚本

#!/usr/local/bin/emacs --script
;;; emacs-script.el --- a simple emacs lisp script
 
;;; Commentary:
;; #!(shebang),从 Emacs 22 开始支持。这让 Emacs 也可以用来写类似
;; Bash 的脚本。处理文本文件,用 Emacs Lisp 脚本,应该是很强大的。当
;; 然,据说 perl 才是文本处理之王,但作为一个编辑器,Emacs 在文本处理上
;; 也有其优势的。
 
;; 一个简单例子,可以处理命令行参数。
;; 打开一个文件,进行一些文本处理,然后保存。
 
;;; Code:
;; handle command line arguments
(if (/= 1 (length command-line-args-left))
    (and (print "just need one argument for filename") (kill-emacs 1)))
(setq filename command-line-args-left)
 
;; open the file
(find-file (car filename))
 
;; processing it
;; indenting
(indent-region (point-min) (point-max))
 
;; filling
;; (fill-region (point-min) (point-max))
 
;; trailing white space
(delete-trailing-whitespace)
 
;; save the file
(save-buffer)
 
;;; emacs-script.el ends here
Leave a comment

Read command-line arguments with Perl

#!/usr/bin/perl -w
use feature ':5.10';
use strict;
use warnings;
 
# How do I read command-line arguments with Perl?
 
# http://perldoc.perl.org/perlvar.html
# The array @ARGV contains the command-line arguments intended for the script.
# $#ARGV is generally the number of arguments minus one, because $ARGV[0] is
# the first argument, not the program's command name itself. See $0 for the
# command name.
 
# $0, script file name.
# @ARGV 
# $ARGV[0] .. $ARGV[9]
# $#ARGV, the last argment's subscript
# $#ARGV+1, the total 
 
 
say $0;
say @ARGV;
say $#ARGV;
 
my $numArgs = $#ARGV + 1;
 
say "you gave me $numArgs command-line arguments.";
 
for my $subscript (0 .. $#ARGV){
	say "$ARGV[$subscript]";
}
 
# @INC is include paths.
# say @INC;
for my $inc (@INC){
	say $inc;
}
 
# %ENV is the enviroment.
# say %ENV;
while ((my $key, my $value) = each %ENV) {
	say "$key=$value";
}
Leave a comment