tcsh别名,条件为if-then-else [英] tcsh alias with if-then-else condition

查看:317
本文介绍了tcsh别名,条件为if-then-else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在命令结果中使用if-then-else条件
的别名。

I am trying to have an alias with if-then-else condition on a command result.


  1. 别名会得到一个文件模式

  2. 如果只有一个文件,它将打开文件

  3. 如果有多个或少于1个文件,它将打开

我尝试过的别名是:

alias atest 'if \("ls \!:1" > 0 \)  "nedit ls \!:1" else "echo cant open the file" '


推荐答案

有两种方法,大多数都不是优雅,但以下是我发现的最美观,最容易创建的东西:

There are a couple of ways, most of them are not very elegant, but the following is the best looking and easiest to create that I have discovered:

alias aliasname '`if (condition == check) echo "echo"` >&/dev/null && code_if_true || code_if_false'

您可以嵌套if语句遵循这种格式,也可以将其用作如果将if语句的内部更改为

You have the ability to nest if statements following this format, and can also use it as a checker for arguments if you change the interior of the if statement to be

if ("\!:1" =~ "check")

>& / dev / null用于清除函数的输出,但不是必须的。如果您有兴趣,也可以在别名中使用for语句,但是这种方法不太优雅。我没有使用过它,但是如果您希望创建一个具有不同数量参数的别名,则有必要使用它。再说一次,那很丑,所以到那时我只写一个函数。

The >&/dev/null is to clean up the output of the function, but is not necessary. If you are interested, there is also a way to make for statements within aliases, but that method is a lot less elegant. I haven't used it but it is necessary if you wish to create an alias with a varying number of arguments. Again, that one is ugly, so at that point I'd just write a function.

这篇关于tcsh别名,条件为if-then-else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆