如何为我们的snap应用取别名alias

我们知道,有时我们的snap应用的名称很长,不便于输入.在snap 2.20+和snapcraft 2.24+以后的版本中,我们提供了别名从而使得我们可以用我们喜欢的名字来调用我们的应用.


我们可以参考我们在snapcraft中的一个项目:

https://github.com/snapcore/snapcraft/tree/master/integration_tests/snaps/alias

这个项目的snapcraft.yaml定义如下:


snapcraft.yaml

name: my-alias
version: '0.1'
summary: command alias example
description: |
  Command alias example
grade: devel
confinement: devmode

apps:
  hello:
    command: hello.sh
    aliases: [hi.sh, howdy.sh]

parts:
  aliases:
    plugin: dump
    source: .

在这个应用中,我们为我们的应用my-alias.hello定义了两个别名:hi.sh及howdy.sh.这里的hello.sh的内容如下:

hello.sh

#!/bin/bash

CMD=$(basename "$0")

if [ "$CMD" == "hi.sh" ]; then
    echo "Hi world!"
elif [ "$CMD" == "howdy.sh"  ]; then
    echo "Howdy world!"
else
    echo "Hello world!"
fi

我们打包我们的应用,并安装运行:

liuxg@liuxg:~$ my-alias.hello 
CMD: hello.sh
Hello world!
liuxg@liuxg:~$ hi.sh
bash: /snap/bin/hi.sh: No such file or directory

很显然,我们的别名并没有起作用.这是什么原因呢?其实我们可以使用snap的命令来查看一下帮助信息:

liuxg@liuxg:~$ snap alias -h
Usage:
  snap [OPTIONS] alias [alias-OPTIONS] [<snap>] [<alias>...]

The alias command enables the given application aliases defined by the snap.

Once enabled the respective application commands can be invoked just using the aliases.

Application Options:
      --version      Print the version and exit

Help Options:
  -h, --help         Show this help message

[alias command options]
          --reset    Reset the aliases to their default state, enabled for automatic aliases,
                     disabled otherwise

原来为了能够使得我们的别名能够起作用,我们应该通过命令行来启动它.

liuxg@liuxg:~$ sudo snap alias my-alias hi.sh
[sudo] password for liuxg: 

通过上面的命令,我们就把别名启动了,让后在我们的命令行中直接打入命令:

liuxg@liuxg:~$ hi.sh
CMD: hello.sh
Hello world!

我们可以通过同样的方法来对howdy.sh来操作.

如果我们重置我们的alias,我们可以通过如下的命令来完成:

$ sudo snap alias --reset my-alias hi.sh

我们的alias可以在Ubuntu Store设置在默认的情况下启动,那么这个reset将会恢复最初的默设置.

或者,我们可以通过如下的命令来完成:

$ sudo snap unalias my-alias hi.sh







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值