比较shell脚本中的字符串

时间:2016-08-11 12:53:01

标签: bash shell

我有一个名为parameters.txt的文件,其内容如下:

sheet_name:TEST
sheet_id:CST
sheet_access:YES

我有一个shell脚本从parameters.txt文件中提取此文本。它使用:作为parameters.txt文件的每一行的分隔符,并存储:var1的剩余内容以及:var2的任何内容。 {1}}。我希望matchedvar1存储sheet_namenot matchedsheet_name存储matched。以下是我的代码,无论var1存储什么,都会打印filename=parameters.txt IFS=$'\n' # make newlines the only separator for j in `cat $filename` do var1=${j%:*} # stores text before : var2=${j#*:} # stores text after : if [ “$var1” == “sheet_name” ]; then echo ‘matched’ else echo “not matched“ fi done

TestCase

我做错了什么?请帮助。

2 个答案:

答案 0 :(得分:1)

你无用的猫。但是有些[ shell parameter expansion ]怎么样?

while read line
do
 if [[ "${line%:*}" = "sheet_name" ]] #double quote variables deals word splitting
 then
  echo "matched"
 fi
done<parameters.txt

会完全按照您的意愿行事。

给您留言

[ ShellCheck ]说,

  

&#34;要读取行而不是单词,管道/重定向到&#39;而读取&#39;   。环路&#34;

检查来自shellcheck的[ this ]注释。

答案 1 :(得分:0)

这个怎么样?

class X {    
    // Int -> () function
    func foo(number num: Int) { print("function \(num)")}

    // Int -> () closure
    let foo: (Int) -> () = { print("closure \($0)")}
}
let x = X()
x.foo(2)    // x.foo(num: Int)
x.foo(3)    // x.foo
相关问题