简体   繁体   English

将 oneline 命令放入别名中

[英]Put oneline command into alias

I can't put this command into a linux alias.我无法将此命令放入 linux 别名中。

du --max-depth=1 | sort -nr | awk ' BEGIN { split("KB,MB,GB,TB", Units, ","); } { u = 1; while ($1 >= 1024) { $1 = $1 / 1024; u += 1 } $1 = sprintf("%.1f %s", $1, Units[u]); print $0; } '

I've tried in multiple ways but a I only get multiple syntax errors.我尝试了多种方式,但我只收到多个语法错误。 I try to escape $ sign, put " in the begin and end but it doesn't works.我尝试转义 $ 符号,将 " 放在开头和结尾,但它不起作用。

Do not bother with aliases.不要为别名烦恼。 They have been effectively deprecated for over 2 decades.它们已被有效弃用超过 2 年。 It is trivial to use a function.使用 function 很简单。 Put this in your startup scripts:把它放在你的启动脚本中:

foo() { du --max-depth=1 | sort -nr | awk  ... ; }

As others pointed out it's much easier to use a function.正如其他人指出的那样,使用 function 要容易得多。

If you really want to use an alias, put the command in a file and follow these steps:如果您真的想使用别名,请将命令放在文件中并按照以下步骤操作:

[STEP 101] # cat file
du --max-depth=1 | sort -nr | awk ' BEGIN { split("KB,MB,GB,TB", Units, ","); } { u = 1; while ($1 >= 1024) { $1 = $1 / 1024; u += 1 } $1 = sprintf("%.1f %s", $1, Units[u]); print $0; } '
[STEP 102] # eval alias foo="$( printf %q "$( < file )" )"
[STEP 103] # alias foo
alias foo='du --max-depth=1 | sort -nr | awk '\'' BEGIN { split("KB,MB,GB,TB", Units, ","); } { u = 1; while ($1 >= 1024) { $1 = $1 / 1024; u += 1 } $1 = sprintf("%.1f %s", $1, Units[u]); print $0; } '\'''
[STEP 104] #

Then copy the outputed alias definition into your bashrc or somewhere you like.然后将输出的别名定义复制到您的 bashrc 或您喜欢的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM