记linux shell的两个小技巧:shell数组和字符串判断
2016-07-01 11:33
591 查看
最近在使用shell写脚本的时候,想实现python中两个很简单的功能:1:判断一个字符串是否包含另一个字符串。2:怎么用实现python的列表功能。这里跟大家分享一下。
1:判断一个字符串是否包含另一个字符串:
运行结果为:do something..
2:使用shell的数组达到python的列表效果。
定义数组:
# test=(a b c d e f g)
查看数组:
# echo ${test[@]}
a b c d e f g
往数组中追加一个元素e:
# test=(${test[@]} e)
# echo ${test[@]}
a b c d e f g e
通过下标索引删除一个元素,shell数组的下表和python一样,是从0开始的,所以2下标代表的是第三个元素c:
# unset test[2]
# echo ${test[@]}
a b d e f g e
数组分片,以下命令截取了test数组0-3的元素范围,即前三个元素:
# echo ${test[@]:0:3}
a b d
循环访问数组:
本文出自 “扮演上帝的小丑” 博客,转载请与作者联系!
1:判断一个字符串是否包含另一个字符串:
string="abcdefg" if [[ "$string" =~ "abc" ]];then echo "do something.." else echo "nothing.." fi以上的shell判断"abc"是否包含在字符串$string中。
运行结果为:do something..
2:使用shell的数组达到python的列表效果。
定义数组:
# test=(a b c d e f g)
查看数组:
# echo ${test[@]}
a b c d e f g
往数组中追加一个元素e:
# test=(${test[@]} e)
# echo ${test[@]}
a b c d e f g e
通过下标索引删除一个元素,shell数组的下表和python一样,是从0开始的,所以2下标代表的是第三个元素c:
# unset test[2]
# echo ${test[@]}
a b d e f g e
数组分片,以下命令截取了test数组0-3的元素范围,即前三个元素:
# echo ${test[@]:0:3}
a b d
循环访问数组:
# for element in ${test[@]} do echo "This is $element..." done #result This is a... This is b... This is d... This is e... This is f... This is g... This is e...
本文出自 “扮演上帝的小丑” 博客,转载请与作者联系!
相关文章推荐
- shell变量和函数定义
- 这里是shell命令的总结Linux和Unix通用
- HBase的shell命令无法使用,如上图,与Hbase对应的HMaster进程没有启动
- shell 模拟curl
- shell while循环
- adb shell am 的用法
- shell
- python调用shell, shell 引用python
- python调用shell, shell 引用python
- shell 脚本
- linux shell “(())” 双括号运算符使用
- shell脚本实现拷贝大文件显示百分比的代码分享
- Shell中的循环
- 深入浅出ShellExecute
- Linux常用的shell命令汇总
- shell脚本中字符串截取的多种方法
- 添加bash命令
- linux下,shell如何删除指定字符间的内容
- 启动一个shell脚本项目
- Linux Shell格式化Json