我使用接受参数的脚本。参数是可选的,可以以任何顺序出现。
#!/bin/bash
# script name: test.sh
for var in "$@"
do
if [ ! -z "$var" ] && ([ $var = "--example" ] || [ $var = "-e" ]); then
echo "example"
elif [ ! -z "$var" ] && ([ $var = "--project" ] || [ $var = "-p" ]); then
echo "project with string xxxxxxx"
fi
done
在这个简单的示例中,您可以将其命名为以下示例(某些示例):
# this will echo example
./test.sh --example
# this will echo project with string xxxxxxx
./test.sh --project
# this will echo both example and project with string xxxxxxx
./test.sh --example --project
现在,我要实现的是我可以做类似的事情(警告,这是pseuco代码):
#!/bin/bash
# script name: test.sh
for var in "$@"
do
if [ ! -z "$var" ] && ([ $var = "--example" ] || [ $var = "-e" ]); then
echo "example"
elif [ ! -z "$var" ] && ([ $var = "--project" ] || [ $var = "-p" ]); then
echo "project with string $VAR_VALUE"
fi
done
# this will echo example
./test.sh --example
# this will echo project with string myproject1
./test.sh --project="myproject1"
# this will echo both example and project with string myproject2
./test.sh --example --project="myproject2"
有人可以帮我重写它吗,这样可以正常工作吗?