linux - 如何让终端建议自定义命令的选项?

标签 linux shell ubuntu terminal console

我是 Kubuntu 用户。

如果我输入

$ apt-get(选项卡)

在终端上,Shell 建议选项:
自动清理、构建依赖、检查、...、清理、下载、安装、删除、更新

如果我想在路径中使用别名或 shell 脚本来使用带有选项的自定义命令,如何使终端(或 Shell)建议选项?


例如,命令是“myclock”,其选项是“us”和“korea”。

如果我输入

$ myclock 韩国

->(打印韩国时间)

如果我输入

$ myclock 我们

->(打印美国时间)

如何让终端在我输入时建议选项(“美国”或“韩国”)

$ myclock(选项卡)

预先感谢您!

最佳答案

您需要通过 .bash_profile 或 kubuntu .profile 添加自定义别名

sudo 进入 root 并使用您选择的文本编辑器。我更喜欢 vi,但你可以使用 nano!

 sudo -i
  root@server vi .profile

默认情况下您的个人资料应如下所示

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n

现在添加别名。您可以随意命名您的别名!这是我使用的 .profile 的自定义别名。我在旧金山工作,住在奥克兰,所以天气可能略有不同,所以我创建了一个自定义别名来适应天气的curl

alias weatherSF="curl -s 'http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=en|us|san-francisco-ca|94103' | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3, \1/p'"
alias weatherOak="curl -s 'http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=en|us|Oakland-ca|94612' | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3, \1/p'"

现在请注意,如果您尝试按 Tab 键,它不会起作用..所以您需要做的是

source ~/.profile

在源代码之后,您应该能够标记您的新别名!

如果您对其他别名感兴趣,这里有一些其他示例

alias myip='curl ip.appspot.com'                    # myip:         Public facing IP Address
alias netCons='lsof -i'                             # netCons:      Show all open TCP/IP sockets
alias flushDNS='dscacheutil -flushcache'            # flushDNS:     Flush out the DNS Cache
alias lsock='sudo /usr/sbin/lsof -i -P'             # lsock:        Display open sockets
alias lsockU='sudo /usr/sbin/lsof -nP | grep UDP'   # lsockU:       Display only open UDP sockets
alias lsockT='sudo /usr/sbin/lsof -nP | grep TCP'   # lsockT:       Display only open TCP sockets
alias ipInfo0='ipconfig getpacket en0'              # ipInfo0:      Get info on connections for en0
alias ipInfo1='ipconfig getpacket en1'              # ipInfo1:      Get info on connections for en1
alias openPorts='sudo lsof -i | grep LISTEN'        # openPorts:    All listening connections
alias showBlocked='sudo ipfw list'                  # showBlocked:  All ipfw rules inc/ blocked IPs

check this website for some tips

关于linux - 如何让终端建议自定义命令的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28156213/

相关文章:

c++ - 在执行过程中替换可执行文件时如何处理 "/proc/self/exe"的 readlink()?

linux - 如何添加 session 启动程序

linux - 远程 SSH 和命令集以及变量替换

memory - 程序刚刚填满了我的内存并开始在 Xubuntu 中填满交换空间

python - 如何解决 Pymongo 的这个错误?游标 ID 找不到 pymongo

html - Libreoffice 将 html 转换为 xls 或 xlsx

linux - 如何在 shell 中最后一次出现子字符串后剪切字符串?

scripting - 脚本在今天适合什么地方

algorithm - shell脚本中关联数组的时间复杂度

python - 在 Ubuntu/GNOME 上使用 PyQt 将窗口放置在桌面图标后面