我正在NAS (WD-MBL)中运行OpenWRT,并将一组别名组合在一起,以便通过命令行简化维护。
这些工作如预期的那样:
alias shutdown='sync && wait && sudo hdparm -Y /dev/sda && wait && sudo halt'优雅地关闭NAS。
daemon='sudo /etc/init.d/rsyncd status'告诉我rsync守护进程的状态。
drivechk='sudo dmesg | grep -i ext4-fs | grep -i sda'提醒我,文件系统问题是由需要e2fsck的错误关机引起的。
tempchk='sudo smartctl -d ata -A /dev/sda | grep Temperature | cut -c 5-8,87-89'告诉我驱动温度。
但有一件事我一直没能去工作:
fschk='df -h | grep -vE '^Filesystem|/dev/root|tmpfs'| awk '{ print $5 " " $1}'
从命令行运行的节按预期工作:
~$ df -h | grep -vE '^Filesystem|/dev/root|tmpfs'| awk '{ print $5 " " $1}'
53% /dev/sda1
37% /dev/sda3
~$如果我将其添加到/etc/profile.d/custom.sh文件中,请注销并再次登录--我在终端上得到如下信息:
~$ ssh user@192.168.1.3
--- snip ---
BusyBox v1.33.2 (2022-02-16 20:29:10 UTC) built-in shell (ash)
alias: }' not found
~$ 如果我运行别名,就会得到以下信息:
~$ fschk
> 如果我从命令行查询alias列表,我会发现我添加的列表在printout中显示得不一样:
~$ alias
--- snip ---
fschk='df -h|grep -vE '"'"'^/dev/root|tmpfs'"'"'|awk '"'"'{print '
--- snip ---
:~$ 但在我输入的文件中却没有:
~$ cat /etc/profile.d/custom.sh
--- snip ---
alias fschk="df -h|grep -vE '^/dev/root|tmpfs'|awk '{print $5 " " $1}'"
--- snip ---
~$ ash似乎有一个不同的/简化的alias版本,但我无法理解这个版本。
任何指示都将受到极大的赞赏。
提前谢谢。
最好的
PCL
发布于 2022-05-08 19:26:08
你的化名,
alias fschk="df -h|grep -vE '^/dev/root|tmpfs'|awk '{print $5 " " $1}'"是一个双引号字符串。因此,当shell定义别名时,它将在字符串中同时展开$5和$1。为了避免出现这种情况,一定要避开美元的迹象。
别名还包含双引号,如果不希望它们中断字符串,则需要转义。
alias fschk="df -h | grep -vE '^/dev/root|tmpfs' | awk '{ print \$5 \" \" \$1}'"或简化如下:
alias fschk="df -h | awk '!/^\/dev/root|tmpfs/ { print \$5, \$1 }'"或者,作为一个shell函数(在这种情况下,您对引用没有任何问题):
fschk () {
df -h | awk '!/^\/dev/root|tmpfs/ { print $5, $1 }'
}或者,作为一个函数,它接受文件系统列表以避免提取df信息:
fschk () {
df -h | (
IFS='|'
pat="${*:+^($*)}" awk 'ENVIRON["pat"] == "" || $0 !~ ENVIRON["pat"] { print $5, $1 }'
)
}你会用这个
fschk tmpfs /dev/root实际上,我建议您将所有别名重写为shell函数。别名只对琐碎的事情有用。
https://unix.stackexchange.com/questions/701865
复制相似问题