为什么不是bash找到我明显存在的桌面文件夹?


1

我可能在这里做了一些愚蠢/明显错误的事情,但是我试图在curl脚本中管道并在OS X上的终端中运行它。我用来执行此操作的命令是:

sh -s stable < <(curl -L http://path_to_my_script/)

这看起来工作得非常正确; 但是,下载脚本中的第一行无效。由于某种原因,“cd~ / Desktop”导致错误“没有这样的文件或目录”。我很确定我的桌面存在,并且当我手动运行它时此命令有效。当我尝试将“ls”作为诊断问题的第一行时,它甚至都不识别该命令。我需要做些什么才能使“cd”和“ls”等命令正常工作?

谢谢您的帮助!

编辑:问题似乎与卷曲没有任何关系,好像我将脚本下载到“file.sh”然后执行“sh file.sh”,我得到相同的错误。

编辑:这是我正在尝试运行的脚本的完整内容:

#!/bin/bash
ls
cd ~/Desktop
curl https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg -o chrome.dmg
hdiutil attach chrome.dmg
cp "/Volumes/Google Chrome/Google Chrome.app" "~/Desktop/chrome.app"
hdiutil detach chrome.dmg
rm chrome.dmg
open -a chrome.app --args --make-default-browser --disable-instant-extended-api --    install-from-webstore="chrome-rdp/cbkkbcmdlboombapidmoeolnmdacpkch"
osascript 'tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.mouse"
end tell'

你有同样的错误吗?curl -L path_to_my_script | sh -s
ed。

@ed。不,我没有得到错误,但它也没有执行脚本。我想用一个命令执行脚本,这样我就可以记住命令并从任何机器运行它。
海登希夫2013年

请告诉我们您尝试运行的实际脚本。这种事情通常归结为环境变量,如果你不告诉我们你想要做什么,很难知道出了什么问题。如果您的脚本只是一个echo "Hello World!"正确的文本文件,它可以正常工作吗?
terdon

好的,这很奇怪。你能尝试一下只包含这一行的简单脚本echo "~/Desktop is : $(echo ~/Desktop)"吗?确保文件中没有其他内容,只有那一行。例如,也尝试使用不同的终端仿真器xterm。Terminal.app默认启动登录shell,这可能使事情变得复杂(不知道怎么做,但你永远都不知道)。
terdon 2013年

如果可以,您真的应该避免直接从Web服务器运行脚本。发生不良事件的可能性似乎很高。
Zoredache 2013年

Answers:


2

我相信我刚刚发现问题的根源。通过强制TextWrangler使用Unix风格的换行符(LF)而不是它显然使用的Windows风格的换行符(CRLF)进行保存,我似乎已经解决了所有问题; 我猜它有麻烦,因为它试图运行命令“set \ r \ n”而不是“set”,或“ls \ r”而不是“ls”。似乎所有我真正需要做的是使用'tr'命令从curl输出中去除所有回车字符(因为显然Pastebin正在使用CRLF换行符)。感谢大家的帮助!


1
这很像脚本中的第一行#!/bin/bash^M而不是脚本的常见问题#!/bin/bash。:)
Hennes 2013年

对于未来而言,这是一件好事。
MariusMatutiae 2013年

0

你有没有试过$ HOME而不是〜/
(没有添加评论因为我没有声誉> = 50)
另外,你可以通过做一个ls等问题并看看它说的话来解决(如果'ls'没有工作,然后尝试'/ bin / ls')...'set'是我最喜欢的命令之一...然后你可以看到你的PATH是什么等等。


切换到$ HOME不能解决问题,输入“/ bin / ls”而不是“ls”会导致另一个“没有这样的文件或目录”错误。
Hayden Schiff 2013年

找到有用的东西......任何东西,并从那里建造。(如果你的意思是'ls'和'/ bin / ls'给你那个错误,或者如果'/ bin / ls~ / Desktop'给你那个错误,那就太暧昧了。'set'内置在shell中..如果没有“那么T工作,你有更大的问题。
9mjb

拥有“<space <(command)”似乎有点奇怪。为什么不只是“<(命令)”。他们差不多,但似乎有点奇怪。您可以尝试使用“echo <(command)”和“cat <(command)”来查看您是否得到了您认为自己正在获得的内容。
2013年

我认为这两种格式之间的区别是无关紧要的,因为即使我用“bash file.sh”启动脚本,我的错误也会发生。当我在一行上有“/ bin / ls”而没有别的东西时,我得到了错误。这是运行脚本的结果,在“ls”行:image之上添加了“set”行。(忽略以后命令的错误;我还没有完成这些错误,此时它们并不重要)。
海登希夫

“命令未找到”错误实际上说“命令not foundne 2:set”好像你可能在那里得到不可打印的字符(比如从Windows回车)...也许试试“od -c <(command)”查看下载文件中的所有奇数字符。
2013年

0

没有看到你的实际脚本(提示,暗示)我无法分辨但是你有什么理由sh而不是用bash?在大多数现代系统sh实际上是一个链接到一个不同的外壳(dash例如),甚至如果不是,称bash作为sh改变其行为(从man bash):

   If  bash  is  invoked  with  the name sh, it tries to mimic the startup
   behavior of historical versions of sh as  closely  as  possible,  while
   conforming  to the POSIX standard as well.  When invoked as an interac
   tive login shell, or a non-interactive shell with the  --login  option,
   it  first  attempts  to read and execute commands from /etc/profile and
   ~/.profile, in that order.  The  --noprofile  option  may  be  used  to
   inhibit  this  behavior.  When invoked as an interactive shell with the
   name sh, bash looks for the variable ENV, expands its value  if  it  is
   defined,  and uses the expanded value as the name of a file to read and
   execute.  Since a shell invoked as sh does not attempt to read and exe
   cute  commands from any other startup files, the --rcfile option has no
   effect.  A non-interactive shell invoked with  the  name  sh  does  not
   attempt  to  read  any  other  startup files.  When invoked as sh, bash
   enters posix mode after the startup files are read.

因此,不会读取任何启动文件,并且您的环境变量将与您期望的不同。试试跑步

bash <(curl -L http://path_to_my_script/)

我不知道什么stable是那里和-s不需要的选项,无论如何,它是在没有参数自动启动:

   -s        If the -s option is present, or if no arguments remain  after
             option  processing,  then commands are read from the standard
             input.  This option allows the positional  parameters  to  be
             set when invoking an interactive shell.

我最初只是“bash”而不是“sh -s stable”,但是当我遇到问题时我试图改变一切,认为我可能使用了错误的shell(“稳定”对我来说也没有意义,但是这个使用它,我没有想法)。切换回“bash”似乎并没有解决问题。好吧,我现在将我的完整脚本添加到原始问题中。
Hayden Schiff
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.