在Bash中使用Argument的别名 - Mac

时间:2017-01-26 06:20:54

标签: linux bash macos shell

您好在尝试在bash中为别名添加参数。我正在使用Mac。我搜索了一些相关的问题: Alias in BashAlias in Bash with autocomplete和其他一些人仍然无法解决这个问题。我知道我需要使用一个函数来创建一个带有输入的别名,但是不清楚它是否看起来像下面的任何选项。 .bash_profile中的所有输入。

$('form').submit(function () {
    $('input[type=submit]').prop('disabled', true);
    $('input[type=text]').prop('readonly', true);
    $('input[type=password]').prop('readonly', true);
});

这些似乎都不起作用。我要么出现语法错误,要么无法识别参数。

您在function mins_ago () { `expr $(date +%s) - 60 \* "$1"`; } alias mins_ago = function mins_ago () { `expr $(date +%s) - 60 \* "$1"`; } alias mins_ago = "function mins_ago () { `expr $(date +%s) - 60 \* "$1"`; }" alias mins_ago = function mins_ago () { `expr $(date +%s) - 60 \* $1`; } 中对此进行正确排序的实际行是什么?先感谢您。是否包含.bash_profile位,或者我只是进入函数定义?

4 个答案:

答案 0 :(得分:2)

Defining alias is not the right approach when you can easily do it with functions, and use the bash, arithmetic operator $(())

function mins_ago() {
    printf "%s" "$(( $(date +%s) - (60 * $1) ))"
}

Add the above function in .bash_profile, and now testing it in the command-line,

date +%s
1485414114

value="$(mins_ago 3)"
printf "%s\n" "$value"
1485413834

(or) without a temporary variable to convert to readable format in GNU date, do

printf "%s\n" "$(date -d@$(mins_ago 3))"

答案 1 :(得分:1)

将此内容添加到您的.bashrc并获取

mins_ago() {
  if [[ $@ != "" ]]; then
    command expr $(date +%s) - 60 \* "$@"
  else
    command echo "command error: mins_ago <integer>"
  fi
}

输出:

$ mins_ago 1
1485414404
$ mins_ago
command error: mins_ago <integer>

答案 2 :(得分:0)

输入

mins_ago () {
 `expr $(date +%s) - 60 \* "$1"`; 
}

适用于GNU bash 4.1.2:

$ mins_ago 5
bash: 1485411776: command not found

要避免错误,请使用echo:

mins_ago () {
 echo `expr $(date +%s) - 60 \* "$1"` 
}

答案 3 :(得分:0)

从第一次尝试中删除反引号,你应该没事。

然后该函数将输出时间戳,您可以编写echo "$(mins_ago)"

相关问题