编写一个从终端接收变量的 shell 脚本?(~$ 命令变量)

Ant*_*Cas 3 bash shell-script

我正在编写一个 shell 脚本,它需要一段时间(以分钟为单位)并为该数量打开一个在线鸡蛋计时器。

#!/bin/bash
#Opens http://e.ggtimer.com/TIMEmin where TIME = number of minutes
TIME=5
xdg-open http://e.ggtimer.com/"$TIME"min
Run Code Online (Sandbox Code Playgroud)

当我运行上面的脚本时,它会打开网页http://e.ggtimer.com/5min 我想从终端设置时间。例如:

eggtimer 10
Run Code Online (Sandbox Code Playgroud)

会打开http://e.ggtimer.com/10min

谢谢!

slm*_*slm 5

如果您使用 Bash 变量的以下功能,如果它们有值,则使用它们,否则使用默认值,您可以像这样编写示例:

$ more etimer.bash 
#!/bin/bash
#Opens http://e.ggtimer.com/TIMEmin where TIME = number of minutes
TIME=${1:-5}
xdg-open http://e.ggtimer.com/"$TIME"min
Run Code Online (Sandbox Code Playgroud)

etimer.bash如果提供,这将使用传入的参数,否则将使用 5 分钟。

例子

$ ./etimer.bash
Run Code Online (Sandbox Code Playgroud)

此 URL 中的结果:http : //e.ggtimer.com/5min

$ ./etimer.bash 10
Run Code Online (Sandbox Code Playgroud)

此 URL 中的结果:http : //e.ggtimer.com/10min

参考