[英]Execute commands one by one in .aliases file in bash
I would like to create an alias in bash where if I type 'logs' it should take me to the latest log file. 我想在bash中创建一个别名,如果我键入“ logs”,它将带我到最新的日志文件。 My folder is structured such that ~/logs/date/time.
我的文件夹结构为〜/ logs / date / time。 After googling for sometime I found out the below command and it is working fine if i give in the bash prompt
谷歌搜索一段时间后,我发现了以下命令,如果我输入bash提示,它工作正常
cd ~/logs && cd `ls -tr | tail -1` && cd `ls -tr | tail -1`
But if I add it .aliases and map it to 'logs' then it is not working as expected. 但是,如果我添加它.aliases并将其映射到“日志”,则它无法按预期工作。 It cd's into logs and then try cd into latest file/folder where i execute the command.
它的cd进入日志,然后尝试cd到我执行命令的最新文件/文件夹。 (For eg.) if I am in FOLDER1 and execute 'logs', it cd's into logs and then try to cd into latest folder in FOLDER1.
(例如)如果我在FOLDER1中并执行'logs',则将其CD到日志中,然后尝试将CD插入FOLDER1中的最新文件夹。
Edited: My alias definition 编辑:我的别名定义
alias logs="cd ~/logs && cd `ls -tr | tail -1` && cd `ls -tr | tail -1`"
Any idea why it is behaving like this & how can I make this work ? 知道为什么它会这样吗?我该如何进行这项工作?
The problem is that backquoted commands are evaluated at once when defining the alias. 问题在于,定义别名时,会立即评估反引号命令。
Either escape them: 要么逃脱它们:
alias logs="cd ~/logs && cd \`ls -tr | tail -1\` && cd \`ls -tr | tail -1\`"
Or single quote the expression as Charles commented (you cannot use env. variables here): 或用单引号将查尔斯(Charles)注释过的表达式括起来(您不能在此处使用环境变量):
alias logs='cd ~/logs && cd "`ls -tr | tail -1`" && cd "`ls -tr | tail -1`"'
Or create a function, that works too and is more readable, so you can add robustness more easily as Charles (again!) suggested (when creating an alias, you just do a quick & hack job): 或创建一个功能也更有效且更具可读性的函数,因此您可以按照Charles(再次!)建议的那样,更轻松地添加健壮性(创建别名时,您只需进行快速的修改即可):
logs()
{
cd ~/logs || return
cd "`ls -tr | tail -1`" || return
cd "`ls -tr | tail -1`" || return
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.