[英]Old csh alias into bash
I'm currently updating some of our scripts from the unreadable world of csh to bash. 我目前正在将一些脚本从不可读的csh更新为bash。 We have a local alias stored on our system which is used throughout
我们在系统上存储了一个本地别名,该别名在整个
alias sc 'set \!:2 = `current -\!:1 | cut -c7-`'
This uses a C executable called current which we use to identify the current selected data type. 这使用了一个名为current的C可执行文件,我们使用该可执行文件来标识当前选择的数据类型。 There are 5 data types here image(i), region(r), curve(c), textfile(t) or listfile (l)
这里有5种数据类型:图像(i),区域(r),曲线(c),文本文件(t)或列表文件(l)
The usage of this alias is fairly straightforward sc i A_image
该别名的用法非常简单,可以是
sc i A_image
This will find the currently selected image and set the value (which will be an integer) and set the retrieved value to the parameter $A_image which can then be used later in the script. 这将找到当前选定的图像并设置值(将是整数),并将检索到的值设置为参数$ A_image,该参数随后可在脚本中使用。
Similarly sc r Aregion
will find the currently selected region and set it to the parameter $Aregion. 同样,
sc r Aregion
将找到当前选定的区域并将其设置为参数$ Aregion。
I have never really seen the used of !:2 in csh and do not know what it is called. 我从来没有真正看到过csh中!:2的用法,也不知道它叫什么。 Is there a way I can implement this functionality in bash as it is very useful for our scripts
有什么办法可以在bash中实现此功能,因为它对我们的脚本非常有用
Those appear to be history-like word/argument selectors. 这些似乎是类似历史的单词/参数选择器。
So !:2
is the second argument to the alias and !:1
is the first argument to the alias. 因此,
!:2
是别名的第二个参数, !:1
是别名的第一个参数。
So sc i A_image
ends up calling 所以
sc i A_image
最终打电话
set A_image = `current -i | cut -c7-`
bash aliases can't take arguments, you need to use functions for that (functions are generally more useful anyway). bash别名不能接受参数,您需要为此使用函数(无论如何,函数通常更有用)。
The equivalent bash 4.2+ function would be 相当于bash 4.2+的功能是
sc() {
declare -g "$2=$(current "$1" | cut -c7-)"
}
For bash 3.1+ it would be 对于bash 3.1+,它将是
sc() {
printf -v "$2" -- %s "$(current "$1" | cut -c7-)"
}
For older bash or /bin/sh
it would be 对于较旧的bash或
/bin/sh
,它将是
sc() {
eval "$2=\$(current \"$1\" | cut -c7-)`"
}
but this version is unsafe in general for untrusted input. 但是此版本通常对于不可信的输入是不安全的。
(All functions above untested but should work just fine.) (以上所有功能未经测试,但应该可以正常工作。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.