为什么使用 ssh 执行命令可以工作,但将其分配给 var 却不起作用?
以下命令有效:
ssh host ls -1 /home/folder/anotherFolder | wc -l
但这个不起作用:
ssh host num=`ls -1 /home/folder/anotherFolder | wc -l`
也不是这样:
ssh host num=`ls -1 "/home/folder/anotherFolder" | wc -l`
也不是这样:
ssh host num=`ls -1 "/home/folder/anotherFolder" | wc -l`
所有这些都返回:
ls: cannot access /home/folder/anotherFolder: No such file or directory
因为它是在客户端计算机而不是在远程计算机中查找此文件夹。
编辑: 澄清: 我需要在 ssh 上下文中包含 $num ,而不是在它之外。 整个 ssh 命令是检查 $num 是否大于 X,如果大于,则在远程计算机上执行另一个命令,所有这些都在单个 ssh 会话中进行。
编辑2: 谢谢,在你的帮助下我已经完成了我所需要的。 最初的任务是删除远程计算机中最旧的文件夹(如果它具有超过 X 个定义的文件夹)。 这对我有用:
ssh host "num=\$(ls -1 \$path | wc -l); if [ \$num -gt $num_of_backups ]; then rm -r $backup/\"\$(ls -t1 $backup | tail -n 1)\"; fi"
不过,我怀疑这不是做我需要的事情的最佳方式。 任何其他方式将不胜感激。
the following command works:
ssh host ls -1 /home/folder/anotherFolder | wc -l
but this one doesnt work:
ssh host num=`ls -1 /home/folder/anotherFolder | wc -l`
nor this:
ssh host num=`ls -1 "/home/folder/anotherFolder" | wc -l`
nor this:
ssh host num=`ls -1 "/home/folder/anotherFolder" | wc -l`
all these return:
ls: cannot access /home/folder/anotherFolder: No such file or directory
as it was looking for this folder in the client computer rather than in the remote one.
EDIT:
clarification:
I nee to have $num in the ssh context, not outside of it.
The whole ssh command is to check if $num is greater than X and if it does, perform another command on the remote computer, all in a single ssh session.
EDIT2:
Thanks, I've accomplished what I've needed with your help.
The original task is to delete the oldest folder in a remote machine if it has more than X defined folders.
this is what works for me:
ssh host "num=\$(ls -1 \$path | wc -l); if [ \$num -gt $num_of_backups ]; then rm -r $backup/\"\$(ls -t1 $backup | tail -n 1)\"; fi"
Though, I suspect its not the best way to do what I need.
Any other way would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试以下操作:
您需要转义
$
符号,否则它们将在本地主机上展开。或者,使用单引号,则不必转义
$
:此外,最好使用
$(command)
格式而不是` command`
用于命令替换。Try the following:
You need to escape the
$
symbols or they will be expanded on the local host.Alternatively, use single quotes, then you don't have to escape the
$
:Also, it is better practice to use
$(command)
format instead of`command`
for command substitution.任何未转义的管道和反引号都将在当前主机上进行评估。因此,
意味着“在其他主机上运行
ls -1 /home/folder/anotherFolder
并计算此主机上的行数”。这些命令都在当前主机上运行
ls
和wc
,然后将num
分配给 em>other 主机:原型修复:
含义:在另一台主机上运行
ls
,将所有数据发送到该主机,然后在该主机上运行wc
。将wc
的结果存储在num
中。 @dogbane 的解决方案更有效,因为它只发回计数,而不是所有 ls 输出。但是,计算文件数量的更好方法是这样的:
Any pipes and backticks which are not escaped will be evaluated on the current host. Therefore,
means "Run
ls -1 /home/folder/anotherFolder
on the other host and count the lines on this host."These commands all run
ls
andwc
on the current host, and then assignsnum
to the result on the other host:Prototype fix:
meaning: Run
ls
on the other host, send all the data to this host, and runwc
on that. Store the result ofwc
innum
. @dogbane's solution is more effective, because it only sends back the count, not all thels
output.BUT, a better way to count the number of files is this: