360 likes | 555 Views
Windows Batch VS Linux Shell. Jason Zhu. Agenda. System and Shell Windows batch and Linux Shell Dos and Linux Shell internal Commands Windows and Linux external commands Batch and Shell variable and special sign Batch and Shell Key words
E N D
Windows Batch VS Linux Shell Jason Zhu
Agenda • System and Shell • Windows batch and Linux Shell • Dos and Linux Shell internal Commands • Windows and Linux external commands • Batch and Shell variable and special sign • Batch and Shell Key words • build forge VS batch/shell • Batch/Shell VS Perl/python
Shell history • Shell: a piece of software that provides an interface for users • Unix/Linux: Bourne/Korn/C/Tc/Bash • Linux GUI: Gnome,kde,xfce • Windows: cmd.exe/explorer.exe • http://en.wikipedia.org/wiki/Shell_(computing)
The difference of Windows and Linux • Case sensitive in Linux • / in Linux • No relationship between executable file and extension • don't search current path • Security policy is more strict in Linux • link file VS shortcut • http://en.wikipedia.org/wiki/Comparison_of_Windows_and_Linux
Where to get more useful tools • http://unxutils.sourceforge.net/ • ftp://ftp.microsoft.com/reskit/ • http://www.microsoft.com/mspress/windowsserver2003reskit/ • http://live.sysinternals.com/ • http://www.nirsoft.net • Other such as zip/unzip…
Robocopy(windows) • windows copy/xcopy: • copy “c:\new folder\1.txt” c: • copy “c:\new folder\1.txt” c:\2.txt • Linux cp/windows cp.exe • cp ~/a.txt ~/test (cp c:\1.txt c:\test2) • cp ~/a.txt ~/test/b.txt (cp c:\1.txt c:\3.txt) • Windows robocopy: • robocopy /E /NP /R:12 /w:10 /MIR SourceDirDestinationDir *.txt *.doc /XF test.txt /xd test • Must check the return value
Printf(windows/Linux) • Printf • Printf %3d 2 • printf %-3d 2 • Printf %03d 2 • Printf %1.2f 3.1415926 • printf %8s "aaaa" • printf "\nSubDoc: %03d\n" 3
Expr(windows/Linux) • expr 5 + 7 expr 5 – 6 • expr 5 ”*” 4 expr 5 / 7 • expr 7 % 9 • expr 1 “|” 0 expr 1 “&” 0 • expr 3 “>” 6 expr 3 “<” 6 • expr 3 “>=” 3 expr 3 “<=” 3 • expr 3 = 3 expr 3 != 3 • exprexpr1 :re expr index expr1expr2 • exprsubstrexpr1expr2expr3 • expr length "abcdef" • expr length "abcdef" "<" 5 "|" 15 - 4 ">" 8
Awk(windows/Linux) • awk '{print $0}' scores.txt • awk '{if($2 ^> "60") print $0}' scores.txt • awk 'BEGIN {print "start..."} {tot+=$2} END {print "totoal is:" tot; print "END..."}' scores.txt • awk '{print length($2)}' scores.txt
sed • sed [选项] s e d命令 输入文件。 • sed = test3.txt • sed s/mapguide/mapguide2010/g test3.txt • sed 5,6d test3.txt • sed 3a\abcd test3.txt • sed 10i\adflajflad\n\adfadfajdlf test3.txt • Sed 10c\aaaa test3.txt • sed 4q test3.txt • sed 2r 1.txt test3.txt
grep • grep "map" test4.txt • grep "map" *.txt • grep -c “aud“ test4.txt • grep -n “aud“ test4.txt • grep -v “aud“ test4.txt • grep -i “aud“ test4.txt
Environment variables shift
Linux 参数变量 • a. 变量=${参数-word}:如果设置了参数,则用参数的值置换变量的值,否则用word置换。即这种变量的值等于某一个参数的值,如果该参数没有设置,则变量就等于word的值。 • b. 变量=${参数=word}:如果设置了参数,则用参数的值置换变量的值,否则把变量设置成word,然后再用word替换参数的值。注意,位置参数不能用于这种方式,因为在Shell程序中不能为位置参数赋值。 • c. 变量=${参数?word}:如果设置了参数,则用参数的值置换变量的值,否则就显示word并从Shell中退出,如果省略了word,则显示标准信息。这种变量要求一定等于某一个参数的值。如果该参数没有设置,就显示一个信息,然后退出,因此这种方式常用于出错指示。 • d. 变量=${参数+word}:如果设置了参数,则用word置换变量,否则不进行置换。
变量延迟 If/then/else
Linux- Until/while/case/break/continue • while CONTROL-COMMAND; do CONSEQUENT-COMMANDS; done • until TEST-COMMAND; do CONSEQUENT-COMMANDS; done • case EXPRESSION in CASE1) COMMAND-LIST;; CASE2) COMMAND-LIST;; ... CASEN) COMMAND-LIST;; esac • select WORD [in LIST]; do RESPECTIVE-COMMANDS; done • Break/continue
Build Forge vs batch and shell • variables definition : build forge syntax • Variables using: both of windows and Linux • Slashes: both of them are ok • For linux OS: shell syntax • For Windows OS: batch syntax
Batch and shell VS perl and python • Shell simply and suitable for daily work • Perl/python can work in different os; more powerful; but complex;
Shell and batch references • http://technet.microsoft.com/zh-cn/library/cc758944(WS.10).aspx • http://www.arachnoid.com/linux/shell_programming.html • http://www.freeos.com/guides/lsst/ • http://linuxcommand.org/
Batch and shell books • Microsoft Windows Shell Script Programming for the Absolute Beginner.chm • 批处理阶段教程奥运最终版[英雄出品] • Bash.新手指南.中文版.(Bash.Beginners.Guide.CHS).CHM • Learning the bash Shell, 3rd Edition.chm • UNIX Shells by Example Fourth Edition.chm
Q&A • Thanks