我在Bash中有一个字符串:
string="My string"
Run Code Online (Sandbox Code Playgroud)
如何测试它是否包含另一个字符串?
if [ $string ?? 'foo' ]; then
echo "It's there!"
fi
Run Code Online (Sandbox Code Playgroud)
??我的未知运营商在哪里?我是否使用echo grep?
if echo "$string" | grep 'foo'; then
echo "It's there!"
fi
Run Code Online (Sandbox Code Playgroud)
这看起来有点笨拙.
我试图看看一个字符串是否是shell脚本中另一个字符串的一部分(#!bin/sh).
我现在的代码是:
#!/bin/sh
#Test scriptje to test string comparison!
testFoo () {
t1=$1
t2=$2
echo "t1: $t1 t2: $t2"
if [ $t1 == "*$t2*" ]; then
echo "$t1 and $t2 are equal"
fi
}
testFoo "bla1" "bla"
Run Code Online (Sandbox Code Playgroud)
我正在寻找的结果是,我想知道"bla1"中存在"bla"的时间.
谢谢和亲切的问候,
更新:我已经尝试了这里描述的"包含"函数:如何判断字符串是否包含Unix shell脚本中的另一个字符串?
但是,它们似乎与普通的shell脚本(bin/sh)不兼容......
救命?
在shell脚本中,如何确定字符串是否包含在另一个字符串中.在bash中,我只想使用=〜,但我不知道如何在/ bin/sh中做同样的事情.可能吗?
我必须编写一个 bash 脚本,根据传递的命令行参数在后台启动一个进程,如果它能够成功运行启动程序,则返回。
这是我想要实现的伪代码
if [ "$1" = "PROG_1" ] ; then
./launchProg1 &
if [ isLaunchSuccess ] ; then
echo "Success"
else
echo "failed"
exit 1
fi
elif [ "$1" = "PROG_2" ] ; then
./launchProg2 &
if [ isLaunchSuccess ] ; then
echo "Success"
else
echo "failed"
exit 1
fi
fi
Run Code Online (Sandbox Code Playgroud)
脚本不能wait或sleep因为它将被另一个关键任务 C++ 程序调用并且需要高吞吐量(每秒启动的进程数),而且进程的运行时间是未知的。脚本既不需要捕获任何输入/输出,也不需要等待启动的进程完成。
我尝试了以下方法失败:
#Method 1
if [ "$1" = "KP1" ] ; then
echo "The …Run Code Online (Sandbox Code Playgroud)