分页: 1 / 1
再问下关于字符串的处理
发表于 : 2012-03-22 11:38
由 chengmoYS
假设我有一个
a="cd dvd bd"
现在我想查看cd这个在变量a中有没有
有没有简便的方法
我可不想
for i in $a
if [ "cd" = "$i"]
这样
有没有直接的比方说
grep "cd" "$a"这样好像不行 出发把$a写到一个文件中
在用grep "cd" 文件
Re: 再问下关于字符串的处理
发表于 : 2012-03-22 11:39
由 chengmoYS
我说简单点,就是看一个字符串中 是否包含特定字符序列
Re: 再问下关于字符串的处理
发表于 : 2012-03-22 11:49
由 aerofox
代码: 全选
if [[ " $a " == *" cd "* ]]; then echo 找到了; fi
代码: 全选
if echo "$a" | grep -qw cd; then echo 找到了; fi
请自行验证。
Re: 再问下关于字符串的处理
发表于 : 2012-03-22 13:15
由 chengmoYS
echo "${_READABLE_MEDIA}" | grep "CD" && echo "medium inserted supported" || (echo "medium inserted not supported"; exit)
谢谢
没想到管道 哈哈
Re: 再问下关于字符串的处理
发表于 : 2012-03-24 10:35
由 josephyoung
Re: 再问下关于字符串的处理
发表于 : 2012-03-24 11:13
由 ubuntu_vivi
grep可以用管道
echo $a | grep cd
然后判断返回值$?
Re: 再问下关于字符串的处理
发表于 : 2012-03-25 20:20
由 loveacat
echo "$a" | grep 'cd'
Re: 再问下关于字符串的处理
发表于 : 2012-03-25 22:02
由 cao627
chengmoYS 写了:echo "${_READABLE_MEDIA}" | grep "CD" && echo "medium inserted supported" || (echo "medium inserted not supported"; exit)
谢谢
没想到管道 哈哈
学习
Re: 再问下关于字符串的处理
发表于 : 2012-03-26 9:02
由 eexpress
say "yes" if /cd/;
perl 风格。