检查输入是否与 bash 脚本中的字符串参数匹配
Check if input matches string argument in a bash script
我需要检查命令行参数是否与 bash 脚本中的匹配,这是我的代码。
#!/bin/bash
# lets call this script.sh
myFunction() {
if [[ == '--log' ]]; then
echo "hello world" >> file.log
else
echo "Unknown argument"
fi
}
myFunction
示例输入:
bash script.sh --log
但似乎没有向 file.log
中写入任何内容
您需要将脚本的参数传递给 myFunction
:
myFunction "$@"
"$@"
表示“传递给此脚本的所有参数”。
我需要检查命令行参数是否与 bash 脚本中的匹配,这是我的代码。
#!/bin/bash
# lets call this script.sh
myFunction() {
if [[ == '--log' ]]; then
echo "hello world" >> file.log
else
echo "Unknown argument"
fi
}
myFunction
示例输入:
bash script.sh --log
但似乎没有向 file.log
中写入任何内容您需要将脚本的参数传递给 myFunction
:
myFunction "$@"
"$@"
表示“传递给此脚本的所有参数”。