我不知道是否可能,但我想编写shell脚本,其作用类似于带有选项的常规可执行文件。作为一个非常简单的示例,请考虑配置为可执行的shell脚本foo.sh:
./foo.sh
./foo.sh -o
代码foo.sh
就像
#!/bin/sh
if ## option -o is turned on
## do something
else
## do something different
endif
有可能,怎么做?感谢。
答案 0 :(得分:19)
$ cat stack.sh
#!/bin/sh
if [[ $1 = "-o" ]]; then
echo "Option -o turned on"
else
echo "You did not use option -o"
fi
$ bash stack.sh -o
Option -o turned on
$ bash stack.sh
You did not use option -o
<强>供参考:强>
$1 = First positional parameter
$2 = Second positional parameter
.. = ..
$n = n th positional parameter
要获得更加整洁/灵活的选项,请阅读其他帖子:Using getopts to process long and short command line options
答案 1 :(得分:9)
这是如何在一个脚本中执行此操作的方法:
synchronized(Restaurant.waitpersonsList) {
try {
System.out.println("Waitperson" + this.waitPersonId + " is waiting...");
wait();
System.out.println("WaitPerson" + this.waitPersonId + " is attempting to get " + restaurant.order);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
点击here获取来源
答案 2 :(得分:4)
答案 3 :(得分:0)
您可以按任意顺序遍历参数并读取选项。这是一个例子。有关使用此代码的脚本,请参见-script example
var data = [{ratings: [ { rating: 5 } ], counts: [ { count: 100 } ]}];
var flatten = function(data) {
if (Array.isArray(data)) {
data = data[0];
for (var key in data) data[key] = flatten(data[key]);
}
return data;
}
console.log(flatten(data));