Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am probably doing something stupid/obviously wrong here, but I am trying to pipe in a script with curl and run it in Terminal on OS X. The command I'm using to do this is:

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

This appears to be working perfectly correctly; however, the first line in the downloaded script isn't working. For some reason, "cd ~/Desktop" is causing the error "No such file or directory". I am quite sure that my desktop exists, and this command works when I manually run it. When I tried putting in "ls" as the first line to diagnose the problem, it didn't even recognize the command. Is there something I need to be doing to make commands like "cd" and "ls" work properly?

Thanks for the help!

EDIT: The problem does not appear to have anything to do with the curl, as if I download the script into "file.sh" and then execute "sh file.sh", I get the same errors.

EDIT: Here is the full content of the script I'm trying to run:

#!/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'
share|improve this question
    
Do you get the same error with this? curl -L path_to_my_script | sh -s –  ed. Dec 9 '13 at 23:19
    
@ed. No I don't get an error with that, but it also doesn't execute the script. I want to execute the script with a single command so that I can just memorize the command and run it from any machine. –  oxguy3 Dec 9 '13 at 23:38
    
Please show us the actual script you are trying to run. This kind of thing is usually down to environmental variables and it is hard to know what's going wrong if you don't tell us what you're trying to do. It works fine if your script is just a text file with echo "Hello World!" right? –  terdon Dec 9 '13 at 23:53
    
OK, that is weird. Could you try with a simple script that only contains this line: echo "~/Desktop is : $(echo ~/Desktop)"? Make sure there is nothing else in the file, just that single line. Also try using a different terminal emulator, xterm for example. Terminal.app starts login shells by default and that might be complicating things (don't really see how but you never know). –  terdon Dec 10 '13 at 0:06
    
You really should avoid running scripts directly from a web server if you can. The chance of something bad happening seems pretty high. –  Zoredache Dec 10 '13 at 0:53

3 Answers 3

up vote 2 down vote accepted

I believe I just discovered the root of the problem. By forcing TextWrangler to save with Unix-style line breaks (LF) instead of the Windows-style line breaks (CRLF) it had apparently been using, I seem to have fixed all the problems; I guess it was having troubles because it was trying to run the command "set\r" instead of "set", or "ls\r" instead of "ls". It seems that all I really need to do is use the 'tr' command to strip all the carriage return characters from the curl output (since apparently Pastebin is using CRLF line breaks). Thanks for the help everyone!

share|improve this answer
1  
This is a lot like the common problem where the first line in a script turns out to be #!/bin/bash^M rather than #!/bin/bash. :) –  Hennes Dec 10 '13 at 2:43
    
That's a good thing to keep in mind for the future. –  MariusMatutiae Dec 10 '13 at 3:53

Have you tried $HOME instead of ~/
(not added as a comment because I don't have a reputation >=50)
Also, you could troubleshoot by doing an ls etc and seeing what it says (if 'ls' doesn't work, then try '/bin/ls')... 'set' is one of my favorite commands... then you can see what your PATH is etc.

share|improve this answer
    
Switching to $HOME doesn't fix the problem, and typing "/bin/ls" instead of "ls" causes another "No such file or directory" error. –  oxguy3 Dec 9 '13 at 23:49
    
Find something that works... anything, and build from there. (it's ambiguous if you meant 'ls' and '/bin/ls' gave you that error, or if '/bin/ls ~/Desktop' gave you that error. 'set' is built into the shell.. if that doesn't work then you have bigger problems. –  9mjb Dec 9 '13 at 23:56
    
It also seems a little odd to have "< space <(command)". Why not just "<(command)". They are near the same, but it seems a little odd. You could try using "echo <(command)" and "cat <(command)" to see if you are getting what you think you are getting. –  9mjb Dec 9 '13 at 23:59
    
I think the difference between the two formats is irrelevant since my error is happening even when I launch the script with "bash file.sh". And I got the error when I had "/bin/ls" on a single line with nothing else. And here is the result of running the script with the line "set" added above the "ls" line: image. (ignore the errors with later commands; I hadn't gotten to those yet and they don't matter at this point). –  oxguy3 Dec 10 '13 at 0:04
    
That "command not found" error actually says "command not foundne 2: set" as if maybe you are getting non-printable characters in there (like carriage returns from Windows)... maybe try "od -c <(command)" to see all the odd characters in your downloaded file. –  9mjb Dec 10 '13 at 0:11

I can't tell without seeing your actual script (hint, hint) but is there any reason you are using sh instead of bash? In most modern systems sh is actually a link to a different shell (dash for example) and even if it isn't, calling bash as sh changes its behavior (from 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.

So, no startup files are read, and your environmental variables will be different to what you expect. Try running

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

I don't know what the stable was there for and the -s option is not needed anyway, it is activated automatically in the absence of arguments:

   -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.
share|improve this answer
    
I originally had just "bash" instead of "sh -s stable", but when I had problems I tried changing things around thinking maybe I was using the wrong shell (the "stable" didn't make sense to me either, but this used it and I was out of ideas). Switching back to "bash" doesn't seem to be fixing the problems. And okay, I'll add my full script to the original question now. –  oxguy3 Dec 9 '13 at 23:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.