可移植性真的很重要:on shell and tr
来源:互联网 发布:淘宝货源一般怎么找 编辑:程序博客网 时间:2024/06/01 12:59
道理很简单,但是不小心造成的麻烦会很大!
关键词:shell, tr, regexp, [A-Z] vs. '[A-Z]'
1. Make sure where we are.
bash-3.2$ pwd
/home/zedware/test
2. What we have
bash-3.2$ ls -al
total 8
drwxr-xr-x 2 zhangw8 zedware 80 Apr 8 22:11 .
drwxr-xr-x 21 zedware zedware 3072 Apr 8 22:11 ..
3. Which shell
bash-3.2$ ps -p $$
PID TTY TIME CMD
27643 pts/0 00:00:00 bash
4. Convert uppercase to lowercase. It works?!
bash-3.2$ echo Bill | tr [A-Z] [a-z]
bill
5. Create a file x
bash-3.2$ touch x
bash-3.2$ ls -al
total 8
drwxr-xr-x 2 zedware zedware 80 Apr 8 22:12 .
drwxr-xr-x 21 zedware zedware 3072 Apr 8 22:11 ..
-rw-r--r-- 1 zedware zedware 0 Apr 8 22:12 x
6. Convert again. It works? NO!
bash-3.2$ echo Bill | tr [A-Z] [a-z]
Bill
7. Why?
[A-Z] and [a-z] are interpreted by current shell - bash as regular expression.
Where [a-z] matches x now.
sh-3.2$ echo Bill | tr [A-Z] x
Bill
8. Supress the regexp by ''. And it works.
bash-3.2$ echo Bill | tr '[A-Z]' '[a-z]'
bill
9. Or use the simpler syntax. It also works.
bash-3.2$ echo Bill | tr A-Z a-z
bill
10. Delete file x and try the tests.
bash-3.2$ rm -rf x
bash-3.2$ ls -al
total 8
drwxr-xr-x 2 zedware zedware 80 Apr 8 22:13 .
drwxr-xr-x 21 zedware zedware 3072 Apr 8 22:11 ..
11. '' still works.
bash-3.2$ echo Bill | tr '[A-Z]' '[a-z]'
bill
12. Also works.
bash-3.2$ echo Bill | tr A-Z a-z
bill
13. Now, try it in another shell - csh.
[zedware@fedora ~/test]$ ps -p $$
PID TTY TIME CMD
26144 pts/0 00:00:00 csh
14. It shows error message. That's better than slient.
[zedware@fedora ~/test]$ echo Bill | tr [A-Z] [a-z]
tr: No match.
15. '' still works.
[zedware@fedora ~/test]$ echo Bill | tr '[A-Z]' '[a-z]'
bill
16. Also works.
[zedware@fedora ~/test]$ echo Bill | tr A-Z a-z
bill
17. Create file x.
[zedware@fedora ~/test]$ touch x
18. Error out as expected.
[zedware@fedora ~/test]$ echo Bill | tr [A-Z] [a-z]
tr: missing operand after `x'
Two strings must be given when translating.
Try `tr --help' for more information.
19. '' still works.
[zedware@fedora ~/test]$ echo Bill | tr '[A-Z]' '[a-z]'
bill
20. Also works.
[zedware@fedora ~/test]$ echo Bill | tr A-Z a-z
bill
Conclusion:
1. Be careful of the differences among shells.
2. Always use the standard, portable syntax of shell commands.
- 可移植性真的很重要:on shell and tr
- 身体真的很重要!
- 钱真的很重要!
- 面试真的很重要
- 心态真的很重要
- 习惯真的很重要
- @Override真的很重要
- 算法真的很重要
- 观念真的很重要
- 选择真的很重要
- 基础真的很重要
- shell tr的语法
- 学好普通话真的很重要
- 英语真的很重要啊!
- acm真的很重要吗
- 工作经验真的很重要吗?
- 软件SOP真的很重要
- 好习惯真的很重要!
- 什么是 UART
- ubuntu vm与windows host共享文件
- 《Linux设备驱动程序》学习笔记----开发前的准备工作
- 在《可计算性与计算复杂性》中你最想学到什么?(限于吉林大学)
- java swing中ProgressBar(进度条)的使用
- 可移植性真的很重要:on shell and tr
- 【转载】MySql重装无响应
- java socket编程——TCP通信
- java socket编程——UDP通信
- 用C#生成不重复的随机数 - asp.net 教程
- hibernate异常"Found shared references to a collection"
- java Timer类的使用(计时器)
- SQL触发器[转]
- 使用StringBuilder类 - asp.net 教程