Shell on the roads
来源:互联网 发布:最近朋友圈 网络 红人 编辑:程序博客网 时间:2024/05/29 15:09
#简单介绍
你能不能用shell判断一个文件中的数字是否有序啊?想想这不挺简单的吗,就开始动手写了,然后就有了这个版本。
#!bin/shfilename=$1before=-1;flag=1;for line in `cat data.txt`;do after=${line}; echo $line if [ "$before">"$after" ];then echo "FAILED"; flag=0; break; else before=${line}; fidoneif [ $flag = 1 ];then echo "SUCCESS"fi
哎呀,shell里不是有自带的sort
命令吗,怎么不懂得试试那个sort
呢。于是我就查阅了相关博客
* Linux下Sort命令的一些使用技巧
* LINUX SHELL脚本攻略笔记[速查]
#返回值
原来shell脚本的返回值不是直接返回啊,而是通过
linux中shell变量
$#
,$@
,$0
,$1
,$2
的含义解释:
变量说明:
$$
Shell本身的PID(ProcessID)
$!
Shell最后运行的后台Process的PID
$?
最后运行的命令的结束代码(返回值)
$-
使用Set命令设定的Flag一览
$*
所有参数列表。如”$*”用「”」括起来的情况、以$1 $2 … $n
的形式输出所有参数。
$@
所有参数列表。如”$@
“用「”」括起来的情况、以”$1
” “$2
” … “$n
” 的形式输出所有参数。
$#
添加到Shell的参数个数
$0
Shell本身的文件名
$1~$n
添加到Shell的各参数值。$1
是第1参数、$2
是第2参数…。
因此判断一个文件是否是有序的sort返回结果需要通过$?
的值进行判断。可是sort是按照行来判断是否有序的,而不是判断所有的是否有序的。比如说datafile 中内容如下:
1 2 3 4 5 6 8 92 3 4 5 6 7 5 323 4 5 6 6 34 3
按照默认sort 情况下,上述文件是有序的。但是实际上总体来说,我们需要返回该文件为无序的,因此,sort的方案只好作罢。
#数字比较
后来看看之前那个实现的逻辑,应该是没有什么大问题的呀。
数字的比较
-eq 相等(equal)-ne 不等(not equal)-gt 大于(greater than)-lt 小于(less than)-ge 大于等于 (greater than or equal)-le 小于等于 (less than or equal)
字符串的比较
[ $str1 = $str2 ] 等于[ $str1 != $str2 ] 不等于[ -z $str ] 空字符串返回true[ -n $str ]或者[ $str ]非空字符串返回true
OMG, 原来shell里的>
不是大于号啊,而是表示输入输出,下面就查找了一下关于linux标准文件描述符:
标准输入和标准输出指的就是键盘和显示器。
当文件描述符(0,1,2)与重定向符号<
组合之后,就可以重新定向输入,输出,及错误。
* command 2>file1
命令执行的错误信息保存到了file1文件中。显示屏只是显示正确的信息。
* command 1>file1 2>file2
命令执行后,没有显示。因为正确输出到file1,错误定向到file2
* command &>file1
命令执行后,输出和错误都定向到file1中
在shell脚本中,可以定义“错误”输出到STDERR指定的文件.需要在重定向符和文件描述符之间加一个and符&
经过这番折腾,终于在shell下将这个简单的问题搞定了!!!
#!bin/shfunction is_sorted(){ before=-1; flag=1; for line in $(<$1); do if [ $before -gt $line ];then echo "Failed at $before, $line" flag=0 break fi before=$line; done if [ $flag = 1 ];then echo "SUCCESS" fi}is_sorted $1
调用函数的方法为is_sorted datafilename
或者调用bash脚本bashfilename.sh datafilename
- Shell on the roads
- The Two Roads 两条路
- Roads in the North
- uva12092 Paint the Roads
- Paint the Roads UVALive
- the evil Linux -- shell tips on if sentence
- 10308 - Roads in the North
- Uva-12092-Paint the Roads
- E - Roads in the North
- 【poj2631】Roads in the North
- <poj2631>Roads in the North
- [Usaco08Open] Roads Around The Farm
- Roads in the North POJ
- [Linux Shell] vim editor quit without content leave on the screen
- 10308 - Roads in the North--不好!
- UVA - 10308 Roads in the North
- POJ - 2631 Roads in the North
- uva 10308 - Roads in the North(dfs)
- 206. Reverse Linked List
- git卡在Resolving deltas的解决办法
- android6.0 Activity(一) Activity创建 初始化
- MySQL增加/删除用户、授权、修改密码等方法和命令总结
- 无论怎么更改jsp,浏览器中显示都不改变。
- Shell on the roads
- 装饰模式
- FFMPEG学习----使用SDL构建音频播放器
- rtmp的消息类型
- java微信公众号开发 - 微信服务器的接入和验证
- 我的后端开发书架
- 如何处理大量数据高并发大流量并发操作方案
- NCL安装及运行
- ffmpeg用代码实现自己的muxer