KSH检查字符串是否以子字符串开头 [英] KSH check if string starts with substring

查看:113
本文介绍了KSH检查字符串是否以子字符串开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查变量是否具有以指定子字符串开头的字符串值.

I need to check if the variable has value of string which starts with specified substring.

在Python中,将是这样的:

In Python it would be something like this:

foo = 'abcdef'
if foo.startswith('abc'):
    print 'Success'

在Ksh中检查strig $foo是否以子字符串bar开头的最明确的方法是什么?

What is the most explicit way to check in Ksh whether strig $foo starts with substring bar?

推荐答案

这很简单,但看起来有点奇怪:

It's very simple but looks a bit odd:

if [[ "$foo" == abc* ]]; then ...

人们会假设ksh会使用当前目录中的文件扩展模式,但是相反,它会进行模式匹配.但是,您需要[[.单个[将不起作用.如果foo中没有空格,则引号不是必须的.

One would assume that ksh would expand the pattern with the files in the current directory but instead, it does pattern matching. You need the [[, though. Single [ won't work. The quotes are not strictly necessary if there are no blanks in foo.

这篇关于KSH检查字符串是否以子字符串开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆