为什么使用 ssh 执行命令可以工作,但将其分配给 var 却不起作用?

发布于 2024-12-26 04:49:23 字数 938 浏览 0 评论 0原文

以下命令有效:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我家小可爱 2025-01-02 04:49:23

请尝试以下操作:

ssh host "num=\$(ls -1 /home/folder/anotherFolder | wc -l); if [ \$num -gt 3 ]; then echo yes: \$num; fi"

您需要转义 $ 符号,否则它们将在本地主机上展开。

或者,使用单引号,则不必转义 $

ssh host 'num=$(ls -1 /home/folder/anotherFolder | wc -l); if [ $num -gt 3 ]; then echo yes: $num; fi'

此外,最好使用 $(command) 格式而不是 ` command` 用于命令替换。

Try the following:

ssh host "num=\$(ls -1 /home/folder/anotherFolder | wc -l); if [ \$num -gt 3 ]; then echo yes: \$num; fi"

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 $:

ssh host 'num=$(ls -1 /home/folder/anotherFolder | wc -l); if [ $num -gt 3 ]; then echo yes: $num; fi'

Also, it is better practice to use $(command) format instead of `command` for command substitution.

明月夜 2025-01-02 04:49:23

任何未转义的管道和反引号都将在当前主机上进行评估。因此,

ssh host ls -1 /home/folder/anotherFolder | wc -l

意味着“在其他主机上运行ls -1 /home/folder/anotherFolder并计算主机上的行数”。

这些命令都在当前主机上运行lswc,然后将num分配给 em>other 主机:

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`

原型修复:

num=$(ssh host "ls -1 /home/folder/anotherFolder" | wc -l)

含义:在另一台主机上运行ls,将所有数据发送到该主机,然后在该主机上运行wc。将wc的结果存储在num中。 @dogbane 的解决方案更有效,因为它只发回计数,而不是所有 ls 输出。

但是计算文件数量的更好方法是这样的:

num=$(ssh host "find /home/folder/anotherFolder -mindepth 1 -maxdepth 1 -printf x | wc -c")

Any pipes and backticks which are not escaped will be evaluated on the current host. Therefore,

ssh host ls -1 /home/folder/anotherFolder | wc -l

means "Run ls -1 /home/folder/anotherFolder on the other host and count the lines on this host."

These commands all run ls and wc on the current host, and then assigns num to the result on the other host:

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`

Prototype fix:

num=$(ssh host "ls -1 /home/folder/anotherFolder" | wc -l)

meaning: Run ls on the other host, send all the data to this host, and run wc on that. Store the result of wc in num. @dogbane's solution is more effective, because it only sends back the count, not all the ls output.

BUT, a better way to count the number of files is this:

num=$(ssh host "find /home/folder/anotherFolder -mindepth 1 -maxdepth 1 -printf x | wc -c")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文