Question about a shell script

Discussion in 'Mac Programming' started by mac2x, Sep 24, 2010.

  1. mac2x macrumors 65816

    Sep 19, 2009
    Hi all!

    I'm writing a shell script that will run pwd on the directory I am in and write the output to a text document for later use. However, I'm running into a couple of things I don't want it to do; I'm attributing that to lack of experience. :>)

    First, the output of pwd is being printed to the screen as well as being written to the file. I know the problem is with the pwd == $HOME statement because if I remove it and the if statement leaving pwd >> paths.txt, the output is only printed to the file ~/paths.txt.

    Second, I would like to make this script work only in the user's home directory. Mainly as an exercise in using if statements. :D Is there any way to do this? As it is now, it works in all directories. Nothing wrong with that, of course, but I don't need an if statement to make it work that way.

    #! /bin/sh
    if pwd  == $HOME
            pwd >> ~/paths.txt
  2. ulbador macrumors 68000


    Feb 11, 2010

    Well, you have a few problems. First off, you are executing the command, not executing and storing the results.

    The results you are seeing printed out are coming from the execution in the "if" statement.

    The reason it "works" is because it is failing on the "if" every time. Also, and this is probably nitpicking, but I'm used to bash, which is probably what you are in, so you could use some formatting help here:

    #! /bin/bash
    #this line executes the pwd command and puts it in a variable named path.  Those are backticks, next to the "1" button.
    #If you just put pwd, it will actually execute it as though you typed it and display the results to STDOUT
    #this SHOULD work.  I'm not near a bash interpreter right now so Im pulling from memory
    if [["$path" = "$HOME"]]; then
    echo $path >> ~/paths.txt
    I'm also not sure if this will work either, is $HOME set? If you want to be sure just drop in echo $path and echo $HOME before the if to see what they are.

    Here is a decent link I found about comparison in bash
  3. mac2x thread starter macrumors 65816

    Sep 19, 2009
    Thank you for the help! As I said I'm a n00b at this. I'll try your suggestions and read the webpage. :)
  4. mac2x thread starter macrumors 65816

    Sep 19, 2009
    Oh, BTW, $HOME is set on my machine. However, your script returns this error:

    line 8: [[/Users/myname: No such file or directory
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    There should be a space between [[ and what follows, and another space before ]]. Example:
    if [[ "$path" = "$HOME" ]]; then

    If you're not referring to the bash man page, or some other bash reference guide, you should be. Shell programming is quirky. Trying to learn it without at least the shell language reference guide is simply futile.

    For an example, visit the online man page:

    Then find the string [[ on the page, where it explains what [[ ]] does. Notice there is a space between [[ and its expression. For comparison, directly above it, notice there isn't a space for ((expression)).
  6. mac2x thread starter macrumors 65816

    Sep 19, 2009

    I have the large O'Reilly Classic Shell Scripting book, and it is a good book, but seems to focus on bourne shell primarily. I'll look at the man pages. Is there a better book for bash?
  7. seepel macrumors 6502


    Dec 22, 2009
    There's also a handy environment variable for this, I would just do this...
    if [ "$PWD" == "$HOME" ]; then
        pwd >> "~/paths.txt"
    Note that the double quotes will protect you against directories that might contain a space.

Share This Page