简体   繁体   English

Bash函数参数

[英]Bash function arguments

My problem is that prepinace_grepu , which is $3 inside of function, evaluates to -i instead of -i -n as I expected it to be . 我的问题是在函数内部位于$3 prepinace_grepu计算结果为-i而不是我期望的-i -n
How can I change it to work? 如何更改它的工作方式? And I cant put it in two variables, because I want it to be flexible and just take it as a string , so it will work even if prepinace_grepu="-c -v -i -f" etc... 而且我不能将其放在两个变量中,因为我希望它是灵活的,只是将其作为字符串使用,因此即使prepinace_grepu="-c -v -i -f"等也可以使用。

compare()
{
semka $2 $1 /etc/passwd /etc/shadow | sort > result1.txt
grep $3 $1 /etc/passwd /etc/shadow | sort > result2.txt
diff result2.txt result1.txt > diff.txt

if [[ -s diff.txt ]] ; 
    then echo "FAIL"
else echo "OK"
fi ;
 }

pattern=Hojny 
prepinace_moje="-vi"
prepinace_grepu="-i -n" 
compare $pattern $prepinace_moje $prepinace_grepu

Quote the variables in shell as much as you can: 尽可能在Shell中引用变量:

pattern="Hojny"
prepinace_moje="-vi"
prepinace_grepu="-i -n" 
compare "$pattern" "$prepinace_moje" "$prepinace_grepu"

Since prepinace_grepu contains space you must send it in quotes otherwise called function will only get -i and $3 and -n as $4 . 由于prepinace_grepu包含空间,因此必须使用引号将其发送,否则称为function只会得到-i$3以及-n$4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM