我想在 case 语句中使用变量作为条件。类似于:
#!/bin/sh
ALLOWED_SERVICES=tomcat6|james;
case $1 in
$ALLOWED_SERVICES )
service $1 restart
;;
* )
echo "Unsupported argument"
;;
esac
Run Code Online (Sandbox Code Playgroud)
这不起作用。当脚本以tomcat6参数为 exapmle启动时,它会输出“不支持的参数”消息。但是当 case 条件被硬编码时,它可以正常工作:
case $1 in
tomcat6|james )
service $1 restart
;;
* )
echo "Unsupported argument"
;;
esac
Run Code Online (Sandbox Code Playgroud)
在这种情况下可以使用变量吗?